Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Bitbanged/fragile ws2812 driver * Hooked up to the register * Python tester to make pretty colors
- Loading branch information
Showing
3 changed files
with
135 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
from periphery import * | ||
|
||
i = I2C("/dev/i2c-0") | ||
|
||
I2C_ADDRESS = 0x25 | ||
|
||
def process_key(key, size): | ||
if type(key) is int: | ||
start = key | ||
length = 1 | ||
else: | ||
slice_start, slice_stop, slice_step = key.indices(size) | ||
if slice_step != 1: | ||
raise AttributeError("Can't have non 1 step.") | ||
|
||
start = slice_start | ||
length = slice_stop - slice_start | ||
return start, length | ||
|
||
class FretRegisters(object): | ||
def __getitem__(self, key): | ||
start, length = process_key(key, 256) | ||
|
||
msg = [ | ||
I2C.Message([start, 0x00]), | ||
I2C.Message([0x00] * length, read=True) | ||
]; | ||
|
||
i.transfer(I2C_ADDRESS, msg); | ||
|
||
if type(key) is int: | ||
return msg[1].data[0]; | ||
else: | ||
return msg[1].data; | ||
|
||
def __setitem__(self, key, value): | ||
start, length = process_key(key, 256) | ||
if type(key) is int: | ||
value = [value] | ||
else: | ||
value = list(value) | ||
|
||
i.transfer(I2C_ADDRESS, [I2C.Message([start, 0x00] + value)]); | ||
|
||
def __repr__(self): | ||
return "FretRegisters"+repr(f[:]) | ||
|
||
f=FretRegisters() | ||
|
||
def dynamic_led_test(): | ||
def rotate(l,n): | ||
return l[n:] + l[:n] | ||
|
||
colors = ( | ||
[[255-x,0,x] for x in range(255)] + | ||
[[0,x,255-x] for x in range(255)] + | ||
[[x,255-x,0] for x in range(255)] | ||
) | ||
colors=colors[::6] #faster | ||
#colors = [[b,b,b] for r,g,b in colors] #white glow | ||
|
||
while True: | ||
for j in range(len(colors)): | ||
c=rotate(colors,j)[::len(colors)//6] | ||
#for g in range(20*2//10): | ||
##what if we had more frets(20x slower), was doing touch(2x slower), but increased the i2c speed(10x faster) too | ||
f[0x80:]=sum(c,[])+[0x00] | ||
#for g in range(20*2//10): | ||
##blink test to show the fps | ||
#f[0x80:]=[0x00]*6*3+[0x00] | ||
|
||
if __name__=="__main__": | ||
dynamic_led_test() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
//Bitbanging ws2812 implementation | ||
//Requires disabled interrupts to run | ||
//Dependent on system clock being 48MHz | ||
//Here be dragons, don't touch | ||
|
||
#include <libopencm3/cm3/cortex.h> | ||
|
||
__attribute__((optimize("unroll-loops"))) | ||
__attribute__((always_inline)) | ||
inline void ws2812_write_byte(uint8_t byte) { | ||
for(int j = 7; j >= 0; j--) | ||
{ | ||
bool bit = (byte & (1 << j)) == 0; | ||
gpio_set(GPIOC,GPIO12); | ||
GPIOC_BSRR = 1 << 12; | ||
if(bit) | ||
GPIOC_BSRR = 1 << 12 << 16; | ||
__asm__("nop"); | ||
__asm__("nop"); | ||
__asm__("nop"); | ||
__asm__("nop"); | ||
__asm__("nop"); | ||
__asm__("nop"); | ||
GPIOC_BSRR = 1 << 12 << 16; | ||
} | ||
} | ||
|
||
__attribute__((optimize("unroll-loops"))) | ||
void ws2812_sendarray(uint8_t *buf, uint16_t size) { | ||
for(volatile int i = 0; i < 100; i++); | ||
|
||
cm_disable_interrupts(); | ||
for(uint16_t i = 0; i < size; i+=3) | ||
{ | ||
ws2812_write_byte(buf[i+1]); | ||
ws2812_write_byte(buf[i+0]); | ||
ws2812_write_byte(buf[i+2]); | ||
} | ||
cm_enable_interrupts(); | ||
} |