Skip to content

Commit 8d812dd

Browse files
committed
refactor delay out
1 parent a803fcd commit 8d812dd

File tree

3 files changed

+14
-11
lines changed

3 files changed

+14
-11
lines changed

src/sp140/extra-data.ino

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ static BLECharacteristic* pBMSCellVoltages = nullptr;
137137
void updateThrottleBLE(int value) {
138138
// Handle disconnecting
139139
if (!deviceConnected && oldDeviceConnected) {
140-
delay(500); // give the bluetooth stack the chance to get things ready
140+
vTaskDelay(pdMS_TO_TICKS(500)); // give the bluetooth stack the chance to get things ready
141141
pServer->startAdvertising(); // restart advertising
142142
USBSerial.println("Start advertising");
143143
oldDeviceConnected = deviceConnected;
@@ -153,7 +153,7 @@ void updateThrottleBLE(int value) {
153153
try {
154154
pThrottleCharacteristic->setValue((uint8_t*)&value, sizeof(value));
155155
pThrottleCharacteristic->notify();
156-
delay(5); // prevent bluetooth stack congestion - can be as low as 3ms
156+
vTaskDelay(pdMS_TO_TICKS(5)); // prevent bluetooth stack congestion - can be as low as 3ms
157157
} catch (...) {
158158
USBSerial.println("Error sending BLE notification");
159159
}

src/sp140/sp140.ino

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ void blinkLEDTask(void *pvParameters) {
275275

276276
for (;;) { // infinite loop
277277
blinkLED(); // call blinkLED function
278-
delay(500); // wait for 500ms
278+
vTaskDelay(pdMS_TO_TICKS(500)); // wait for 500ms
279279
}
280280
vTaskDelete(NULL); // should never reach this
281281
}
@@ -285,7 +285,7 @@ void throttleTask(void *pvParameters) {
285285

286286
for (;;) { // infinite loop
287287
handleThrottle(); //
288-
delay(20); // wait for 20ms
288+
vTaskDelay(pdMS_TO_TICKS(20)); // wait for 20ms
289289
}
290290
vTaskDelete(NULL); // should never reach this
291291
}
@@ -445,7 +445,7 @@ void setupAnalogRead() {
445445
void setupWatchdog() {
446446
#ifndef OPENPPG_DEBUG
447447
// Initialize Task Watchdog
448-
//ESP_ERROR_CHECK(esp_task_wdt_init(3000, true)); // 3 second timeout, panic on timeout
448+
ESP_ERROR_CHECK(esp_task_wdt_init(3000, true)); // 3 second timeout, panic on timeout
449449
#endif // OPENPPG_DEBUG
450450
}
451451

@@ -623,7 +623,7 @@ void setup140() {
623623

624624
// main loop all work is done in tasks
625625
void loop() {
626-
delay(25);
626+
vTaskDelay(pdMS_TO_TICKS(25));
627627
}
628628

629629
void initButtons() {
@@ -783,7 +783,7 @@ void disarmSystem() {
783783
updateArmedTime();
784784
writeDeviceData();
785785

786-
delay(500); // TODO: just disable button thread to not allow immediate rearming
786+
vTaskDelay(pdMS_TO_TICKS(500)); // TODO: just disable button thread to not allow immediate rearming
787787
// Set the last disarm time
788788
lastDisarmTime = millis();
789789
}

src/sp140/vibration_pwm.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
#include "sp140/vibration_pwm.h"
22
#include "Arduino.h"
33
#include "sp140/shared-config.h"
4-
5-
const int VIBE_PWM_PIN = 46; // TODO: move to config
4+
#include "sp140/esp32s3-config.h"
65
const int VIBE_PWM_FREQ = 1000; // Adjust as needed
76
const int VIBE_PWM_RESOLUTION = 8; // 8-bit resolution
87

@@ -41,8 +40,9 @@ void vibeTask(void* parameter) {
4140
* @return Returns true if initialization was successful, false otherwise
4241
*/
4342
bool initVibeMotor() {
43+
extern HardwareConfig board_config;
4444
ledcSetup(VIBE_PWM_CHANNEL, VIBE_PWM_FREQ, VIBE_PWM_RESOLUTION);
45-
ledcAttachPin(VIBE_PWM_PIN, VIBE_PWM_CHANNEL);
45+
ledcAttachPin(board_config.vibe_pwm, VIBE_PWM_CHANNEL);
4646

4747
// Create vibration queue
4848
vibeQueue = xQueueCreate(5, sizeof(VibeRequest));
@@ -72,7 +72,10 @@ void pulseVibeMotor() {
7272
};
7373

7474
// Send request to queue (non-blocking)
75-
xQueueSend(vibeQueue, &request, 0); // Don't wait if queue is full
75+
if (xQueueSend(vibeQueue, &request, 0) != pdTRUE) {
76+
// Handle queue full error
77+
USBSerial.println("Vibration queue full!");
78+
}
7679
}
7780

7881
/**

0 commit comments

Comments
 (0)