Skip to content

Commit f0b62a2

Browse files
committed
Save space by only supporting 800khz neopixels.
1 parent 710b5d8 commit f0b62a2

File tree

6 files changed

+28
-70
lines changed

6 files changed

+28
-70
lines changed

atmel-samd/common-hal/neopixel_write/__init__.c

Lines changed: 15 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030

3131
#include "asf/common2/services/delay/delay.h"
3232

33-
void common_hal_neopixel_write(const nativeio_digitalinout_obj_t* digitalinout, uint8_t *pixels, uint32_t numBytes, bool is800KHz) {
33+
void common_hal_neopixel_write(const nativeio_digitalinout_obj_t* digitalinout, uint8_t *pixels, uint32_t numBytes) {
3434
// This is adapted directly from the Adafruit NeoPixel library SAMD21G18A code:
3535
// https://github.com/adafruit/Adafruit_NeoPixel/blob/master/Adafruit_NeoPixel.cpp
3636
uint8_t *ptr, *end, p, bitMask;
@@ -54,53 +54,22 @@ void common_hal_neopixel_write(const nativeio_digitalinout_obj_t* digitalinout,
5454
volatile uint32_t *set = &(port->OUTSET.reg),
5555
*clr = &(port->OUTCLR.reg);
5656

57-
if(is800KHz) {
58-
for(;;) {
59-
*set = pinMask;
57+
for(;;) {
58+
*set = pinMask;
59+
asm("nop; nop;");
60+
if(p & bitMask) {
61+
asm("nop; nop; nop; nop; nop; nop; nop;");
62+
*clr = pinMask;
63+
} else {
64+
*clr = pinMask;
6065
asm("nop; nop;");
61-
if(p & bitMask) {
62-
asm("nop; nop; nop; nop; nop; nop; nop;");
63-
*clr = pinMask;
64-
} else {
65-
*clr = pinMask;
66-
asm("nop; nop;");
67-
}
68-
if((bitMask >>= 1) != 0) {
69-
asm("nop; nop; nop; nop; nop;");
70-
} else {
71-
if(ptr >= end) break;
72-
p = *ptr++;
73-
bitMask = 0x80;
74-
}
7566
}
76-
} else { // 400 KHz bitstream
77-
for(;;) {
78-
*set = pinMask;
79-
80-
asm("nop; nop; nop; nop; nop; nop; nop;");
81-
if(p & bitMask) {
82-
asm("nop; nop; nop; nop; nop; nop; nop; nop;"
83-
"nop; nop; nop; nop; nop; nop; nop; nop;"
84-
"nop; nop; nop;");
85-
*clr = pinMask;
86-
} else {
87-
*clr = pinMask;
88-
asm("nop; nop; nop; nop; nop; nop; nop; nop;"
89-
"nop; nop; nop; nop; nop; nop; nop; nop;"
90-
"nop; nop; nop; nop; nop; nop; nop; nop;"
91-
"nop; nop; nop;");
92-
}
93-
asm("nop; nop; nop; nop; nop; nop; nop; nop;"
94-
"nop; nop; nop; nop; nop; nop; nop; nop;"
95-
"nop; nop; nop; nop; nop; nop; nop; nop;"
96-
"nop; nop; nop; nop; nop; nop; nop; nop;");
97-
if(bitMask >>= 1) {
98-
asm("nop; nop; nop; nop; nop; nop; nop;");
99-
} else {
100-
if(ptr >= end) break;
101-
p = *ptr++;
102-
bitMask = 0x80;
103-
}
67+
if((bitMask >>= 1) != 0) {
68+
asm("nop; nop; nop; nop; nop;");
69+
} else {
70+
if(ptr >= end) break;
71+
p = *ptr++;
72+
bitMask = 0x80;
10473
}
10574
}
10675

esp8266/common-hal/neopixel_write/__init__.c

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

2929
#include "espneopixel.h"
3030

31-
void common_hal_neopixel_write(const nativeio_digitalinout_obj_t* digitalinout, uint8_t *pixels, uint32_t numBytes, bool is800KHz) {
32-
esp_neopixel_write(digitalinout->pin->gpio_number, pixels, numBytes, is800KHz);
31+
void common_hal_neopixel_write(const nativeio_digitalinout_obj_t* digitalinout, uint8_t *pixels, uint32_t numBytes) {
32+
esp_neopixel_write(digitalinout->pin->gpio_number, pixels, numBytes);
3333
}

esp8266/espneopixel.c

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
#define NEO_KHZ400 (1)
1818

19-
void /*ICACHE_RAM_ATTR*/ esp_neopixel_write(uint8_t pin, uint8_t *pixels, uint32_t numBytes, bool is800KHz) {
19+
void /*ICACHE_RAM_ATTR*/ esp_neopixel_write(uint8_t pin, uint8_t *pixels, uint32_t numBytes) {
2020

2121
uint8_t *p, *end, pix, mask;
2222
uint32_t t, time0, time1, period, c, startTime, pinMask;
@@ -30,19 +30,10 @@ void /*ICACHE_RAM_ATTR*/ esp_neopixel_write(uint8_t pin, uint8_t *pixels, uint32
3030

3131
uint32_t fcpu = system_get_cpu_freq() * 1000000;
3232

33-
#ifdef NEO_KHZ400
34-
if(is800KHz) {
35-
#endif
36-
time0 = fcpu / 2857143; // 0.35us
37-
time1 = fcpu / 1250000; // 0.8us
38-
period = fcpu / 800000; // 1.25us per bit
39-
#ifdef NEO_KHZ400
40-
} else { // 400 KHz bitstream
41-
time0 = fcpu / 2000000; // 0.5uS
42-
time1 = fcpu / 833333; // 1.2us
43-
period = fcpu / 400000; // 2.5us per bit
44-
}
45-
#endif
33+
34+
time0 = fcpu / 2857143; // 0.35us
35+
time1 = fcpu / 1250000; // 0.8us
36+
period = fcpu / 800000; // 1.25us per bit
4637

4738
uint32_t irq_state = mp_hal_quiet_timing_enter();
4839
for(t = time0;; t = time0) {

esp8266/espneopixel.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
void esp_neopixel_write(uint8_t pin, uint8_t *pixels, uint32_t numBytes, bool is800KHz);
1+
void esp_neopixel_write(uint8_t pin, uint8_t *pixels, uint32_t numBytes);

shared-bindings/neopixel_write/__init__.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,27 +40,25 @@
4040
//| :platform: SAMD21
4141
//|
4242
//| The `neopixel_write` module contains a helper method to write out bytes in
43-
//| the neopixel protocol.
43+
//| the 800khz neopixel protocol.
4444

4545
//| .. method:: neopixel_write.neopixel_write(digitalinout, buf, is800KHz)
4646
//|
4747
//| Write buf out on the given DigitalInOut.
4848
//|
4949
//| :param ~nativeio.DigitalInOut gpio: the DigitalInOut to output with
5050
//| :param bytearray buf: The bytes to clock out. No assumption is made about color order
51-
//| :param bool is800KHz: True if the pixels are 800KHz, otherwise 400KHz is assumed.
5251
//|
53-
STATIC mp_obj_t neopixel_write_neopixel_write_(mp_obj_t digitalinout_obj, mp_obj_t buf, mp_obj_t is800k) {
52+
STATIC mp_obj_t neopixel_write_neopixel_write_(mp_obj_t digitalinout_obj, mp_obj_t buf) {
5453
// Convert parameters into expected types.
5554
const nativeio_digitalinout_obj_t *digitalinout = MP_OBJ_TO_PTR(digitalinout_obj);
5655
mp_buffer_info_t bufinfo;
5756
mp_get_buffer_raise(buf, &bufinfo, MP_BUFFER_READ);
5857
// Call platform's neopixel write function with provided buffer and options.
59-
common_hal_neopixel_write(digitalinout, (uint8_t*)bufinfo.buf, bufinfo.len,
60-
mp_obj_is_true(is800k));
58+
common_hal_neopixel_write(digitalinout, (uint8_t*)bufinfo.buf, bufinfo.len);
6159
return mp_const_none;
6260
}
63-
STATIC MP_DEFINE_CONST_FUN_OBJ_3(neopixel_write_neopixel_write_obj, neopixel_write_neopixel_write_);
61+
STATIC MP_DEFINE_CONST_FUN_OBJ_2(neopixel_write_neopixel_write_obj, neopixel_write_neopixel_write_);
6462

6563
STATIC const mp_rom_map_elem_t neopixel_write_module_globals_table[] = {
6664
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_neopixel_write) },

shared-bindings/neopixel_write/__init__.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,6 @@
3232

3333
#include "common-hal/nativeio/types.h"
3434

35-
extern void common_hal_neopixel_write(const nativeio_digitalinout_obj_t* gpio, uint8_t *pixels, uint32_t numBytes, bool is800KHz);
35+
extern void common_hal_neopixel_write(const nativeio_digitalinout_obj_t* gpio, uint8_t *pixels, uint32_t numBytes);
3636

3737
#endif

0 commit comments

Comments
 (0)