forked from CoolNeon/arduino-tcl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTCL.cpp
114 lines (96 loc) · 2.52 KB
/
TCL.cpp
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*****************************************************************************
* Tcl.cpp
*
* Copyright 2011-2012 Christpher De Vries
* This program is distributed under the Artistic License 2.0, a copy of which
* is included in the file LICENSE.txt
****************************************************************************/
#include "TCL.h"
TclClass TCL;
#ifdef TCL_DIO
uint8_t TclClass::datapinmask, TclClass::clkpinmask;
volatile uint8_t *TclClass::dataport, *TclClass::clkport;
#endif
void TclClass::begin() {
#ifdef TCL_SPI
// Set the SPI parameters
SPI.begin();
SPI.setBitOrder(MSBFIRST);
SPI.setDataMode(SPI_MODE0);
SPI.setClockDivider(SPI_CLOCK_DIV2);
#endif
#ifdef TCL_DIO
pinMode(TCL_CLOCKPIN, OUTPUT);
pinMode(TCL_DATAPIN, OUTPUT);
clkport = portOutputRegister(digitalPinToPort(TCL_CLOCKPIN));
clkpinmask = digitalPinToBitMask(TCL_CLOCKPIN);
dataport = portOutputRegister(digitalPinToPort(TCL_DATAPIN));
datapinmask = digitalPinToBitMask(TCL_DATAPIN);
*clkport &= ~clkpinmask;
*dataport &= ~datapinmask;
#endif
}
void TclClass::setupDeveloperShield() {
pinMode(TCL_MOMENTARY1, INPUT);
pinMode(TCL_MOMENTARY2, INPUT);
pinMode(TCL_SWITCH1, INPUT);
pinMode(TCL_SWITCH2, INPUT);
digitalWrite(TCL_MOMENTARY1, HIGH);
digitalWrite(TCL_MOMENTARY2, HIGH);
digitalWrite(TCL_SWITCH1, HIGH);
digitalWrite(TCL_SWITCH2, HIGH);
}
void TclClass::end() {
#ifdef TCL_SPI
SPI.end();
#endif
}
byte TclClass::makeFlag(byte red, byte green, byte blue) {
byte flag = 0;
flag = (red&0xC0)>>6;
flag |= ((green&0xC0)>>4);
flag |= ((blue&0xC0)>>2);
return ~flag;
}
#ifdef TCL_DIO
void TclClass::dioWrite(byte c) {
for(byte bit = 0x80; bit; bit >>= 1) {
if(c & bit) {
*dataport |= datapinmask;
} else {
*dataport &= ~datapinmask;
}
*clkport |= clkpinmask;
*clkport &= ~clkpinmask;
}
}
#endif
void TclClass::sendFrame(byte flag, byte red, byte green, byte blue) {
#ifdef TCL_SPI
SPI.transfer(flag);
SPI.transfer(blue);
SPI.transfer(green);
SPI.transfer(red);
#endif
#ifdef TCL_DIO
dioWrite(flag);
dioWrite(blue);
dioWrite(green);
dioWrite(red);
#endif
}
void TclClass::sendColor(byte red, byte green, byte blue) {
byte flag;
flag = makeFlag(red,green,blue);
sendFrame(flag,red,green,blue);
}
void TclClass::sendEmptyFrame() {
sendFrame(0x00,0x00,0x00,0x00);
}
void TclClass::setAll(int num_leds, byte red, byte green, byte blue) {
sendEmptyFrame();
for (int i=0; i<num_leds; i++) {
sendColor(red, green, blue);
}
sendEmptyFrame();
}