|
4 | 4 | #include "bsp_delay.h" |
5 | 5 | #include "rgb_marquee.h" |
6 | 6 | #include "bsp_time.h" |
| 7 | +#include "ble_main.h" |
| 8 | +#include "app_timer.h" |
7 | 9 |
|
8 | 10 |
|
9 | 11 | #define NRF_LOG_MODULE_NAME rgb |
@@ -33,7 +35,6 @@ nrf_drv_pwm_config_t pwm_config = {//PWM configuration structure |
33 | 35 | }; |
34 | 36 | static autotimer *timer; |
35 | 37 | static uint8_t ledblink6_step = 0; |
36 | | -static uint8_t ledblink6_color = RGB_RED; |
37 | 38 | static uint8_t ledblink1_step = 0; |
38 | 39 | extern bool g_usb_led_marquee_enable; |
39 | 40 |
|
@@ -372,118 +373,68 @@ void ledblink5(uint8_t color, uint8_t start, uint8_t stop) { |
372 | 373 | } |
373 | 374 |
|
374 | 375 |
|
375 | | -// Charging animation |
376 | | -// the current percentage of the battery 0-4 4 represents full electric breathing light |
| 376 | +// Charging animation - displays battery percentage on all LEDs sequentially |
377 | 377 | volatile bool callback_waiting6 = 0; |
| 378 | +static uint8_t ledblink6_color = RGB_GREEN; |
| 379 | +static uint8_t prev_leds_lit = 0; |
| 380 | +static uint8_t prev_percentage = 101; // Initialize to impossible value to force first update |
| 381 | + |
378 | 382 | void ledblink6_pwm_callback(nrfx_pwm_evt_type_t event_type) { |
379 | 383 | if (event_type == NRF_DRV_PWM_EVT_FINISHED) { |
380 | 384 | callback_waiting6 = 1; |
381 | 385 | } |
382 | 386 | } |
| 387 | + |
383 | 388 | void ledblink6(void) { |
384 | 389 | uint32_t *led_array = hw_get_led_array(); |
385 | | - const uint16_t delay_time = 25; |
386 | | - static int16_t light_level = 99; //LED brightness value |
387 | | - |
388 | | - if (!g_usb_led_marquee_enable && ledblink6_step != 0) { |
389 | | - light_level = 99; |
390 | | - callback_waiting6 = 0; |
391 | | - rgb_marquee_stop(); |
392 | | - return; |
393 | | - } |
394 | | - |
395 | | - if (ledblink6_step == 0) { |
396 | | - set_slot_light_color(ledblink6_color); |
397 | | - for (uint8_t i = 0; i < RGB_LIST_NUM; i++) { |
398 | | - nrf_gpio_pin_clear(led_array[i]); |
399 | | - } |
400 | | - pwm_config.output_pins[0] = led_array[2]; |
401 | | - pwm_config.output_pins[1] = led_array[3]; |
402 | | - pwm_config.output_pins[2] = led_array[4]; |
403 | | - pwm_config.output_pins[3] = led_array[5]; |
404 | | - ledblink6_step = 1; |
405 | | - |
406 | | - // Reset the state of the lamp when the USB is not turned on |
407 | | - ledblink1_step = 0; |
408 | | - } |
409 | | - |
410 | | - if (ledblink6_step == 1) { |
411 | | - light_level = 0; |
412 | | - ledblink6_step = 2; |
413 | | - } |
414 | | - |
415 | | - if (ledblink6_step == 2 || ledblink6_step == 3 || ledblink6_step == 4) { |
416 | | - if (light_level <= 99) { |
417 | | - if (ledblink6_step == 2) { |
418 | | - //Treatment brightness |
419 | | - pwm_sequ_val.channel_0 = get_pwmduty(light_level); |
420 | | - pwm_sequ_val.channel_1 = pwm_sequ_val.channel_0; |
421 | | - pwm_sequ_val.channel_2 = pwm_sequ_val.channel_0; |
422 | | - pwm_sequ_val.channel_3 = pwm_sequ_val.channel_0; |
423 | | - nrfx_pwm_uninit(&pwm0_ins); //Close PWM output |
424 | | - set_slot_light_color(ledblink6_color); |
425 | | - nrf_drv_pwm_init(&pwm0_ins, &pwm_config, ledblink6_pwm_callback); |
426 | | - nrf_drv_pwm_simple_playback(&pwm0_ins, &seq, 1, NRF_DRV_PWM_FLAG_LOOP); |
427 | | - ledblink6_step = 3; |
428 | | - } |
429 | | - if (ledblink6_step == 3) { //Waiting for the output of the PWM module to complete |
430 | | - if (callback_waiting6 != 0) { |
431 | | - ledblink6_step = 4; |
432 | | - bsp_set_timer(timer, 0); |
433 | | - } |
434 | | - } |
435 | | - if (ledblink6_step == 4) { |
436 | | - if (!NO_TIMEOUT_1MS(timer, delay_time)) { |
437 | | - callback_waiting = 0; |
438 | | - light_level++; |
439 | | - ledblink6_step = 2; |
440 | | - } |
| 390 | + |
| 391 | + // Update battery level when percentage changes or when marquee is disabled |
| 392 | + if (prev_percentage != percentage_batt_lvl || !g_usb_led_marquee_enable) { |
| 393 | + |
| 394 | + if (!g_usb_led_marquee_enable) { |
| 395 | + // If marquee is disabled, turn off all LEDs and return |
| 396 | + for (uint8_t i = 0; i < RGB_LIST_NUM; i++) { |
| 397 | + nrf_gpio_pin_clear(led_array[i]); |
441 | 398 | } |
| 399 | + return; |
| 400 | + } |
| 401 | + |
| 402 | + // Calculate how many LEDs should be lit based on battery percentage |
| 403 | + // Map 0-100% to 0-8 LEDs |
| 404 | + uint8_t leds_to_light = (percentage_batt_lvl * RGB_LIST_NUM) / 100; |
| 405 | + |
| 406 | + // Update the color based on battery level (green when full, red when low) |
| 407 | + if (percentage_batt_lvl >= 75) { |
| 408 | + ledblink6_color = RGB_GREEN; // Green for high battery |
| 409 | + } else if (percentage_batt_lvl >= 50) { |
| 410 | + ledblink6_color = RGB_CYAN; // Cyan for medium-high battery |
| 411 | + } else if (percentage_batt_lvl >= 25) { |
| 412 | + ledblink6_color = RGB_YELLOW; // Yellow for medium-low battery |
442 | 413 | } else { |
443 | | - ledblink6_step = 5; |
| 414 | + ledblink6_color = RGB_RED; // Red for low battery |
444 | 415 | } |
445 | | - } |
446 | | - |
447 | | - if (ledblink6_step == 5) { |
448 | | - light_level = 99; |
449 | | - ledblink6_step = 6; |
450 | | - } |
451 | | - |
452 | | - if (ledblink6_step == 6 || ledblink6_step == 7 || ledblink6_step == 8) { |
453 | | - if (light_level >= 0) { |
454 | | - if (ledblink6_step == 6) { |
455 | | - //Treatment brightness |
456 | | - pwm_sequ_val.channel_0 = get_pwmduty(light_level); |
457 | | - pwm_sequ_val.channel_1 = pwm_sequ_val.channel_0; |
458 | | - pwm_sequ_val.channel_2 = pwm_sequ_val.channel_0; |
459 | | - pwm_sequ_val.channel_3 = pwm_sequ_val.channel_0; |
460 | | - nrfx_pwm_uninit(&pwm0_ins); //Close PWM output |
461 | | - set_slot_light_color(ledblink6_color); |
462 | | - nrf_drv_pwm_init(&pwm0_ins, &pwm_config, ledblink6_pwm_callback); |
463 | | - nrf_drv_pwm_simple_playback(&pwm0_ins, &seq, 1, NRF_DRV_PWM_FLAG_LOOP); |
464 | | - ledblink6_step = 7; |
465 | | - } |
466 | | - if (ledblink6_step == 7) { //Waiting for the output of the PWM module to complete |
467 | | - if (callback_waiting6 != 0) { |
468 | | - ledblink6_step = 8; |
469 | | - bsp_set_timer(timer, 0); |
470 | | - } |
| 416 | + |
| 417 | + // Set the color |
| 418 | + set_slot_light_color(ledblink6_color); |
| 419 | + |
| 420 | + // Gradually update LEDs to show smooth transition |
| 421 | + if (leds_to_light > prev_leds_lit) { |
| 422 | + // Turn on additional LEDs one by one |
| 423 | + for (uint8_t i = prev_leds_lit; i < leds_to_light && i < RGB_LIST_NUM; i++) { |
| 424 | + nrf_gpio_pin_set(led_array[i]); |
| 425 | + bsp_delay_ms(50); // Small delay for smooth visual effect |
471 | 426 | } |
472 | | - if (ledblink6_step == 8) { |
473 | | - if (!NO_TIMEOUT_1MS(timer, delay_time)) { |
474 | | - callback_waiting = 0; |
475 | | - light_level--; |
476 | | - ledblink6_step = 6; |
477 | | - } |
| 427 | + } else if (leds_to_light < prev_leds_lit) { |
| 428 | + // Turn off LEDs one by one |
| 429 | + for (uint8_t i = prev_leds_lit; i > leds_to_light && i > 0; i--) { |
| 430 | + nrf_gpio_pin_clear(led_array[i-1]); |
| 431 | + bsp_delay_ms(50); // Small delay for smooth visual effect |
478 | 432 | } |
479 | | - } else { |
480 | | - ledblink6_step = 0; |
481 | | - //if (++ledblink6_color == RGB_WHITE) ledblink6_color = RGB_RED; |
482 | | - uint8_t new_color = rand() % 6; |
483 | | - for (; new_color == ledblink6_color; new_color = rand() % 6); |
484 | | - ledblink6_color = new_color; |
485 | 433 | } |
| 434 | + |
| 435 | + prev_leds_lit = leds_to_light; |
486 | 436 | } |
| 437 | + prev_percentage = percentage_batt_lvl; |
487 | 438 | } |
488 | 439 |
|
489 | 440 | /** |
|
0 commit comments