-
Notifications
You must be signed in to change notification settings - Fork 0
/
tlc.h
111 lines (90 loc) · 3.2 KB
/
tlc.h
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
/***************************************************************************
* *
Mini library for particular Christmas lights
project based on TLC59731
(c) 2015 Jan Prihoda
* *
* *
***************************************************************************
* *
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU License V2 for more details.
* *
***************************************************************************/
#include "Arduino.h"
#ifndef tlc_H
#define tlc_H
#define auto_pad
#ifndef auto_pad
#define start_bit_pad 0
#define high_bit_pad 1
#define low_bit_pad 7
#define latch_length 160
#define eos_length 70
#endif
class tlc
{
public:
tlc(int pin = 8){pin_out = pin;}
#ifdef auto_pad
long start_bit_pad = 0;
long low_bit_pad = 0;
long high_bit_pad = 0;
long latch_length = 0;
long eos_length = 0;
void configure(long freq)
{
long msecs = 1000000 / freq;
unsigned long startM, stopM;
startM = micros();
startBit(1);startBit(1);startBit(1);startBit(1);
stopM = micros();
unsigned long start_bit_min = (stopM - startM)/4;
startM = micros();
writeBit(0);writeBit(0);writeBit(0);writeBit(0);
stopM = micros();
unsigned long low_bit_min = (stopM - startM)/4;
startM = micros();
writeBit(1);writeBit(1);writeBit(1);writeBit(1);
stopM = micros();
unsigned long high_bit_min = (stopM - startM)/4;
if((start_bit_min * 2) +2 > msecs)
{
msecs = (start_bit_min * 2) + 2;
}
start_bit_pad = (msecs/2 - 1) - start_bit_min;
high_bit_pad = msecs - (start_bit_pad + high_bit_min);
low_bit_pad = msecs - (start_bit_pad + low_bit_min);
latch_length = msecs * 8;
eos_length = msecs * 3.5;
}
void startBit(int data)
{
digitalWrite(pin_out, HIGH);
digitalWrite(pin_out, LOW);
if(data){}
}
void printSettings()
{
while (!Serial) {}
delay(10);
Serial.println("settings:");
Serial.print("#define start_bit_pad ");Serial.println(start_bit_pad);
Serial.print("#define high_bit_pad ");Serial.println(high_bit_pad);
Serial.print("#define low_bit_pad ");Serial.println(low_bit_pad);
Serial.print("#define latch_length ");Serial.println(latch_length);
Serial.print("#define eos_length ");Serial.println(eos_length);
}
#endif
int pin_out;
void gsDataLatchSequence();
void EOS();
void writeBit(uint8_t data);
void writeByte(uint8_t data);
void writeLed(uint8_t r, uint8_t g, uint8_t b, bool latch);
};
#endif