8000 add set_rgb_status_brightness (#246) · sparkfun/circuitpython@5aa8922 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5aa8922

Browse files
asherlietannewt
authored andcommitted
add set_rgb_status_brightness (adafruit#246)
Add set_rgb_status_brightness() via `samd.rgb_status_brightness`. Fixes adafruit#162.
1 parent dd72fe6 commit 5aa8922

File tree

4 files changed

+39
-10
lines changed

4 files changed

+39
-10
lines changed

atmel-samd/Makefile

Lines changed: 1 addition & 1 deletion
< 10000 tr class="diff-line-row">
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ CFLAGS += -Os -ggdb -DNDEBUG -DENABLE_MICRO_TRACE_BUFFER -DMICROPY_DEBUG_MODULES
137137
else
138138
# -finline-limit can shrink the image size. -finline-limit=80 or so is similar to not having it on.
139139
# There is no simple default value, though.
140-
CFLAGS += -Os -DNDEBUG -flto -finline-limit=57
140+
CFLAGS += -Os -DNDEBUG -flto -finline-limit=49
141141
endif
142142

143143
ifneq ($(FROZEN_DIR),)

atmel-samd/bindings/samd/__init__.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "py/obj.h"
2727
#include "py/runtime.h"
2828
#include "autoreload.h"
29+
#include "rgb_led_status.h"
2930

3031
//| :mod:`samd` --- SAMD implementation settings
3132
//| =================================================
@@ -56,10 +57,27 @@ STATIC mp_obj_t samd_disable_autoreload(void) {
5657
}
5758
MP_DEFINE_CONST_FUN_OBJ_0(samd_disable_autoreload_obj, samd_disable_autoreload);
5859

60+
//| .. method:: set_rgb_status_brightness()
61+
//|
62+
//| Set brightness of status neopixel from 0-255
63+
//| `set_rgb_status_brightness` is called.
64+
//|
65+
STATIC mp_obj_t samd_set_rgb_status_brightness(mp_obj_t lvl){
66+
// This must be int. If cast to uint8_t first, will never raise a ValueError.
67+
int brightness_int = mp_obj_get_int(lvl);
68+
if(brightness_int < 0 || brightness_int > 255){
69+
mp_raise_ValueError("Brightness must be between 0 and 255");
70+
}
71+
set_rgb_status_brightness((uint8_t)brightness_int);
72+
return mp_const_none;
73+
}
74+
MP_DEFINE_CONST_FUN_OBJ_1(samd_set_rgb_status_brightness_obj, samd_set_rgb_status_brightness);
75+
5976
STATIC const mp_rom_map_elem_t samd_module_globals_table[] = {
6077
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_samd) },
6178
{ MP_OBJ_NEW_QSTR(MP_QSTR_enable_autoreload), MP_ROM_PTR(&samd_enable_autoreload_obj)},
6279
{ MP_OBJ_NEW_QSTR(MP_QSTR_disable_autoreload), MP_ROM_PTR(&samd_disable_autoreload_obj)},
80+
{ MP_OBJ_NEW_QSTR(MP_QSTR_set_rgb_status_brightness), MP_ROM_PTR(&samd_set_rgb_status_brightness_obj)},
6381
};
6482

6583
STATIC MP_DEFINE_CONST_DICT(samd_module_globals, samd_module_globals_table);

atmel-samd/rgb_led_status.c

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "rgb_led_status.h"
1313
#include "samd21_pins.h"
1414

