Skip to content

Commit

Permalink
fixed ledc deinit before deep sleep
Browse files Browse the repository at this point in the history
  • Loading branch information
karaulj-bfusa committed Jul 7, 2021
1 parent 2acbaae commit 9c00f9c
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 59 deletions.
13 changes: 10 additions & 3 deletions components/device_profile/RedDeviceProfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ void RedDeviceProfile::gestureUpCallback()
// BLE HID status
xQueueSend(
ledCmdQueue,
(void*)&((bleConnected) ?
(void*)&(bleConnected ?
RGB_CMD_BLE_CONNECTED : RGB_CMD_BLE_DISCONNECTED),
1/portTICK_PERIOD_MS
);
Expand All @@ -56,9 +56,9 @@ void RedDeviceProfile::gestureUpCallback()

void RedDeviceProfile::gestureDownCallback()
{
ESP_LOGI(TAG, "%s: batteryChargingStatus", __func__);
/*
// battery charging status
ESP_LOGI(TAG, "%s: isBatteryCharging", __func__);
xQueueSend(
ledCmdQueue,
(void*)&((TinyPICO::getInstance()->isBatteryCharging()) ?
Expand All @@ -67,14 +67,21 @@ void RedDeviceProfile::gestureDownCallback()
);
*/
// enter deep sleep
ESP_LOGI(TAG, "%s: enterDeepSleep", __func__);
xQueueSend(
ledCmdQueue,
(void*)&RGB_CMD_DEEP_SLEEP_START,
1/portTICK_PERIOD_MS
);
vTaskDelay(1000/portTICK_PERIOD_MS); // LED animation
bleDisable();
vTaskDelay(500/portTICK_PERIOD_MS);
vTaskDelay(900/portTICK_PERIOD_MS);
xQueueSend(
ledCmdQueue,
(void*)&RGB_CMD_SYS_SHUTDOWN,
1/portTICK_PERIOD_MS
); // shutdown LED; happens once
vTaskDelay(100/portTICK_PERIOD_MS);
esp_deep_sleep_start();
}

Expand Down
90 changes: 52 additions & 38 deletions components/rgb_led/RGB_CC_LED.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@
static const char *TAG = "RGB_CC_LED";


RGB_CC_LED::RGB_CC_LED(uint8_t rPin, uint8_t gPin, uint8_t bPin
) : r((gpio_num_t)rPin), g((gpio_num_t)gPin), b((gpio_num_t)bPin)
RGB_CC_LED::RGB_CC_LED(uint8_t rPin, uint8_t gPin, uint8_t bPin,
ledc_channel_t rChannel, ledc_channel_t gChannel, ledc_channel_t bChannel,
ledc_mode_t speedModeAll
) : r((gpio_num_t)rPin), g((gpio_num_t)gPin), b((gpio_num_t)bPin),
rChan(rChannel), gChan(gChannel), bChan(bChannel), speedMode(speedModeAll)
{
esp_err_t ret;

Expand All @@ -39,8 +42,8 @@ RGB_CC_LED::RGB_CC_LED(uint8_t rPin, uint8_t gPin, uint8_t bPin
// R ledc channel setup
ledc_channel_config_t ledc_channel = {
.gpio_num = r,
.speed_mode = RGB_CC_LED_SPEED_MODE,
.channel = RGB_CC_LED_R_CHANNEL,
.speed_mode = speedMode,
.channel = rChan,
.intr_type = RGB_CC_LED_INTR_TYPE,
.timer_sel = RGB_CC_LED_TIMER_NUM,
.duty = 0,
Expand All @@ -51,14 +54,14 @@ RGB_CC_LED::RGB_CC_LED(uint8_t rPin, uint8_t gPin, uint8_t bPin
ESP_LOGE(TAG, "Could not initialize R ledc channel: %d", (int)ret);
}
// G
ledc_channel.channel = RGB_CC_LED_G_CHANNEL;
ledc_channel.channel = gChan;
ledc_channel.gpio_num = g;
ret = ledc_channel_config(&ledc_channel);
if (ret != ESP_OK) {
ESP_LOGE(TAG, "Could not initialize G ledc channel: %d", (int)ret);
}
// B
ledc_channel.channel = RGB_CC_LED_B_CHANNEL;
ledc_channel.channel = bChan;
ledc_channel.gpio_num = b;
ret = ledc_channel_config(&ledc_channel);
if (ret != ESP_OK) {
Expand All @@ -78,56 +81,67 @@ RGB_CC_LED::RGB_CC_LED(uint8_t rPin, uint8_t gPin, uint8_t bPin
}


RGB_CC_LED::~RGB_CC_LED()
void RGB_CC_LED::deInit()
{
if (ledInit)
{
ledc_stop(RGB_CC_LED_SPEED_MODE, RGB_CC_LED_R_CHANNEL, 0);
ledc_stop(RGB_CC_LED_SPEED_MODE, RGB_CC_LED_G_CHANNEL, 0);
ledc_stop(RGB_CC_LED_SPEED_MODE, RGB_CC_LED_B_CHANNEL, 0);
ledc_stop(speedMode, rChan, 0);
ledc_stop(speedMode, gChan, 0);
ledc_stop(speedMode, bChan, 0);

ledc_fade_func_uninstall();

ledInit = 0;
}
}


RGB_CC_LED::~RGB_CC_LED()
{
if (ledInit)
{
deInit();
}
}


void RGB_CC_LED::fadeAll(color_level_t rVal, color_level_t gVal, color_level_t bVal,
uint32_t rFadeTimeMs, uint32_t gFadeTimeMs, uint32_t bFadeTimeMs)
{
if (ledInit)
{
ledc_set_fade_time_and_start(
RGB_CC_LED_SPEED_MODE,
RGB_CC_LED_R_CHANNEL,
rVal,
rFadeTimeMs,
RGB_CC_LED_FADE_MODE
);
ledc_set_fade_time_and_start(
RGB_CC_LED_SPEED_MODE,
RGB_CC_LED_G_CHANNEL,
gVal,
gFadeTimeMs,
RGB_CC_LED_FADE_MODE
);
ledc_set_fade_time_and_start(
RGB_CC_LED_SPEED_MODE,
RGB_CC_LED_B_CHANNEL,
bVal,
bFadeTimeMs,
RGB_CC_LED_FADE_MODE
);
colorLevels[0] = rVal;
colorLevels[1] = gVal;
colorLevels[2] = bVal;
}
ledc_set_fade_time_and_start(
RGB_CC_LED_SPEED_MODE,
RGB_CC_LED_R_CHANNEL,
rVal,
rFadeTimeMs,
RGB_CC_LED_FADE_MODE
);
ledc_set_fade_time_and_start(
RGB_CC_LED_SPEED_MODE,
RGB_CC_LED_G_CHANNEL,
gVal,
gFadeTimeMs,
RGB_CC_LED_FADE_MODE
);
ledc_set_fade_time_and_start(
RGB_CC_LED_SPEED_MODE,
RGB_CC_LED_B_CHANNEL,
bVal,
bFadeTimeMs,
RGB_CC_LED_FADE_MODE
);
colorLevels[0] = rVal;
colorLevels[1] = gVal;
colorLevels[2] = bVal;
}


void RGB_CC_LED::setColor(color_level_t rVal, color_level_t gVal, color_level_t bVal,
uint32_t rFadeTimeMs, uint32_t gFadeTimeMs, uint32_t bFadeTimeMs)
{
return fadeAll(rVal, gVal, bVal, rFadeTimeMs, gFadeTimeMs, bFadeTimeMs);
if (ledInit)
{
return fadeAll(rVal, gVal, bVal, rFadeTimeMs, gFadeTimeMs, bFadeTimeMs);
}
}


Expand Down
10 changes: 9 additions & 1 deletion components/rgb_led/include/RGB_CC_LED.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,15 @@ class RGB_CC_LED
{
public:
const gpio_num_t r, g, b;
RGB_CC_LED(uint8_t rPin, uint8_t gPin, uint8_t bPin);
const ledc_channel_t rChan, gChan, bChan;
const ledc_mode_t speedMode;
RGB_CC_LED(uint8_t rPin, uint8_t gPin, uint8_t bPin,
ledc_channel_t rChannel = RGB_CC_LED_R_CHANNEL,
ledc_channel_t gChannel = RGB_CC_LED_G_CHANNEL,
ledc_channel_t bChannel = RGB_CC_LED_B_CHANNEL,
ledc_mode_t speedMode = RGB_CC_LED_SPEED_MODE
);
void deInit();
~RGB_CC_LED();

void setColor(
Expand Down
30 changes: 16 additions & 14 deletions components/rgb_led/include/rgb_commands.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,26 @@

// All RGB commands defined here
typedef uint8_t rgb_cmd_t;
// shutdown LED instance (can be called once)
const rgb_cmd_t RGB_CMD_SYS_SHUTDOWN = 0x00;
// system
const rgb_cmd_t RGB_CMD_SYSTEM_INIT = 0x00;
const rgb_cmd_t RGB_CMD_RESTART = 0x01;
const rgb_cmd_t RGB_CMD_DEEP_SLEEP_START = 0x02;
const rgb_cmd_t RGB_CMD_CLEAR_ALL = 0x0F;
const rgb_cmd_t RGB_CMD_INIT = 0x10;
const rgb_cmd_t RGB_CMD_RESTART = 0x11;
const rgb_cmd_t RGB_CMD_DEEP_SLEEP_START = 0x12;
const rgb_cmd_t RGB_CMD_CLEAR_ALL = 0x1F;
// ble
const rgb_cmd_t RGB_CMD_BLE_CONNECTED = 0x10;
const rgb_cmd_t RGB_CMD_BLE_DISCONNECTED = 0x11;
const rgb_cmd_t RGB_CMD_BLE_PAIRING_MODE_ACTIVE = 0x12;
const rgb_cmd_t RGB_CMD_BLE_CONNECTED = 0x20;
const rgb_cmd_t RGB_CMD_BLE_DISCONNECTED = 0x21;
const rgb_cmd_t RGB_CMD_BLE_PAIRING_MODE_ACTIVE = 0x22;
// device mode - joystick gesture recognition
const rgb_cmd_t RGB_CMD_DEVICE_MODE = 0x20;
const rgb_cmd_t RGB_CMD_GESTURE_ACTIVE = 0x21;
const rgb_cmd_t RGB_CMD_GESTURE_FOUND = 0x22;
const rgb_cmd_t RGB_CMD_GESTURE_NOT_FOUND = 0x23;
const rgb_cmd_t RGB_CMD_GESTURE_NOT_IMPL = 0x24;
const rgb_cmd_t RGB_CMD_CANCEL_GESTURE = 0x25;
const rgb_cmd_t RGB_CMD_DEVICE_MODE = 0x30;
const rgb_cmd_t RGB_CMD_GESTURE_ACTIVE = 0x31;
const rgb_cmd_t RGB_CMD_GESTURE_FOUND = 0x32;
const rgb_cmd_t RGB_CMD_GESTURE_NOT_FOUND = 0x33;
const rgb_cmd_t RGB_CMD_GESTURE_NOT_IMPL = 0x34;
const rgb_cmd_t RGB_CMD_CANCEL_GESTURE = 0x35;
// training mode
const rgb_cmd_t RGB_CMD_TRAINING_MODE = 0x30;
const rgb_cmd_t RGB_CMD_TRAINING_MODE = 0x40;
// battery
const rgb_cmd_t RGB_CMD_BATTERY_RANGE = 0x60;
const rgb_cmd_t RGB_CMD_BATTERY_LEVEL = 0x61;
Expand Down
2 changes: 1 addition & 1 deletion main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ void app_main(void)
}

// system init cplt
xQueueSend(ledCmdQueue, (void*)&RGB_CMD_SYSTEM_INIT, 1/portTICK_PERIOD_MS);
xQueueSend(ledCmdQueue, (void*)&RGB_CMD_INIT, 1/portTICK_PERIOD_MS);


/*
Expand Down
13 changes: 11 additions & 2 deletions main/tasks/tasks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,13 @@ void touchDeepSleepTask(void *pvParameter)
if (esp_timer_get_time()-last >= delay)
{
bleDisable();
vTaskDelay(500/portTICK_PERIOD_MS); // LED animation
vTaskDelay(900/portTICK_PERIOD_MS); // LED animation
xQueueSend(
ledCmdQueue,
(void*)&RGB_CMD_SYS_SHUTDOWN,
1/portTICK_PERIOD_MS
); // shutdown LED; happens once
vTaskDelay(100/portTICK_PERIOD_MS); // LED animation
esp_deep_sleep_start();
}

Expand Down Expand Up @@ -125,7 +131,10 @@ void ledCmdTask(void *pvParameter)
{
switch(ledCmd)
{
case RGB_CMD_SYSTEM_INIT:
case RGB_CMD_SYS_SHUTDOWN:
led->deInit();
break;
case RGB_CMD_INIT:
rgbcSystemInit(led);
break;
case RGB_CMD_RESTART:
Expand Down

0 comments on commit 9c00f9c

Please sign in to comment.