2017-09-22 21:05:51 -04:00
|
|
|
/*
|
|
|
|
* This file is part of the Micro Python project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2017 Scott Shawcroft for Adafruit Industries
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
2016-12-09 22:27:41 -05:00
|
|
|
|
|
|
|
#include "mphalport.h"
|
2017-04-12 18:24:50 -04:00
|
|
|
#include "common-hal/microcontroller/Pin.h"
|
2016-12-09 22:27:41 -05:00
|
|
|
#include "rgb_led_status.h"
|
|
|
|
|
|
|
|
#ifdef MICROPY_HW_NEOPIXEL
|
2018-02-12 21:34:59 -05:00
|
|
|
uint8_t rgb_status_brightness = 63;
|
2017-09-22 21:05:51 -04:00
|
|
|
#include "shared-bindings/digitalio/DigitalInOut.h"
|
|
|
|
#include "shared-bindings/neopixel_write/__init__.h"
|
2016-12-09 22:27:41 -05:00
|
|
|
static uint8_t status_neopixel_color[3];
|
2017-04-10 16:32:19 -04:00
|
|
|
static digitalio_digitalinout_obj_t status_neopixel;
|
2016-12-09 22:27:41 -05:00
|
|
|
#endif
|
|
|
|
|
2017-04-12 18:24:50 -04:00
|
|
|
|
2016-12-09 22:27:41 -05:00
|
|
|
#if defined(MICROPY_HW_APA102_MOSI) && defined(MICROPY_HW_APA102_SCK)
|
2018-02-12 21:34:59 -05:00
|
|
|
uint8_t rgb_status_brightness = 255;
|
2016-12-09 22:27:41 -05:00
|
|
|
static uint8_t status_apa102_color[12] = {0, 0, 0, 0, 0xff, 0, 0, 0};
|
2017-04-12 18:24:50 -04:00
|
|
|
#ifdef CIRCUITPY_BITBANG_APA102
|
2017-09-22 21:05:51 -04:00
|
|
|
#include "shared-bindings/bitbangio/SPI.h"
|
|
|
|
#include "shared-module/bitbangio/types.h"
|
2016-12-09 22:27:41 -05:00
|
|
|
static bitbangio_spi_obj_t status_apa102;
|
2017-04-12 18:24:50 -04:00
|
|
|
#else
|
2017-09-22 21:05:51 -04:00
|
|
|
#include "shared-bindings/busio/SPI.h"
|
2017-04-12 18:24:50 -04:00
|
|
|
busio_spi_obj_t status_apa102;
|
|
|
|
#endif
|
2016-12-09 22:27:41 -05:00
|
|
|
#endif
|
|
|
|
|
2019-06-04 22:20:43 -04:00
|
|
|
#if defined(CP_RGB_STATUS_R) || defined(CP_RGB_STATUS_G) || defined(CP_RGB_STATUS_B)
|
|
|
|
#define CP_RGB_STATUS_LED
|
|
|
|
|
|
|
|
#include "shared-bindings/pulseio/PWMOut.h"
|
|
|
|
#include "shared-bindings/microcontroller/Pin.h"
|
|
|
|
|
|
|
|
pulseio_pwmout_obj_t rgb_status_r;
|
|
|
|
pulseio_pwmout_obj_t rgb_status_g;
|
|
|
|
pulseio_pwmout_obj_t rgb_status_b;
|
|
|
|
|
|
|
|
uint8_t rgb_status_brightness = 0xFF;
|
|
|
|
|
|
|
|
uint16_t status_rgb_color[3] = {
|
|
|
|
0 /* red */, 0 /* green */, 0 /* blue */
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(MICROPY_HW_NEOPIXEL) || (defined(MICROPY_HW_APA102_MOSI) && defined(MICROPY_HW_APA102_SCK)) || (defined(CP_RGB_STATUS_LED))
|
2016-12-09 22:27:41 -05:00
|
|
|
static uint32_t current_status_color = 0;
|
|
|
|
#endif
|
|
|
|
|
2019-06-04 22:20:43 -04:00
|
|
|
|
2016-12-09 22:27:41 -05:00
|
|
|
void rgb_led_status_init() {
|
|
|
|
#ifdef MICROPY_HW_NEOPIXEL
|
2017-04-10 16:32:19 -04:00
|
|
|
common_hal_digitalio_digitalinout_construct(&status_neopixel, MICROPY_HW_NEOPIXEL);
|
2017-04-12 18:24:50 -04:00
|
|
|
// Pretend we aren't using the pins. digitalio.DigitalInOut
|
|
|
|
// will mark them as used.
|
|
|
|
neopixel_in_use = false;
|
2017-04-10 16:32:19 -04:00
|
|
|
common_hal_digitalio_digitalinout_switch_to_output(&status_neopixel, false, DRIVE_MODE_PUSH_PULL);
|
2016-12-09 22:27:41 -05:00
|
|
|
#endif
|
|
|
|
#if defined(MICROPY_HW_APA102_MOSI) && defined(MICROPY_HW_APA102_SCK)
|
2017-04-12 18:24:50 -04:00
|
|
|
#ifdef CIRCUITPY_BITBANG_APA102
|
2016-12-09 22:27:41 -05:00
|
|
|
shared_module_bitbangio_spi_construct(&status_apa102,
|
|
|
|
MICROPY_HW_APA102_SCK,
|
|
|
|
MICROPY_HW_APA102_MOSI,
|
2017-04-12 18:24:50 -04:00
|
|
|
mp_const_none);
|
|
|
|
#else
|
2018-03-08 23:39:47 -05:00
|
|
|
if (!common_hal_busio_spi_deinited(&status_apa102)) {
|
2017-04-12 18:24:50 -04:00
|
|
|
// Don't use spi_deinit because that leads to infinite
|
2018-08-31 17:46:03 -04:00
|
|
|
// recursion because reset_pin_number may call
|
2017-04-12 18:24:50 -04:00
|
|
|
// rgb_led_status_init.
|
2018-03-08 23:39:47 -05:00
|
|
|
spi_m_sync_disable(&status_apa102.spi_desc);
|
2017-04-12 18:24:50 -04:00
|
|
|
}
|
|
|
|
common_hal_busio_spi_construct(&status_apa102,
|
|
|
|
MICROPY_HW_APA102_SCK,
|
|
|
|
MICROPY_HW_APA102_MOSI,
|
|
|
|
mp_const_none);
|
|
|
|
#endif
|
|
|
|
// Pretend we aren't using the pins. bitbangio.SPI will
|
|
|
|
// mark them as used.
|
|
|
|
apa102_mosi_in_use = false;
|
|
|
|
apa102_sck_in_use = false;
|
|
|
|
#ifdef CIRCUITPY_BITBANG_APA102
|
2016-12-09 22:27:41 -05:00
|
|
|
shared_module_bitbangio_spi_try_lock(&status_apa102);
|
|
|
|
shared_module_bitbangio_spi_configure(&status_apa102, 100000, 0, 1, 8);
|
2017-04-12 18:24:50 -04:00
|
|
|
#else
|
|
|
|
common_hal_busio_spi_try_lock(&status_apa102);
|
|
|
|
common_hal_busio_spi_configure(&status_apa102, 100000, 0, 1, 8);
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2019-06-04 22:20:43 -04:00
|
|
|
|
|
|
|
#if defined(CP_RGB_STATUS_LED)
|
|
|
|
if (common_hal_mcu_pin_is_free(CP_RGB_STATUS_R)) {
|
|
|
|
pwmout_result_t red_result = common_hal_pulseio_pwmout_construct(&rgb_status_r, CP_RGB_STATUS_R, 0, 50000, false);
|
|
|
|
|
|
|
|
if (PWMOUT_OK == red_result) {
|
|
|
|
common_hal_pulseio_pwmout_never_reset(&rgb_status_r);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (common_hal_mcu_pin_is_free(CP_RGB_STATUS_G)) {
|
|
|
|
pwmout_result_t green_result = common_hal_pulseio_pwmout_construct(&rgb_status_g, CP_RGB_STATUS_G, 0, 50000, false);
|
|
|
|
|
|
|
|
if (PWMOUT_OK == green_result) {
|
|
|
|
common_hal_pulseio_pwmout_never_reset(&rgb_status_g);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (common_hal_mcu_pin_is_free(CP_RGB_STATUS_B)) {
|
|
|
|
pwmout_result_t blue_result = common_hal_pulseio_pwmout_construct(&rgb_status_b, CP_RGB_STATUS_B, 0, 50000, false);
|
|
|
|
|
|
|
|
if (PWMOUT_OK == blue_result) {
|
|
|
|
common_hal_pulseio_pwmout_never_reset(&rgb_status_b);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(MICROPY_HW_NEOPIXEL) || (defined(MICROPY_HW_APA102_MOSI) && defined(MICROPY_HW_APA102_SCK)) || (defined(CP_RGB_STATUS_LED))
|
2017-04-12 18:24:50 -04:00
|
|
|
// Force a write of the current status color.
|
|
|
|
uint32_t rgb = current_status_color;
|
2018-04-12 16:33:58 -04:00
|
|
|
current_status_color = 0x1000000; // Not a valid color
|
2017-04-12 18:24:50 -04:00
|
|
|
new_status_color(rgb);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void reset_status_led() {
|
|
|
|
#ifdef MICROPY_HW_NEOPIXEL
|
2018-08-31 17:46:03 -04:00
|
|
|
reset_pin_number(MICROPY_HW_NEOPIXEL->number);
|
2017-04-12 18:24:50 -04:00
|
|
|
#endif
|
|
|
|
#if defined(MICROPY_HW_APA102_MOSI) && defined(MICROPY_HW_APA102_SCK)
|
2018-08-31 17:46:03 -04:00
|
|
|
reset_pin_number(MICROPY_HW_APA102_MOSI->number);
|
|
|
|
reset_pin_number(MICROPY_HW_APA102_SCK->number);
|
2016-12-09 22:27:41 -05:00
|
|
|
#endif
|
2019-06-04 22:20:43 -04:00
|
|
|
#if defined(CP_RGB_STATUS_LED)
|
2019-07-06 15:48:16 -04:00
|
|
|
// TODO: Support sharing status LED with user.
|
2019-06-04 22:20:43 -04:00
|
|
|
#endif
|
2016-12-09 22:27:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void new_status_color(uint32_t rgb) {
|
2019-06-04 22:20:43 -04:00
|
|
|
#if defined(MICROPY_HW_NEOPIXEL) || (defined(MICROPY_HW_APA102_MOSI) && defined(MICROPY_HW_APA102_SCK)) || (defined(CP_RGB_STATUS_LED))
|
2016-12-09 22:27:41 -05:00
|
|
|
if (current_status_color == rgb) {
|
|
|
|
return;
|
|
|
|
}
|
2017-09-12 15:09:22 -04:00
|
|
|
uint32_t rgb_adjusted = color_brightness(rgb, rgb_status_brightness);
|
2018-04-12 16:33:58 -04:00
|
|
|
current_status_color = rgb;
|
2016-12-09 22:27:41 -05:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef MICROPY_HW_NEOPIXEL
|
2017-04-12 18:24:50 -04:00
|
|
|
if (neopixel_in_use) {
|
|
|
|
return;
|
|
|
|
}
|
2017-09-12 15:09:22 -04:00
|
|
|
status_neopixel_color[0] = (rgb_adjusted >> 8) & 0xff;
|
|
|
|
status_neopixel_color[1] = (rgb_adjusted >> 16) & 0xff;
|
|
|
|
status_neopixel_color[2] = rgb_adjusted & 0xff;
|
2017-02-19 11:39:34 -05:00
|
|
|
common_hal_neopixel_write(&status_neopixel, status_neopixel_color, 3);
|
2016-12-09 22:27:41 -05:00
|
|
|
#endif
|
|
|
|
#if defined(MICROPY_HW_APA102_MOSI) && defined(MICROPY_HW_APA102_SCK)
|
2017-04-12 18:24:50 -04:00
|
|
|
if (apa102_mosi_in_use || apa102_sck_in_use) {
|
|
|
|
return;
|
|
|
|
}
|
2017-09-12 15:09:22 -04:00
|
|
|
status_apa102_color[5] = rgb_adjusted & 0xff;
|
|
|
|
status_apa102_color[6] = (rgb_adjusted >> 8) & 0xff;
|
|
|
|
status_apa102_color[7] = (rgb_adjusted >> 16) & 0xff;
|
2017-04-12 18:24:50 -04:00
|
|
|
|
|
|
|
#ifdef CIRCUITPY_BITBANG_APA102
|
2016-12-09 22:27:41 -05:00
|
|
|
shared_module_bitbangio_spi_write(&status_apa102, status_apa102_color, 8);
|
2017-04-12 18:24:50 -04:00
|
|
|
#else
|
|
|
|
common_hal_busio_spi_write(&status_apa102, status_apa102_color, 8);
|
|
|
|
#endif
|
2016-12-09 22:27:41 -05:00
|
|
|
#endif
|
2019-06-04 22:20:43 -04:00
|
|
|
|
|
|
|
#if defined(CP_RGB_STATUS_LED)
|
|
|
|
uint8_t red_u8 = (rgb_adjusted >> 16) & 0xFF;
|
|
|
|
uint8_t green_u8 = (rgb_adjusted >> 8) & 0xFF;
|
|
|
|
uint8_t blue_u8 = rgb_adjusted & 0xFF;
|
2019-07-06 17:50:48 -04:00
|
|
|
|
|
|
|
#if defined(CP_RGB_STATUS_INVERTED_PWM)
|
|
|
|
status_rgb_color[0] = (1 << 16) - 1 - ((uint16_t) (red_u8 << 8) + red_u8);
|
|
|
|
status_rgb_color[1] = (1 << 16) - 1 - ((uint16_t) (green_u8 << 8) + green_u8);
|
|
|
|
status_rgb_color[2] = (1 << 16) - 1 - ((uint16_t) (blue_u8 << 8) + blue_u8);
|
|
|
|
#else
|
|
|
|
status_rgb_color[0] = (uint16_t) (red_u8 << 8) + red_u8;
|
|
|
|
status_rgb_color[1] = (uint16_t) (green_u8 << 8) + green_u8;
|
|
|
|
status_rgb_color[2] = (uint16_t) (blue_u8 << 8) + blue_u8;
|
|
|
|
#endif
|
|
|
|
|
2019-06-04 22:20:43 -04:00
|
|
|
common_hal_pulseio_pwmout_set_duty_cycle(&rgb_status_r, status_rgb_color[0]);
|
|
|
|
common_hal_pulseio_pwmout_set_duty_cycle(&rgb_status_g, status_rgb_color[1]);
|
|
|
|
common_hal_pulseio_pwmout_set_duty_cycle(&rgb_status_b, status_rgb_color[2]);
|
|
|
|
#endif
|
2016-12-09 22:27:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void temp_status_color(uint32_t rgb) {
|
2019-06-04 22:20:43 -04:00
|
|
|
#if defined(MICROPY_HW_NEOPIXEL) || (defined(MICROPY_HW_APA102_MOSI) && defined(MICROPY_HW_APA102_SCK)) || (defined(CP_RGB_STATUS_LED))
|
2017-09-12 15:09:22 -04:00
|
|
|
uint32_t rgb_adjusted = rgb;
|
|
|
|
rgb_adjusted = color_brightness(rgb, rgb_status_brightness);
|
|
|
|
#endif
|
2016-12-09 22:27:41 -05:00
|
|
|
#ifdef MICROPY_HW_NEOPIXEL
|
2017-04-12 18:24:50 -04:00
|
|
|
if (neopixel_in_use) {
|
|
|
|
return;
|
|
|
|
}
|
2017-09-12 15:09:22 -04:00
|
|
|
uint8_t colors[3] = {(rgb_adjusted >> 8) & 0xff, (rgb_adjusted >> 16) & 0xff, rgb_adjusted & 0xff};
|
2017-02-19 11:39:34 -05:00
|
|
|
common_hal_neopixel_write(&status_neopixel, colors, 3);
|
2016-12-09 22:27:41 -05:00
|
|
|
#endif
|
|
|
|
#if defined(MICROPY_HW_APA102_MOSI) && defined(MICROPY_HW_APA102_SCK)
|
2017-04-12 18:24:50 -04:00
|
|
|
if (apa102_mosi_in_use || apa102_sck_in_use) {
|
|
|
|
return;
|
|
|
|
}
|
2017-09-12 15:09:22 -04:00
|
|
|
uint8_t colors[12] = {0, 0, 0, 0, 0xff, rgb_adjusted & 0xff, (rgb_adjusted >> 8) & 0xff, (rgb_adjusted >> 16) & 0xff, 0x0, 0x0, 0x0, 0x0};
|
2017-04-12 18:24:50 -04:00
|
|
|
#ifdef CIRCUITPY_BITBANG_APA102
|
|
|
|
shared_module_bitbangio_spi_write(&status_apa102, colors, 12);
|
|
|
|
#else
|
|
|
|
common_hal_busio_spi_write(&status_apa102, colors, 12);
|
|
|
|
#endif
|
2016-12-09 22:27:41 -05:00
|
|
|
#endif
|
2019-06-04 22:20:43 -04:00
|
|
|
#if defined(CP_RGB_STATUS_LED)
|
|
|
|
uint8_t red_u8 = (rgb_adjusted >> 16) & 0xFF;
|
|
|
|
uint8_t green_u8 = (rgb_adjusted >> 8) & 0xFF;
|
|
|
|
uint8_t blue_u8 = rgb_adjusted & 0xFF;
|
|
|
|
|
2019-07-06 17:50:48 -04:00
|
|
|
uint16_t temp_status_color_rgb[3] = {0};
|
|
|
|
|
|
|
|
#if defined(CP_RGB_STATUS_INVERTED_PWM)
|
|
|
|
temp_status_color_rgb[0] = (1 << 16) - 1 - ((uint16_t) (red_u8 << 8) + red_u8);
|
|
|
|
temp_status_color_rgb[1] = (1 << 16) - 1 - ((uint16_t) (green_u8 << 8) + green_u8);
|
|
|
|
temp_status_color_rgb[2] = (1 << 16) - 1 - ((uint16_t) (blue_u8 << 8) + blue_u8);
|
|
|
|
#else
|
2019-07-06 15:48:16 -04:00
|
|
|
temp_status_color_rgb[0] = (uint16_t) (red_u8 << 8) + red_u8;
|
|
|
|
temp_status_color_rgb[1] = (uint16_t) (green_u8 << 8) + green_u8;
|
|
|
|
temp_status_color_rgb[2] = (uint16_t) (blue_u8 << 8) + blue_u8;
|
2019-07-06 17:50:48 -04:00
|
|
|
#endif
|
|
|
|
|
2019-07-06 15:48:16 -04:00
|
|
|
common_hal_pulseio_pwmout_set_duty_cycle(&rgb_status_r, temp_status_color_rgb[0]);
|
|
|
|
common_hal_pulseio_pwmout_set_duty_cycle(&rgb_status_g, temp_status_color_rgb[1]);
|
|
|
|
common_hal_pulseio_pwmout_set_duty_cycle(&rgb_status_b, temp_status_color_rgb[2]);
|
2019-06-04 22:20:43 -04:00
|
|
|
#endif
|
2016-12-09 22:27:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void clear_temp_status() {
|
|
|
|
#ifdef MICROPY_HW_NEOPIXEL
|
2017-02-19 11:39:34 -05:00
|
|
|
common_hal_neopixel_write(&status_neopixel, status_neopixel_color, 3);
|
2016-12-09 22:27:41 -05:00
|
|
|
#endif
|
|
|
|
#if defined(MICROPY_HW_APA102_MOSI) && defined(MICROPY_HW_APA102_SCK)
|
2017-04-12 18:24:50 -04:00
|
|
|
#ifdef CIRCUITPY_BITBANG_APA102
|
|
|
|
shared_module_bitbangio_spi_write(&status_apa102, status_apa102_color, 8);
|
|
|
|
#else
|
|
|
|
common_hal_busio_spi_write(&status_apa102, status_apa102_color, 8);
|
|
|
|
#endif
|
2016-12-09 22:27:41 -05:00
|
|
|
#endif
|
2019-06-04 22:20:43 -04:00
|
|
|
#if defined(CP_RGB_STATUS_LED)
|
2019-07-07 22:15:51 -04:00
|
|
|
|
|
|
|
uint16_t red = 0;
|
|
|
|
uint16_t green = 0;
|
|
|
|
uint16_t blue = 0;
|
|
|
|
|
|
|
|
#if defined(CP_RGB_STATUS_INVERTED_PWM)
|
|
|
|
red = (1 << 16) - 1 - status_rgb_color[0];
|
|
|
|
green = (1 << 16) - 1 - status_rgb_color[1];
|
|
|
|
blue = (1 << 16) - 1 - status_rgb_color[2];
|
|
|
|
#else
|
|
|
|
red = status_rgb_color[0];
|
|
|
|
green = status_rgb_color[1];
|
|
|
|
blue = status_rgb_color[2];
|
|
|
|
#endif
|
|
|
|
|
|
|
|
common_hal_pulseio_pwmout_set_duty_cycle(&rgb_status_r, red);
|
|
|
|
common_hal_pulseio_pwmout_set_duty_cycle(&rgb_status_g, green);
|
|
|
|
common_hal_pulseio_pwmout_set_duty_cycle(&rgb_status_b, blue);
|
2019-06-04 22:20:43 -04:00
|
|
|
#endif
|
2016-12-09 22:27:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t color_brightness(uint32_t color, uint8_t brightness) {
|
2019-07-08 23:06:43 -04:00
|
|
|
#if defined(MICROPY_HW_NEOPIXEL) || (defined(MICROPY_HW_APA102_MOSI) && defined(MICROPY_HW_APA102_SCK)) || (defined(CP_RGB_STATUS_LED))
|
2016-12-09 22:27:41 -05:00
|
|
|
uint32_t result = ((color & 0xff0000) * brightness / 255) & 0xff0000;
|
|
|
|
result += ((color & 0xff00) * brightness / 255) & 0xff00;
|
|
|
|
result += ((color & 0xff) * brightness / 255) & 0xff;
|
|
|
|
return result;
|
|
|
|
#else
|
|
|
|
return color;
|
|
|
|
#endif
|
|
|
|
}
|
2017-09-12 15:09:22 -04:00
|
|
|
|
|
|
|
void set_rgb_status_brightness(uint8_t level){
|
2019-07-08 23:06:43 -04:00
|
|
|
#if defined(MICROPY_HW_NEOPIXEL) || (defined(MICROPY_HW_APA102_MOSI) && defined(MICROPY_HW_APA102_SCK)) || (defined(CP_RGB_STATUS_LED))
|
2018-02-13 12:39:19 -05:00
|
|
|
rgb_status_brightness = level;
|
2019-03-26 19:43:35 -04:00
|
|
|
uint32_t current_color = current_status_color;
|
|
|
|
// Temporarily change the current color global to force the new_status_color call to update the
|
|
|
|
// LED. Usually duplicate calls of the same color are ignored without regard to brightness
|
|
|
|
// changes.
|
|
|
|
current_status_color = 0;
|
|
|
|
new_status_color(current_color);
|
2018-02-13 12:39:19 -05:00
|
|
|
#endif
|
2017-09-12 15:09:22 -04:00
|
|
|
}
|
2017-09-22 21:05:51 -04:00
|
|
|
|
2017-11-01 01:23:45 -04:00
|
|
|
void prep_rgb_status_animation(const pyexec_result_t* result,
|
|
|
|
bool found_main,
|
|
|
|
safe_mode_t safe_mode,
|
|
|
|
rgb_status_animation_t* status) {
|
2019-06-04 22:20:43 -04:00
|
|
|
#if defined(MICROPY_HW_NEOPIXEL) || (defined(MICROPY_HW_APA102_MOSI) && defined(MICROPY_HW_APA102_SCK)) || (defined(CP_RGB_STATUS_LED))
|
2017-09-22 21:05:51 -04:00
|
|
|
new_status_color(ALL_DONE);
|
|
|
|
status->pattern_start = ticks_ms;
|
2017-11-01 01:23:45 -04:00
|
|
|
status->safe_mode = safe_mode;
|
2017-11-22 21:56:28 -05:00
|
|
|
status->found_main = found_main;
|
2017-11-01 01:23:45 -04:00
|
|
|
status->total_exception_cycle = 0;
|
2017-09-22 21:05:51 -04:00
|
|
|
status->ones = result->exception_line % 10;
|
|
|
|
status->ones += status->ones > 0 ? 1 : 0;
|
|
|
|
status->tens = (result->exception_line / 10) % 10;
|
|
|
|
status->tens += status->tens > 0 ? 1 : 0;
|
|
|
|
status->hundreds = (result->exception_line / 100) % 10;
|
|
|
|
status->hundreds += status->hundreds > 0 ? 1 : 0;
|
|
|
|
status->thousands = (result->exception_line / 1000) % 10;
|
|
|
|
status->thousands += status->thousands > 0 ? 1 : 0;
|
|
|
|
status->digit_sum = status->ones + status->tens + status->hundreds + status->thousands;
|
|
|
|
uint8_t num_places = 0;
|
|
|
|
uint16_t line = result->exception_line;
|
|
|
|
for (int i = 0; i < 4; i++) {
|
|
|
|
if ((line % 10) > 0) {
|
|
|
|
num_places++;
|
|
|
|
}
|
|
|
|
line /= 10;
|
|
|
|
}
|
|
|
|
status->ok = result->return_code != PYEXEC_EXCEPTION;
|
|
|
|
if (!status->ok) {
|
2017-11-01 01:23:45 -04:00
|
|
|
status->total_exception_cycle = EXCEPTION_TYPE_LENGTH_MS * 3 + LINE_NUMBER_TOGGLE_LENGTH * status->digit_sum + LINE_NUMBER_TOGGLE_LENGTH * num_places;
|
2017-09-22 21:05:51 -04:00
|
|
|
}
|
|
|
|
if (mp_obj_is_subclass_fast(result->exception_type, &mp_type_IndentationError)) {
|
|
|
|
status->exception_color = INDENTATION_ERROR;
|
|
|
|
} else if (mp_obj_is_subclass_fast(result->exception_type, &mp_type_SyntaxError)) {
|
|
|
|
status->exception_color = SYNTAX_ERROR;
|
|
|
|
} else if (mp_obj_is_subclass_fast(result->exception_type, &mp_type_NameError)) {
|
|
|
|
status->exception_color = NAME_ERROR;
|
|
|
|
} else if (mp_obj_is_subclass_fast(result->exception_type, &mp_type_OSError)) {
|
|
|
|
status->exception_color = OS_ERROR;
|
|
|
|
} else if (mp_obj_is_subclass_fast(result->exception_type, &mp_type_ValueError)) {
|
|
|
|
status->exception_color = VALUE_ERROR;
|
2019-05-08 14:54:08 -04:00
|
|
|
} else if (mp_obj_is_subclass_fast(result->exception_type, &mp_type_MpyError)) {
|
|
|
|
status->exception_color = MPY_ERROR;
|
2017-09-22 21:05:51 -04:00
|
|
|
} else {
|
|
|
|
status->exception_color = OTHER_ERROR;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void tick_rgb_status_animation(rgb_status_animation_t* status) {
|
2019-06-04 22:20:43 -04:00
|
|
|
#if defined(MICROPY_HW_NEOPIXEL) || (defined(MICROPY_HW_APA102_MOSI) && defined(MICROPY_HW_APA102_SCK)) || (defined(CP_RGB_STATUS_LED))
|
2017-11-01 01:23:45 -04:00
|
|
|
uint32_t tick_diff = ticks_ms - status->pattern_start;
|
2017-09-22 21:05:51 -04:00
|
|
|
if (status->ok) {
|
|
|
|
// All is good. Ramp ALL_DONE up and down.
|
|
|
|
if (tick_diff > ALL_GOOD_CYCLE_MS) {
|
2017-11-01 01:23:45 -04:00
|
|
|
status->pattern_start = ticks_ms;
|
2017-09-22 21:05:51 -04:00
|
|
|
tick_diff = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t brightness = tick_diff * 255 / (ALL_GOOD_CYCLE_MS / 2);
|
|
|
|
if (brightness > 255) {
|
|
|
|
brightness = 511 - brightness;
|
|
|
|
}
|
2017-11-01 01:23:45 -04:00
|
|
|
if (status->safe_mode == NO_SAFE_MODE) {
|
2017-09-22 21:05:51 -04:00
|
|
|
new_status_color(color_brightness(ALL_DONE, brightness));
|
|
|
|
} else {
|
|
|
|
new_status_color(color_brightness(SAFE_MODE, brightness));
|
|
|
|
}
|
|
|
|
} else {
|
2017-11-01 01:23:45 -04:00
|
|
|
if (tick_diff > status->total_exception_cycle) {
|
|
|
|
status->pattern_start = ticks_ms;
|
2017-09-22 21:05:51 -04:00
|
|
|
tick_diff = 0;
|
|
|
|
}
|
|
|
|
// First flash the file color.
|
|
|
|
if (tick_diff < EXCEPTION_TYPE_LENGTH_MS) {
|
|
|
|
if (status->found_main) {
|
|
|
|
new_status_color(MAIN_RUNNING);
|
|
|
|
} else {
|
|
|
|
new_status_color(BOOT_RUNNING);
|
|
|
|
}
|
|
|
|
// Next flash the exception color.
|
|
|
|
} else if (tick_diff < EXCEPTION_TYPE_LENGTH_MS * 2) {
|
2017-11-01 01:23:45 -04:00
|
|
|
new_status_color(status->exception_color);
|
2017-09-22 21:05:51 -04:00
|
|
|
// Finally flash the line number digits from highest to lowest.
|
|
|
|
// Zeroes will not produce a flash but can be read by the absence of
|
|
|
|
// a color from the sequence.
|
2017-11-01 01:23:45 -04:00
|
|
|
} else if (tick_diff < (EXCEPTION_TYPE_LENGTH_MS * 2 + LINE_NUMBER_TOGGLE_LENGTH * status->digit_sum)) {
|
2017-09-22 21:05:51 -04:00
|
|
|
uint32_t digit_diff = tick_diff - EXCEPTION_TYPE_LENGTH_MS * 2;
|
|
|
|
if ((digit_diff % LINE_NUMBER_TOGGLE_LENGTH) < (LINE_NUMBER_TOGGLE_LENGTH / 2)) {
|
|
|
|
new_status_color(BLACK);
|
2017-11-01 01:23:45 -04:00
|
|
|
} else if (digit_diff < LINE_NUMBER_TOGGLE_LENGTH * status->thousands) {
|
2017-09-22 21:05:51 -04:00
|
|
|
if (digit_diff < LINE_NUMBER_TOGGLE_LENGTH) {
|
|
|
|
new_status_color(BLACK);
|
|
|
|
} else {
|
|
|
|
new_status_color(THOUSANDS);
|
|
|
|
}
|
2017-11-01 01:23:45 -04:00
|
|
|
} else if (digit_diff < LINE_NUMBER_TOGGLE_LENGTH * (status->thousands + status->hundreds)) {
|
|
|
|
if (digit_diff < LINE_NUMBER_TOGGLE_LENGTH * (status->thousands + 1)) {
|
2017-09-22 21:05:51 -04:00
|
|
|
new_status_color(BLACK);
|
|
|
|
} else {
|
|
|
|
new_status_color(HUNDREDS);
|
|
|
|
}
|
2017-11-01 01:23:45 -04:00
|
|
|
} else if (digit_diff < LINE_NUMBER_TOGGLE_LENGTH * (status->thousands + status->hundreds + status->tens)) {
|
|
|
|
if (digit_diff < LINE_NUMBER_TOGGLE_LENGTH * (status->thousands + status->hundreds + 1)) {
|
2017-09-22 21:05:51 -04:00
|
|
|
new_status_color(BLACK);
|
|
|
|
} else {
|
|
|
|
new_status_color(TENS);
|
|
|
|
}
|
|
|
|
} else {
|
2017-11-01 01:23:45 -04:00
|
|
|
if (digit_diff < LINE_NUMBER_TOGGLE_LENGTH * (status->thousands + status->hundreds + status->tens + 1)) {
|
2017-09-22 21:05:51 -04:00
|
|
|
new_status_color(BLACK);
|
|
|
|
} else {
|
|
|
|
new_status_color(ONES);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
new_status_color(BLACK);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2019-07-06 17:50:48 -04:00
|
|
|
}
|