add set_rgb_status_brightness (#246)

Add set_rgb_status_brightness() via `samd.rgb_status_brightness`.

Fixes #162.
This commit is contained in:
Asher Lieber 2017-09-12 15:09:22 -04:00 committed by Scott Shawcroft
parent dd72fe6945
commit 5aa8922038
4 changed files with 39 additions and 10 deletions

View File

@ -137,7 +137,7 @@ CFLAGS += -Os -ggdb -DNDEBUG -DENABLE_MICRO_TRACE_BUFFER -DMICROPY_DEBUG_MODULES
else
# -finline-limit can shrink the image size. -finline-limit=80 or so is similar to not having it on.
# There is no simple default value, though.
CFLAGS += -Os -DNDEBUG -flto -finline-limit=57
CFLAGS += -Os -DNDEBUG -flto -finline-limit=49
endif
ifneq ($(FROZEN_DIR),)

View File

@ -26,6 +26,7 @@
#include "py/obj.h"
#include "py/runtime.h"
#include "autoreload.h"
#include "rgb_led_status.h"
//| :mod:`samd` --- SAMD implementation settings
//| =================================================
@ -56,10 +57,27 @@ STATIC mp_obj_t samd_disable_autoreload(void) {
}
MP_DEFINE_CONST_FUN_OBJ_0(samd_disable_autoreload_obj, samd_disable_autoreload);
//| .. method:: set_rgb_status_brightness()
//|
//| Set brightness of status neopixel from 0-255
//| `set_rgb_status_brightness` is called.
//|
STATIC mp_obj_t samd_set_rgb_status_brightness(mp_obj_t lvl){
// This must be int. If cast to uint8_t first, will never raise a ValueError.
int brightness_int = mp_obj_get_int(lvl);
if(brightness_int < 0 || brightness_int > 255){
mp_raise_ValueError("Brightness must be between 0 and 255");
}
set_rgb_status_brightness((uint8_t)brightness_int);
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_1(samd_set_rgb_status_brightness_obj, samd_set_rgb_status_brightness);
STATIC const mp_rom_map_elem_t samd_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_samd) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_enable_autoreload), MP_ROM_PTR(&samd_enable_autoreload_obj)},
{ MP_OBJ_NEW_QSTR(MP_QSTR_disable_autoreload), MP_ROM_PTR(&samd_disable_autoreload_obj)},
{ MP_OBJ_NEW_QSTR(MP_QSTR_set_rgb_status_brightness), MP_ROM_PTR(&samd_set_rgb_status_brightness_obj)},
};
STATIC MP_DEFINE_CONST_DICT(samd_module_globals, samd_module_globals_table);

View File

@ -12,6 +12,7 @@
#include "rgb_led_status.h"
#include "samd21_pins.h"
uint8_t rgb_status_brightness = 255;
#ifdef MICROPY_HW_NEOPIXEL
static uint8_t status_neopixel_color[3];
static digitalio_digitalinout_obj_t status_neopixel;
@ -94,25 +95,26 @@ void new_status_color(uint32_t rgb) {
if (current_status_color == rgb) {
return;
}
current_status_color = rgb;
uint32_t rgb_adjusted = color_brightness(rgb, rgb_status_brightness);
current_status_color = rgb_adjusted;
#endif
#ifdef MICROPY_HW_NEOPIXEL
if (neopixel_in_use) {
return;
}
status_neopixel_color[0] = (rgb >> 8) & 0xff;
status_neopixel_color[1] = (rgb >> 16) & 0xff;
status_neopixel_color[2] = rgb & 0xff;
status_neopixel_color[0] = (rgb_adjusted >> 8) & 0xff;
status_neopixel_color[1] = (rgb_adjusted >> 16) & 0xff;
status_neopixel_color[2] = rgb_adjusted & 0xff;
common_hal_neopixel_write(&status_neopixel, status_neopixel_color, 3);
#endif
#if defined(MICROPY_HW_APA102_MOSI) && defined(MICROPY_HW_APA102_SCK)
if (apa102_mosi_in_use || apa102_sck_in_use) {
return;
}
status_apa102_color[5] = rgb & 0xff;
status_apa102_color[6] = (rgb >> 8) & 0xff;
status_apa102_color[7] = (rgb >> 16) & 0xff;
status_apa102_color[5] = rgb_adjusted & 0xff;
status_apa102_color[6] = (rgb_adjusted >> 8) & 0xff;
status_apa102_color[7] = (rgb_adjusted >> 16) & 0xff;
#ifdef CIRCUITPY_BITBANG_APA102
shared_module_bitbangio_spi_write(&status_apa102, status_apa102_color, 8);
@ -123,18 +125,22 @@ void new_status_color(uint32_t rgb) {
}
void temp_status_color(uint32_t rgb) {
#if defined(MICROPY_HW_NEOPIXEL) || (defined(MICROPY_HW_APA102_MOSI) && defined(MICROPY_HW_APA102_SCK))
uint32_t rgb_adjusted = rgb;
rgb_adjusted = color_brightness(rgb, rgb_status_brightness);
#endif
#ifdef MICROPY_HW_NEOPIXEL
if (neopixel_in_use) {
return;
}
uint8_t colors[3] = {(rgb >> 8) & 0xff, (rgb >> 16) & 0xff, rgb & 0xff};
uint8_t colors[3] = {(rgb_adjusted >> 8) & 0xff, (rgb_adjusted >> 16) & 0xff, rgb_adjusted & 0xff};
common_hal_neopixel_write(&status_neopixel, colors, 3);
#endif
#if defined(MICROPY_HW_APA102_MOSI) && defined(MICROPY_HW_APA102_SCK)
if (apa102_mosi_in_use || apa102_sck_in_use) {
return;
}
uint8_t colors[12] = {0, 0, 0, 0, 0xff, rgb & 0xff, (rgb >> 8) & 0xff, (rgb >> 16) & 0xff, 0x0, 0x0, 0x0, 0x0};
uint8_t colors[12] = {0, 0, 0, 0, 0xff, rgb_adjusted & 0xff, (rgb_adjusted >> 8) & 0xff, (rgb_adjusted >> 16) & 0xff, 0x0, 0x0, 0x0, 0x0};
#ifdef CIRCUITPY_BITBANG_APA102
shared_module_bitbangio_spi_write(&status_apa102, colors, 12);
#else
@ -166,3 +172,7 @@ uint32_t color_brightness(uint32_t color, uint8_t brightness) {
return color;
#endif
}
void set_rgb_status_brightness(uint8_t level){
rgb_status_brightness = level;
}

View File

@ -46,5 +46,6 @@ void temp_status_color(uint32_t rgb);
void clear_temp_status(void);
uint32_t color_brightness(uint32_t color, uint8_t brightness);
void set_rgb_status_brightness(uint8_t level);
#endif // MICROPY_INCLUDED_ATMEL_SAMD_RGB_LED_STATUS_H