Skip to content

Commit bf336e0

Browse files
committed
Use logging macros and iostream
1 parent 148ef20 commit bf336e0

3 files changed

Lines changed: 28 additions & 11 deletions

File tree

lib/storage/storage.cpp

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ static_assert(ARDUINO_USB_MODE == 0, "must be used when USB is in OTG mode");
1010
#include <cstdint>
1111
#include <esp_err.h>
1212
#include <esp_partition.h>
13+
#include <iostream>
1314

1415
#if ARDUINO_USB_CDC_ON_BOOT == 1
1516
#define HWSerial Serial0
@@ -22,13 +23,14 @@ static USBMSC MSC;
2223
static constexpr std::uint16_t blockSize = 512; // Should be 512
2324

2425
static const esp_partition_t *partition = nullptr;
26+
static const char *const TAG = "STORAGE";
2527

2628
// Callback invoked when received WRITE10 command.
2729
// Process data in buffer to disk's storage and
2830
// return number of written bytes (must be multiple of block size)
2931
static int32_t onWrite(uint32_t lba, uint32_t offset, uint8_t *buffer, uint32_t bufsize)
3032
{
31-
HWSerial.printf("MSC WRITE: lba: %u, offset: %u, bufsize: %u\n", lba, offset, bufsize);
33+
ESP_LOGV(TAG, "MSC WRITE: lba: %u, offset: %u, bufsize: %u\n", lba, offset, bufsize);
3234
uint32_t byteOffset = lba * blockSize + offset;
3335
// erase must be called before write
3436
ESP_ERROR_CHECK(esp_partition_erase_range(partition, byteOffset, bufsize));
@@ -41,15 +43,15 @@ static int32_t onWrite(uint32_t lba, uint32_t offset, uint8_t *buffer, uint32_t
4143
// return number of copied bytes (must be multiple of block size)
4244
static int32_t onRead(uint32_t lba, uint32_t offset, void *buffer, uint32_t bufsize)
4345
{
44-
HWSerial.printf("MSC READ: lba: %u, offset: %u, bufsize: %u\n", lba, offset, bufsize);
46+
ESP_LOGV(TAG, "MSC READ: lba: %u, offset: %u, bufsize: %u\n", lba, offset, bufsize);
4547
uint32_t byteOffset = lba * blockSize + offset;
4648
ESP_ERROR_CHECK(esp_partition_read(partition, byteOffset, buffer, bufsize));
4749
return bufsize;
4850
}
4951

5052
static bool onStartStop(uint8_t power_condition, bool start, bool load_eject)
5153
{
52-
HWSerial.printf("MSC START/STOP: power: %u, start: %u, eject: %u\n", power_condition, start, load_eject);
54+
ESP_LOGV(TAG, "MSC START/STOP: power: %u, start: %u, eject: %u\n", power_condition, start, load_eject);
5355
return true;
5456
}
5557

@@ -58,18 +60,19 @@ static bool onStartStop(uint8_t power_condition, bool start, bool load_eject)
5860
*/
5961
static void listFiles(const char *const dirname)
6062
{
61-
HWSerial.printf("Directory: '%s'\n", dirname);
63+
std::cout << "Directory: '" << dirname << "'" << std::endl;
6264
File root = FFat.open(dirname);
6365
if (!root || !root.isDirectory())
6466
{
65-
HWSerial.printf("Error: '%s' is not a directory!\n", dirname);
67+
ESP_LOGE(TAG, "Error: '%s' is not a directory!\n", dirname);
6668
return;
6769
}
6870

6971
File file = root.openNextFile();
7072
while (file)
7173
{
72-
HWSerial.printf(" %s (%s, %d Bytes)\n", file.name(), file.isDirectory() ? "d" : "f", file.size());
74+
std::cout << "\t" << file.name() << " (" << (file.isDirectory() ? "d" : "f") << ", " << file.size() << " Bytes)"
75+
<< std::endl;
7376
file = root.openNextFile();
7477
}
7578
}
@@ -129,18 +132,18 @@ void Storage::begin()
129132

130133
if (!FFat.begin(true))
131134
{ // `true` = Formatieren falls kein Dateisystem vorhanden
132-
HWSerial.println("Failed to init files system, flash may not be formatted");
135+
ESP_LOGE(TAG, "Failed to init files system, flash may not be formatted");
133136
return;
134137
}
135-
HWSerial.println("FatFS erfolgreich gemountet.");
138+
ESP_LOGI(TAG, "FatFS erfolgreich gemountet.");
136139

137140
partition = check_ffat_partition(FFAT_PARTITION_LABEL);
138141
if (!partition)
139142
{
140-
printf("Error with FAT partition");
143+
ESP_LOGE(TAG, "Error with FAT partition");
141144
return;
142145
}
143-
HWSerial.printf("Flash has a size of %u bytes\n", FFat.totalBytes());
146+
ESP_LOGI(TAG, "Flash has a size of %u bytes\n", FFat.totalBytes());
144147

145148
USB.onEvent(ARDUINO_USB_STARTED_EVENT, usbStartedCallback);
146149
USB.onEvent(ARDUINO_USB_STOPPED_EVENT, usbStoppedCallback);

platformio.ini

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ board_build.filesystem = fatfs
66
board_build.partitions = default_ffat_8MB.csv
77
monitor_filters = esp32_exception_decoder
88
monitor_speed = 115200
9+
build_type = debug
910

1011
build_unflags =
1112
-DARDUINO_USB_MODE=1
@@ -18,3 +19,5 @@ build_flags =
1819
-DARDUINO_USB_CDC_ON_BOOT=1
1920
-DARDUINO_USB_MSC_ON_BOOT=0
2021
-DARDUINO_USB_DFU_ON_BOOT=0
22+
-DCONFIG_ARDUHAL_LOG_COLORS=1
23+
-DCORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_VERBOSE

src/main.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ static_assert(ARDUINO_USB_MODE == 0, "must be used when USB is in OTG mode");
44
#endif
55
#include <USB.h>
66
#include <USBMSC.h>
7+
#include <esp32-hal-log.h>
78
#include <esp_err.h>
9+
#include <iostream>
810
#include <storage.hpp>
911

1012
#if ARDUINO_USB_CDC_ON_BOOT == 1
@@ -13,10 +15,19 @@ static_assert(ARDUINO_USB_MODE == 0, "must be used when USB is in OTG mode");
1315
#define HWSerial Serial
1416
#endif
1517

18+
static const char *const TAG = "MAIN";
19+
1620
void setup()
1721
{
1822
HWSerial.begin(115200);
1923
HWSerial.setDebugOutput(true);
24+
ESP_LOGV(TAG, "verbose");
25+
ESP_LOGD(TAG, "debug");
26+
ESP_LOGI(TAG, "info");
27+
ESP_LOGW(TAG, "warning");
28+
ESP_LOGE(TAG, "error");
29+
delay(200); // wait for the serial monitor to be ready
30+
std::cout << "Started program" << std::endl;
2031
Storage::begin();
2132
}
2233

@@ -26,7 +37,7 @@ void loop()
2637
static uint32_t lastTrigger = 0;
2738
if (millis() - lastTrigger > 1000)
2839
{
29-
HWSerial.print(".");
40+
std::cout << "." << std::flush;
3041
lastTrigger = millis();
3142
}
3243
}

0 commit comments

Comments
 (0)