Skip to content

Commit 68f9aee

Browse files
committed
reset NeoPixels on CPB on soft reload
1 parent 1831f51 commit 68f9aee

File tree

12 files changed

+113
-36
lines changed

12 files changed

+113
-36
lines changed

ports/atmel-samd/boards/circuitplayground_express/board.c

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,8 @@
2828

2929
#include "boards/board.h"
3030
#include "common-hal/microcontroller/Pin.h"
31+
#include "supervisor/shared/board.h"
3132
#include "hal/include/hal_gpio.h"
32-
#include "shared-bindings/digitalio/DigitalInOut.h"
33-
#include "shared-bindings/neopixel_write/__init__.h"
3433

3534
void board_init(void)
3635
{
@@ -54,12 +53,5 @@ bool board_requests_safe_mode(void) {
5453
}
5554

5655
void reset_board(void) {
57-
uint8_t empty[30];
58-
memset(empty, 0, 30);
59-
digitalio_digitalinout_obj_t neopixel_pin;
60-
common_hal_digitalio_digitalinout_construct(&neopixel_pin, &pin_PB23);
61-
common_hal_digitalio_digitalinout_switch_to_output(&neopixel_pin, false,
62-
DRIVE_MODE_PUSH_PULL);
63-
common_hal_neopixel_write(&neopixel_pin, empty, 30);
64-
common_hal_digitalio_digitalinout_deinit(&neopixel_pin);
56+
board_reset_user_neopixels();
6557
}

ports/atmel-samd/boards/circuitplayground_express/mpconfigboard.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
// Increase stack size slightly due to CPX library import nesting
3131
#define CIRCUITPY_DEFAULT_STACK_SIZE (4760) //divisible by 8
3232

33+
#define USER_NEOPIXELS_PIN (&pin_PB23)
34+
3335
#define DEFAULT_I2C_BUS_SCL (&pin_PB03)
3436
#define DEFAULT_I2C_BUS_SDA (&pin_PB02)
3537

ports/atmel-samd/boards/circuitplayground_express_crickit/board.c

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@
2929
#include "boards/board.h"
3030
#include "common-hal/microcontroller/Pin.h"
3131
#include "hal/include/hal_gpio.h"
32-
#include "shared-bindings/digitalio/DigitalInOut.h"
33-
#include "shared-bindings/neopixel_write/__init__.h"
32+
#include "supervisor/shared/board.h"
3433

3534
void board_init(void)
3635
{
@@ -54,12 +53,5 @@ bool board_requests_safe_mode(void) {
5453
}
5554

5655
void reset_board(void) {
57-
uint8_t empty[30];
58-
memset(empty, 0, 30);
59-
digitalio_digitalinout_obj_t neopixel_pin;
60-
common_hal_digitalio_digitalinout_construct(&neopixel_pin, &pin_PB23);
61-
common_hal_digitalio_digitalinout_switch_to_output(&neopixel_pin, false,
62-
DRIVE_MODE_PUSH_PULL);
63-
common_hal_neopixel_write(&neopixel_pin, empty, 30);
64-
common_hal_digitalio_digitalinout_deinit(&neopixel_pin);
56+
board_reset_user_neopixels();
6557
}

ports/atmel-samd/boards/circuitplayground_express_crickit/mpconfigboard.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424

2525
#define CALIBRATE_CRYSTALLESS 1
2626

27+
#define USER_NEOPIXELS_PIN (&pin_PB23)
28+
2729
// Explanation of how a user got into safe mode.
2830
#define BOARD_USER_SAFE_MODE_ACTION "pressing both buttons at start up"
2931

ports/atmel-samd/boards/circuitplayground_express_displayio/board.c

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@
2929
#include "boards/board.h"
3030
#include "common-hal/microcontroller/Pin.h"
3131
#include "hal/include/hal_gpio.h"
32-
#include "shared-bindings/digitalio/DigitalInOut.h"
33-
#include "shared-bindings/neopixel_write/__init__.h"
32+
#include "supervisor/shared/board.h"
3433

3534
void board_init(void)
3635
{
@@ -54,12 +53,5 @@ bool board_requests_safe_mode(void) {
5453
}
5554

5655
void reset_board(void) {
57-
uint8_t empty[30];
58-
memset(empty, 0, 30);
59-
digitalio_digitalinout_obj_t neopixel_pin;
60-
common_hal_digitalio_digitalinout_construct(&neopixel_pin, &pin_PB23);
61-
common_hal_digitalio_digitalinout_switch_to_output(&neopixel_pin, false,
62-
DRIVE_MODE_PUSH_PULL);
63-
common_hal_neopixel_write(&neopixel_pin, empty, 30);
64-
common_hal_digitalio_digitalinout_deinit(&neopixel_pin);
56+
board_reset_user_neopixels();
6557
}

ports/atmel-samd/boards/circuitplayground_express_displayio/mpconfigboard.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
// Increase stack size slightly due to CPX library import nesting.
3131
#define CIRCUITPY_DEFAULT_STACK_SIZE (4760) // divisible by 8
3232

33+
#define USER_NEOPIXELS_PIN (&pin_PB23)
34+
3335
#define DEFAULT_I2C_BUS_SCL (&pin_PB03)
3436
#define DEFAULT_I2C_BUS_SDA (&pin_PB02)
3537

ports/nrf/boards/circuitplayground_bluefruit/board.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "mpconfigboard.h"
2929
#include "py/obj.h"
3030
#include "peripherals/nrf/pins.h"
31+
#include "supervisor/shared/board.h"
3132

3233
#include "nrf_gpio.h"
3334

@@ -47,4 +48,6 @@ void reset_board(void) {
4748
NRF_GPIO_PIN_S0S1,
4849
NRF_GPIO_PIN_NOSENSE);
4950
nrf_gpio_pin_write(POWER_SWITCH_PIN->number, false);
51+
52+
board_reset_user_neopixels();
5053
}

ports/nrf/boards/circuitplayground_bluefruit/mpconfigboard.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@
5454
// Disables onboard peripherals and neopixels to save power.
5555
#define POWER_SWITCH_PIN (&pin_P0_06)
5656

57+
#define USER_NEOPIXELS_PIN (&pin_P0_13)
58+
5759
#define DEFAULT_I2C_BUS_SCL (&pin_P0_04)
5860
#define DEFAULT_I2C_BUS_SDA (&pin_P0_05)
5961

ports/nrf/common-hal/neopixel_write/__init__.c

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ uint32_t next_start_tick_us = 1000;
102102

103103
void common_hal_neopixel_write (const digitalio_digitalinout_obj_t* digitalinout, uint8_t *pixels, uint32_t numBytes) {
104104
// To support both the SoftDevice + Neopixels we use the EasyDMA
105-
// feature from the NRF25. However this technique implies to
105+
// feature from the NRF52. However this technique implies to
106106
// generate a pattern and store it on the memory. The actual
107107
// memory used in bytes corresponds to the following formula:
108108
// totalMem = numBytes*8*2+(2*2)
@@ -113,22 +113,28 @@ void common_hal_neopixel_write (const digitalio_digitalinout_obj_t* digitalinout
113113
// using DWT
114114

115115
#define PATTERN_SIZE(numBytes) (numBytes * 8 * sizeof(uint16_t) + 2 * sizeof(uint16_t))
116+
// Allocate PWM space for up to STACK_PIXELS on the stack, to avoid malloc'ing.
117+
// We may need to write to the status neopixel or to Circuit Playground NeoPixels
118+
// when we cannot malloc, between VM instantiations.
119+
// We need space for at least 10 pixels for Circuit Playground, but let's choose 24
120+
// to handle larger NeoPixel rings without malloc'ing.
121+
#define STACK_PIXELS 24
116122

117123
uint32_t pattern_size = PATTERN_SIZE(numBytes);
118124
uint16_t* pixels_pattern = NULL;
119125
bool pattern_on_heap = false;
120126

121-
// Use the stack to store 1 pixels worth of PWM data for the status led. uint32_t to ensure alignment.
122-
// Make it at least as big as PATTERN_SIZE(3), for one pixel of RGB data.
127+
// Use the stack to store STACK_PIXEL's worth of PWM data. uint32_t to ensure alignment.
128+
// It is 3*STACK_PIXELS to handle RGB.
123129
// PATTERN_SIZE is a multiple of 4, so we don't need round up to make sure one_pixel is large enough.
124-
uint32_t one_pixel[PATTERN_SIZE(3)/sizeof(uint32_t)];
130+
uint32_t stack_pixels[PATTERN_SIZE(3 * STACK_PIXELS) / sizeof(uint32_t)];
125131

126132
NRF_PWM_Type* pwm = find_free_pwm();
127133

128134
// only malloc if there is PWM device available
129135
if ( pwm != NULL ) {
130-
if (pattern_size <= sizeof(one_pixel)) {
131-
pixels_pattern = (uint16_t *) one_pixel;
136+
if (pattern_size <= sizeof(stack_pixels)) {
137+
pixels_pattern = (uint16_t *) stack_pixels;
132138
} else {
133139
uint8_t sd_en = 0;
134140
(void) sd_softdevice_is_enabled(&sd_en);

supervisor/shared/board.c

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2020 Dan Halbert for Adafruit Industries
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include "supervisor/shared/board.h"
28+
29+
#include "shared-bindings/digitalio/DigitalInOut.h"
30+
#include "shared-bindings/neopixel_write/__init__.h"
31+
32+
#ifdef USER_NEOPIXELS_PIN
33+
34+
void board_reset_user_neopixels(void) {
35+
// Turn off on-board NeoPixel string
36+
uint8_t empty[30] = { 0 };
37+
digitalio_digitalinout_obj_t neopixel_pin;
38+
common_hal_digitalio_digitalinout_construct(&neopixel_pin, USER_NEOPIXELS_PIN);
39+
common_hal_digitalio_digitalinout_switch_to_output(&neopixel_pin, false,
40+
DRIVE_MODE_PUSH_PULL);
41+
common_hal_neopixel_write(&neopixel_pin, empty, 30);
42+
common_hal_digitalio_digitalinout_deinit(&neopixel_pin);
43+
}
44+
45+
#endif

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy