-
Notifications
You must be signed in to change notification settings - Fork 1
/
apa102.c
62 lines (54 loc) · 1.63 KB
/
apa102.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// apa102.c
// Driver for APA102 RGB LEDs (e.g. sparkfun LuMi & LuMini RGB LED boards)
// by Roy Eltham
#include <propeller.h> // Propeller-specific functions
uint32_t _apa102ClkPinMask;
uint32_t _apa102DataPinMask;
void InitApa102(int clkPin, int dataPin)
{
_apa102ClkPinMask = 1 << clkPin;
_apa102DataPinMask = 1 << dataPin;
// set data and clk as outputs with the clk in a low state
DIRA |= _apa102DataPinMask;
OUTA &= (~_apa102ClkPinMask);
DIRA |= _apa102ClkPinMask;
}
__attribute__((fcache)) // allows function to run directly from cog ram, 10x+ speed increase
void _apa102WriteLeds(const uint32_t* leds, int numLeds)
{
OUTA &= (~_apa102ClkPinMask);
for (int j = 0; j < numLeds; j++)
{
for (int i = 31; i >= 0 ; i--)
{
if ((leds[j] >> i) & 1)
{
OUTA |= _apa102DataPinMask;
}
else
{
OUTA &= (~_apa102DataPinMask);
}
OUTA ^= _apa102ClkPinMask;
OUTA ^= _apa102ClkPinMask;
}
}
}
// write out numBits zeros
__attribute__((fcache)) // allows function to run directly from cog ram, 10x+ speed increase
void _apa102StartEndFrame(int numBits)
{
OUTA &= (~_apa102ClkPinMask);
OUTA &= (~_apa102DataPinMask);
for (int i = 0; i < numBits ; i++)
{
OUTA ^= _apa102ClkPinMask;
OUTA ^= _apa102ClkPinMask;
}
}
void SendLeds(const uint32_t* leds, int numLeds)
{
_apa102StartEndFrame(32);
_apa102WriteLeds( leds, numLeds );
_apa102StartEndFrame(64);
}