Skip to content

Commit 2f0c435

Browse files
committed
Leven: add placeholder library for LED experiments
1 parent 1873eaa commit 2f0c435

File tree

4 files changed

+409
-0
lines changed

4 files changed

+409
-0
lines changed
Lines changed: 264 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,264 @@
1+
#include "FspTimer.h"
2+
#include "ArduinoGraphics.h"
3+
4+
// the setup function runs once when you press reset or power the board
5+
void setup() {
6+
// initialize digital pin LED_BUILTIN as an output.
7+
pinMode(LED_BUILTIN, OUTPUT);
8+
Serial.begin(115200);
9+
Serial1.begin(115200);
10+
Serial2.begin(115200);
11+
}
12+
13+
const int led_zero_index = 28;
14+
const int leds_howmany = 11;
15+
16+
size_t pins[][2] = {
17+
{ 7, 3 }, // 0
18+
{ 3, 7 },
19+
{ 7, 4 },
20+
{ 4, 7 },
21+
{ 3, 4 },
22+
{ 4, 3 },
23+
{ 7, 8 },
24+
{ 8, 7 },
25+
{ 3, 8 },
26+
{ 8, 3 },
27+
{ 4, 8 }, // 10
28+
{ 8, 4 },
29+
{ 7, 0 },
30+
{ 0, 7 },
31+
{ 3, 0 },
32+
{ 0, 3 },
33+
{ 4, 0 },
34+
{ 0, 4 },
35+
{ 8, 0 },
36+
{ 0, 8 },
37+
{ 7, 6 }, // 20
38+
{ 6, 7 },
39+
{ 3, 6 },
40+
{ 6, 3 },
41+
{ 4, 6 },
42+
{ 6, 4 },
43+
{ 8, 6 },
44+
{ 6, 8 },
45+
{ 0, 6 },
46+
{ 6, 0 },
47+
{ 7, 5 }, // 30
48+
{ 5, 7 },
49+
{ 3, 5 },
50+
{ 5, 3 },
51+
{ 4, 5 },
52+
{ 5, 4 },
53+
{ 8, 5 },
54+
{ 5, 8 },
55+
{ 0, 5 },
56+
{ 5, 0 },
57+
{ 6, 5 }, // 40
58+
{ 5, 6 },
59+
{ 7, 1 },
60+
{ 1, 7 },
61+
{ 3, 1 },
62+
{ 1, 3 },
63+
{ 4, 1 },
64+
{ 1, 4 },
65+
{ 8, 1 },
66+
{ 1, 8 },
67+
{ 0, 1 }, // 50
68+
{ 1, 0 },
69+
{ 6, 1 },
70+
{ 1, 6 },
71+
{ 5, 1 },
72+
{ 1, 5 },
73+
{ 7, 2 },
74+
{ 2, 7 },
75+
{ 3, 2 },
76+
{ 2, 3 },
77+
{ 4, 2 },
78+
{ 2, 4 },
79+
{ 8, 2 },
80+
{ 2, 8 },
81+
{ 0, 2 },
82+
{ 2, 0 },
83+
{ 6, 2 },
84+
{ 2, 6 },
85+
{ 5, 2 },
86+
{ 2, 5 },
87+
{ 1, 2 },
88+
{ 2, 1 },
89+
{ 7, 10 },
90+
{ 10, 7 },
91+
{ 3, 10 },
92+
{ 10, 3 },
93+
{ 4, 10 },
94+
{ 10, 4 },
95+
{ 8, 10 },
96+
{ 10, 8 },
97+
{ 0, 10 },
98+
{ 10, 0 },
99+
{ 6, 10 },
100+
{ 10, 6 },
101+
{ 5, 10 },
102+
{ 10, 5 },
103+
{ 1, 10 },
104+
{ 10, 1 },
105+
{ 2, 10 },
106+
{ 10, 2 },
107+
{ 7, 9 },
108+
{ 9, 7 },
109+
{ 3, 9 },
110+
{ 9, 3 },
111+
{ 4, 9 },
112+
{ 9, 4 },
113+
};
114+
115+
#include "charmatrix.h"
116+
void display_char(int letter) {
117+
118+
for (int i = 0; i < 5; i++) {
119+
if (font[letter].d[i] & 1) {
120+
turnLed(3 + (12 * i), true);
121+
}
122+
if (font[letter].d[i] & 2) {
123+
turnLed(2 + (12 * i), true);
124+
}
125+
if (font[letter].d[i] & 4) {
126+
turnLed(1 + (12 * i), true);
127+
}
128+
if (font[letter].d[i] & 8) {
129+
turnLed(0 + (12 * i), true);
130+
}
131+
}
132+
}
133+
134+
void turnLed(int idx, bool on) {
135+
size_t pin_anode = pins[idx][0] + led_zero_index;
136+
size_t pin_catode = pins[idx][1] + led_zero_index;
137+
138+
for (int i = led_zero_index; i < led_zero_index + leds_howmany; i++) {
139+
//if (i != pin_anode && i != pin_catode) {
140+
pinMode(i, INPUT);
141+
//}
142+
}
143+
if (on) {
144+
pinMode(pin_anode, OUTPUT);
145+
pinMode(pin_catode, OUTPUT);
146+
digitalWrite(pin_anode, HIGH);
147+
digitalWrite(pin_catode, LOW);
148+
}
149+
}
150+
151+
#include <ArduinoGraphics.h>
152+
153+
const byte canvasWidth = 12;
154+
const byte canvasHeight = 8;
155+
156+
class LEDsDrawClass : public ArduinoGraphics {
157+
public:
158+
// can be used with an object of any class that inherits from the Print class
159+
LEDsDrawClass(Print &printObject = (Print &)Serial) :
160+
ArduinoGraphics(canvasWidth, canvasHeight),
161+
_printObject(&printObject) {
162+
memset((uint8_t*)_canvasBuffer, 0, canvasWidth * canvasHeight) ;
163+
}
164+
165+
// this function is called by the ArduinoGraphics library's functions
166+
virtual void set(int x, int y, uint8_t r, uint8_t g, uint8_t b) {
167+
// the r parameter is (mis)used to set the character to draw with
168+
_canvasBuffer[y][x] = r;
169+
// cast unused parameters to void to fix "unused parameter" warning
170+
(void)g;
171+
(void)b;
172+
}
173+
174+
// display the drawing
175+
void endDraw() {
176+
ArduinoGraphics::endDraw();
177+
for (int i = 0; i < canvasWidth * canvasHeight; i++) {
178+
Serial.println(((uint8_t*)_canvasBuffer)[i]);
179+
}
180+
}
181+
182+
uint8_t* getCanvas() {
183+
return (uint8_t*)_canvasBuffer;
184+
}
185+
186+
private:
187+
Print *_printObject;
188+
char _canvasBuffer[canvasHeight][canvasWidth] = {{0}};
189+
};
190+
191+
LEDsDrawClass LEDS;
192+
193+
volatile int i_isr = 0;
194+
void __attribute__((naked)) turnOnLedISR(timer_callback_args_t *arg) {
195+
asm("push {LR}");
196+
turnLed(i_isr, (LEDS.getCanvas()[i_isr] > 0));
197+
i_isr = (i_isr + 1) % 96;
198+
asm("pop {r0}"); asm("bx r0");
199+
}
200+
201+
int i = 0;
202+
// the loop function runs over and over again forever
203+
void loop() {
204+
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
205+
delay(1000); // wait for a second
206+
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
207+
delay(1000); // wait for a second
208+
Serial.println(i++);
209+
Serial2.println("AT");
210+
while (Serial2.available()) {
211+
Serial.write(Serial2.read());
212+
}
213+
Serial2.println("AT+WIFIBEGIN=BCMI,ArduinoccRulez");
214+
while (Serial2.available()) {
215+
Serial.write(Serial2.read());
216+
}
217+
218+
int j = 0;
219+
220+
#if 1
221+
while (1) {
222+
turnLed(j++ % 96, true);
223+
delay(33);
224+
if (j == 96) {
225+
break;
226+
}
227+
}
228+
229+
FspTimer led_timer;
230+
231+
uint8_t type;
232+
uint8_t ch = FspTimer::get_available_timer(type);
233+
led_timer.begin(TIMER_MODE_PERIODIC, type, ch, 10000.0, 50.0, turnOnLedISR);
234+
led_timer.setup_overflow_irq();
235+
led_timer.open();
236+
led_timer.start();
237+
238+
draw_leds();
239+
240+
while (1) {
241+
if (Serial.available()) {
242+
Serial.write(Serial.read());
243+
}
244+
}
245+
246+
while (1) {
247+
for (int i = 0; i < 96; i++) {
248+
delayMicroseconds(100);
249+
turnLed(i, true);
250+
}
251+
}
252+
#endif
253+
254+
while (1) {
255+
while (Serial1.available()) {
256+
Serial.write(Serial1.read());
257+
}
258+
if (millis() % 500 == 0) {
259+
delay(1);
260+
j++;
261+
}
262+
display_char(j % (sizeof(font) / sizeof(font[0])));
263+
}
264+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
struct charmatrix { char c; int d[5]; };
2+
static const charmatrix font[] =
3+
{
4+
5+
//"4x5 character map.
6+
//https://github.com/tompreston/4x5-Font
7+
//http://clubweb.interbaun.com/~rc/Papers/microfont.pdf
8+
9+
' ', {0x0, 0x0, 0x0, 0x0, 0x0},
10+
'!', {0x4, 0x4, 0x4, 0x0, 0x4},
11+
'"', {0x1, 0x2, 0x0, 0x0, 0x0},
12+
'#', {0x6, 0xf, 0x6, 0xf, 0x6},
13+
'$', {0x7, 0xa, 0x6, 0x5, 0xe},
14+
'%', {0x7, 0xe, 0x4, 0x7, 0xe},
15+
'&', {0x2, 0x5, 0x6, 0xa, 0x5},
16+
'\'', {0x0, 0xa, 0x0, 0x0, 0x0},
17+
'(', {0x4, 0x8, 0x8, 0x8, 0x4},
18+
')', {0x4, 0x2, 0x2, 0x2, 0x4},
19+
'*', {0x0, 0x6, 0xf, 0x6, 0x0},
20+
'+', {0x0, 0x2, 0x7, 0x2, 0x0},
21+
',', {0x0, 0x0, 0x0, 0x2, 0x4},
22+
'-', {0x0, 0x0, 0x0, 0xf, 0x0},
23+
'.', {0x0, 0x0, 0x0, 0x0, 0x4},
24+
'/', {0x1, 0x1, 0x2, 0x4, 0x8},
25+
'0', {0x6, 0xb, 0xf, 0xd, 0x6},
26+
'1', {0x2, 0x6, 0x2, 0x2, 0x2},
27+
'2', {0xe, 0x1, 0x6, 0x8, 0xf},
28+
'3', {0xe, 0x1, 0x6, 0x1, 0xe},
29+
'4', {0x2, 0x6, 0xa, 0xf, 0x2},
30+
'5', {0xf, 0x8, 0xe, 0x1, 0xe},
31+
'6', {0x6, 0x8, 0xe, 0x9, 0x6},
32+
'7', {0xf, 0x1, 0x2, 0x4, 0x8},
33+
'8', {0x6, 0x9, 0x6, 0x9, 0x6},
34+
'9', {0x6, 0x9, 0xf, 0x1, 0x6},
35+
':', {0x0, 0x4, 0x0, 0x4, 0x0},
36+
';', {0x0, 0x4, 0x0, 0x4, 0x8},
37+
'<', {0x2, 0x4, 0x8, 0x4, 0x2},
38+
'=', {0x0, 0xf, 0x0, 0xf, 0x0},
39+
'>', {0x4, 0x2, 0x1, 0x2, 0x4},
40+
'?', {0x6, 0x9, 0x2, 0x0, 0x2},
41+
'@', {0x6, 0xd, 0xb, 0x8, 0x6},
42+
'A', {0x4, 0xa, 0xe, 0xa, 0xa},
43+
'B', {0xe, 0x9, 0xe, 0x9, 0xe},
44+
'C', {0x6, 0x9, 0x8, 0x9, 0x6},
45+
'D', {0xe, 0x9, 0x9, 0x9, 0xe},
46+
'E', {0xf, 0x8, 0xe, 0x8, 0xf},
47+
'F', {0xf, 0x8, 0xe, 0x8, 0x8},
48+
'G', {0x6, 0x8, 0xb, 0x9, 0x6},
49+
'H', {0x9, 0x9, 0xf, 0x9, 0x9},
50+
'I', {0xe, 0x4, 0x4, 0x4, 0xe},
51+
'J', {0x1, 0x1, 0x1, 0x9, 0x6},
52+
'K', {0x9, 0xa, 0xc, 0xa, 0x9},
53+
'L', {0x8, 0x8, 0x8, 0x8, 0xf},
54+
'M', {0x9, 0xf, 0xf, 0x9, 0x9},
55+
'N', {0x9, 0xd, 0xf, 0xb, 0x9},
56+
'O', {0x6, 0x9, 0x9, 0x9, 0x6},
57+
'P', {0xe, 0x9, 0xe, 0x8, 0x8},
58+
'Q', {0x6, 0x9, 0x9, 0xb, 0x7},
59+
'R', {0xe, 0x9, 0xe, 0xa, 0x9},
60+
'S', {0x7, 0x8, 0x6, 0x1, 0xe},
61+
'T', {0xe, 0x4, 0x4, 0x4, 0x4},
62+
'U', {0x9, 0x9, 0x9, 0x9, 0x6},
63+
'V', {0x9, 0x9, 0x9, 0x6, 0x6},
64+
'W', {0x9, 0x9, 0xf, 0xf, 0x9},
65+
'X', {0x9, 0x9, 0x6, 0x9, 0x9},
66+
'Y', {0x9, 0x5, 0x2, 0x2, 0x2},
67+
'Z', {0xf, 0x2, 0x4, 0x8, 0xf},
68+
'[', {0xe, 0x8, 0x8, 0x8, 0xe},
69+
'\\', {0x8, 0x8, 0x4, 0x2, 0x1},
70+
']', {0x7, 0x1, 0x1, 0x1, 0x7},
71+
'^', {0x4, 0xa, 0x0, 0x0, 0x0},
72+
'_', {0x0, 0x0, 0x0, 0x0, 0xf},
73+
'`', {0x4, 0x2, 0x0, 0x0, 0x0},
74+
'a', {0x0, 0x5, 0xb, 0xb, 0x5},
75+
'b', {0x8, 0x8, 0xe, 0x9, 0xe},
76+
'c', {0x0, 0x7, 0x8, 0x8, 0x7},
77+
'd', {0x1, 0x1, 0x7, 0x9, 0x7},
78+
'e', {0x0, 0x6, 0xf, 0x8, 0x7},
79+
'f', {0x3, 0x4, 0xe, 0x4, 0x4},
80+
'g', {0x7, 0x9, 0x7, 0x1, 0x7},
81+
'h', {0x8, 0x8, 0xe, 0x9, 0x9},
82+
'i', {0x0, 0x2, 0x0, 0x2, 0x2},
83+
'j', {0x1, 0x0, 0x1, 0x1, 0x6},
84+
'k', {0x8, 0xa, 0xc, 0xa, 0x9},
85+
'l', {0xc, 0x4, 0x4, 0x4, 0xe},
86+
'm', {0x0, 0x9, 0xf, 0xf, 0x9},
87+
'n', {0x0, 0xe, 0x9, 0x9, 0x9},
88+
'o', {0x0, 0x6, 0x9, 0x9, 0x6},
89+
'p', {0x0, 0xe, 0x9, 0xe, 0x8},
90+
'q', {0x0, 0x6, 0x9, 0x7, 0x1},
91+
'r', {0x0, 0xb, 0xc, 0x8, 0x8},
92+
's', {0x0, 0x7, 0x4, 0x2, 0xe},
93+
't', {0x4, 0xe, 0x4, 0x4, 0x3},
94+
'u', {0x0, 0x9, 0x9, 0x9, 0x6},
95+
'v', {0x0, 0x9, 0x9, 0x6, 0x6},
96+
'w', {0x0, 0x9, 0xf, 0xf, 0x6},
97+
'x', {0x0, 0x9, 0x6, 0x6, 0x9},
98+
'y', {0x0, 0x9, 0x7, 0x1, 0x6},
99+
'z', {0x0, 0xf, 0x2, 0x4, 0xf},
100+
'{', {0x6, 0x4, 0xc, 0x4, 0x6},
101+
'|', {0x4, 0x4, 0x0, 0x4, 0x4},
102+
'}', {0xc, 0x4, 0x6, 0x4, 0xc},
103+
'~', {0x0, 0x0, 0x5, 0xa, 0x0}
104+
};

0 commit comments

Comments
 (0)