15+
uint8_t rgb_status_brightness = 255;
1516
#ifdef MICROPY_HW_NEOPIXEL
1617
static uint8_t status_neopixel_color[3];
1718
static digitalio_digitalinout_obj_t status_neopixel;
@@ -94,25 +95,26 @@ void new_status_color(uint32_t rgb) {
9495
if (current_status_color == rgb) {
9596
return;
9697
}
97-
current_status_color = rgb;
98+
uint32_t rgb_adjusted = color_brightness(rgb, rgb_status_brightness);
99+
current_status_color = rgb_adjusted;
98100
#endif
99101

100102
#ifdef MICROPY_HW_NEOPIXEL
101103
if (neopixel_in_use) {
102104
return;
103105
}
104-
status_neopixel_color[0] = (rgb >> 8) & 0xff;
105-
status_neopixel_color[1] = (rgb >> 16) & 0xff;
106-
status_neopixel_color[2] = rgb & 0xff;
106+
status_neopixel_color[0] = (rgb_adjusted >> 8) & 0xff;
107+
status_neopixel_color[1] = (rgb_adjusted >> 16) & 0xff;
108+
status_neopixel_color[2] = rgb_adjusted & 0xff;
107109
common_hal_neopixel_write(&status_neopixel, status_neopixel_color, 3);
108110
#endif
109111
#if defined(MICROPY_HW_APA102_MOSI) && defined(MICROPY_HW_APA102_SCK)
110112
if (apa102_mosi_in_use || apa102_sck_in_use) {
111113
return;
112114
}
113-
status_apa102_color[5] = rgb & 0xff;
114-
status_apa102_color[6] = (rgb >> 8) & 0xff;
115-
status_apa102_color[7] = (rgb >> 16) & 0xff;
115+
status_apa102_color[5] = rgb_adjusted & 0xff;
116+
status_apa102_color[6] = (rgb_adjusted >> 8) & 0xff;
117+
status_apa102_color[7] = (rgb_adjusted >> 16) & 0xff;
116118

117119
#ifdef CIRCUITPY_BITBANG_APA102
118120
shared_module_bitbangio_spi_write(&status_apa102, status_apa102_color, 8);
@@ -123,18 +125,22 @@ void new_status_color(uint32_t rgb) {
123125
}
124126

125127
void temp_status_color(uint32_t rgb) {
128+
#if defined(MICROPY_HW_NEOPIXEL) || (defined(MICROPY_HW_APA102_MOSI) && defined(MICROPY_HW_APA102_SCK))
129+
uint32_t rgb_adjusted = rgb;
130+
rgb_adjusted = color_brightness(rgb, rgb_status_brightness);
131+
#endif
126132
#ifdef MICROPY_HW_NEOPIXEL
127133
if (neopixel_in_use) {
128134
return;
129135
}
130-
uint8_t colors[3] = {(rgb >> 8) & 0xff, (rgb >> 16) & 0xff, rgb & 0xff};
136+
uint8_t colors[3] = {(rgb_adjusted >> 8) & 0xff, (rgb_adjusted >> 16) & 0xff, rgb_adjusted & 0xff};
131137
common_hal_neopixel_write(&status_neopixel, colors, 3);
132138
#endif
133139
#if defined(MICROPY_HW_APA102_MOSI) && defined(MICROPY_HW_APA102_SCK)
134140
if (apa102_mosi_in_use || apa102_sck_in_use) {
135141
return;
136142
}
137-
uint8_t colors[12] = {0, 0, 0, 0, 0xff, rgb & 0xff, (rgb >> 8) & 0xff, (rgb >> 16) & 0xff, 0x0, 0x0, 0x0, 0x0};
143+
uint8_t colors[12] = {0, 0, 0, 0, 0xff, rgb_adjusted & 0xff, (rgb_adjusted >> 8) & 0xff, (rgb_adjusted >> 16) & 0xff, 0x0, 0x0, 0x0, 0x0};
138144
#ifdef CIRCUITPY_BITBANG_APA102
139145
shared_module_bitbangio_spi_write(&status_apa102, colors, 12);
140146
#else
@@ -166,3 +172,7 @@ uint32_t color_brightness(uint32_t color, uint8_t brightness) {
166172
return color;
167173
#endif
168174
}
175+
176+
void set_rgb_status_brightness(uint8_t level){
177+
rgb_status_brightness = level;
178+
}

atmel-samd/rgb_led_status.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,6 @@ void temp_status_color(uint32_t rgb);
4646
void clear_temp_status(void);
4747

4848
uint32_t color_brightness(uint32_t color, uint8_t brightness);
49+
void set_rgb_status_brightness(uint8_t level);
4950

5051
#endif // MICROPY_INCLUDED_ATMEL_SAMD_RGB_LED_STATUS_H

0 commit comments

Comments
 (0)
0