-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ino
222 lines (198 loc) · 4.95 KB
/
main.ino
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/* ---------------------------------------------------------------------------
** This software controls two ANNAX 41.4301.0470 LED Panel (total 192x16 LEDs)
** This software will not work on ATmega328/168/8 boards. It is meant to be
** run on an ESP8266
**
** Reverse Engineering has been done to identify the connecting pins.
** The PCB pin numbering refers to the female connector's pin numbering.
**
** Author: Fabian Lüpke
** -------------------------------------------------------------------------*/
// Disclaimer: This code is extremely hacky.
extern "C" {
#include "user_interface.h"
}
#include "SPI.h"
#include "font.h"
/* Wiring
*
* PCB Meaning Wemos
* 16 +5V +3V3
* 15 +5V +3V3
* 14 SRCK D5
* 13 GND GND
* 12 RCK D8
* 11 GND GND
* 10 ???
* 9 OUT_E D0
* 8 A0 D1
* 7 A1 D2
* 6 A2 D3
* 5 A3 D4
* 4 NC
* 3 NC
* 2 SEROUT Could be used to determine the number of boards attached
* 1 SERIN D7
*/
uint8_t framebuffer[24][16];
// Pin definitions
#define RCK D8
#define OUT_E D0
#define A0 D1
#define A1 D2
#define A2 D3
#define A3 D4
// Timer stuff
#define TIMER_FREQ 5000000
#define DISPLAY_FREQ 1000
#define TIMER_TICKS TIMER_FREQ / DISPLAY_FREQ
uint8_t scanLine = 0;
// Macros
#define pulse(pin) ({ digitalWrite(pin, HIGH); digitalWrite(pin, LOW); })
#define output_on() ( GPOS = (1 << OUT_E) )
#define output_off() ( GPOC = (1 << OUT_E) );
/**
* Setups the whole stuff
*/
void setup() {
setupOutputs();
setupSPI();
clearPixels();
setupInterrupts();
Serial.begin(115200);
// test
renderString("Hey there!\0", 96);
dumpFrameToSerial();
}
/**
* Configure necessary pins as outputs
*/
void setupOutputs() {
pinMode(RCK, OUTPUT);
pinMode(OUT_E, OUTPUT);
pinMode(A0, OUTPUT);
pinMode(A1, OUTPUT);
pinMode(A2, OUTPUT);
pinMode(A3, OUTPUT);
}
/**
* Configure SPI
*/
void setupSPI() {
SPI.begin();
SPI.setFrequency(30000000);
}
void setupInterrupts() {
timer1_isr_init();
timer1_attachInterrupt(&interruptHandler);
timer1_enable(TIM_DIV16, TIM_EDGE, TIM_LOOP);
timer1_write(TIMER_TICKS);
}
/**
* Main loop
*/
void loop() {
// Do nothing!
}
/**
* Clears all pixels
*/
void clearPixels() {
for (int x=0; x<24; x++) {
for (int y=0; y<16; y++) {
framebuffer[x][y] = 0;
}
}
}
void interruptHandler() {
pulse(RCK);
output_off();
digitalWrite(A0, (scanLine - 1) & 1);
digitalWrite(A1, (scanLine - 1) & 2);
digitalWrite(A2, (scanLine - 1) & 4);
digitalWrite(A3, (scanLine - 1) & 8);
for (int x=0; x<24; x++) {
SPI.transfer(framebuffer[x][scanLine]);
}
output_on();
scanLine++;
if (scanLine >= 16) scanLine = 0;
}
void setPixel(uint8_t x, uint8_t y) {
framebuffer[x >> 3][y] |= 1 << (x % 8);
}
void clearPixel(uint8_t x, uint8_t y) {
framebuffer[x >> 3][y] &= ~(1 << (x % 8));
}
void renderChar(char c, uint8_t x) {
uint32_t start = (c - 32) * 32;
Serial.print("Rendering ");
Serial.println(c);
/*
* 3 possibilities
*
* 1. Perfectly aligned
* x x x x x x x x X X X
*
* 2. Spreading two bytes
* x x x x x x x x X X X
*
* 3. Spreading three bytes
* x x x x x x x x X X X
* 0 1 2 3 4 5 6 7 | 8 9 10 11 12 13 14 15 | 16 17 18 19 20 21 22 23
* Byte 1 | Byte 2 | Byte 3
*/
// x & 7 == x % 8
uint8_t mod = x & 7;
if (mod == 0) { // case 1
for (int y=15; y>=0; y--) {
framebuffer[(x >> 3)][y] |= font[start];
framebuffer[(x >> 3) + 1][y] |= font[start+1];
start += 2;
}
} else if ((mod > 0) && (mod < 6)) { // case 2
for (int y=15; y>=0; y--) {
framebuffer[(x >> 3)][y] |= font[start] >> mod;
framebuffer[(x >> 3) + 1][y] |= (font[start] << (8 - mod)) | (font[start+1] >> mod);
start += 2;
}
/*
framebuffer[(x >> 3)][15] |= font[start] >> mod;
framebuffer[(x >> 3) + 1][15] |= (font[start] << (8 - mod)) | (font[start+1] >> mod); */
// ...
} else {
for (int y=15; y>=0; y--) {
framebuffer[(x >> 3)][y] |= font[start] >> mod;
framebuffer[(x >> 3) + 1][y] |= (font[start] << (8 - mod)) | (font[start+1] >> mod);
framebuffer[(x >> 3) + 2][y] |= (font[start+1] << (8 - mod));
start += 2;
}
/*
framebuffer[(x >> 3)][15] |= font[start] >> mod;
framebuffer[(x >> 3) + 1][15] |= (font[start] << (8 - mod)) | (font[start+1] >> mod);
framebuffer[(x >> 3) + 2][15] |= (font[start+1] << (8 - mod)); */
}
}
void renderString (char s[], uint8_t x) {
while(*s) {
renderChar(*s++, x);
x += 11;
if (x > 192) {
return;
}
}
}
void dumpFrameToSerial() {
for (int y=15; y>=0; y--) {
for (int x=0; x<24; x++) {
for (int z=7; z>=0; z--) {
if (framebuffer[x][y] & (1 << z)) {
Serial.print('X');
} else {
Serial.print('-');
}
}
}
Serial.print('\n');
}
}