-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmat.c
190 lines (174 loc) · 3.79 KB
/
mat.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
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
#include <stdlib.h>
#include <stdio.h>
#include "max.h"
#include "font/font.h"
#include "mat.h"
int DEVICES_NUMBER;
void command(int command, int data)
{
sendCommand(command, data);
}
void init8x8()
{
command(COMMAND_SCAN_LIMIT, 7);
maxWrite();
command(COMMAND_DECODE, 0);
maxWrite();
command(COMMAND_SHUTDOWN, 1);
maxWrite();
command(COMMAND_TEST, 0);
maxWrite();
command(COMMAND_INTENSITY, 2);
maxWrite();
}
void initMatrix(int DIN, int CR, int CK, int devicesNumber)
{
DEVICES_NUMBER = devicesNumber;
initMax(DIN, CR, CK);
int i;
for (i = 0; i < DEVICES_NUMBER * 4; i++)
{
init8x8();
}
}
void tearDownMatrix()
{
tearDownMax();
}
int imageSize(char * str, int strLength)
{
int size = 0, i;
for (i = 0; i < strLength; i++)
{
size += getCharSize(str[i]) + 1;
}
return size;
}
unsigned short int ** createImageBuffer(int width)
{
unsigned short int ** image = (unsigned short int**) malloc(8 * sizeof(unsigned short int *));
int i;
for (i = 0; i < 8; i++)
{
image[i] = calloc(width, sizeof(unsigned short int));
}
return image;
}
int bitsToInt(unsigned short int * intStr, int index, int size)
{
int i, result = 0;
for (i = 0; i < 8; i++)
{
result <<= 1;
result ^= (index + i < size && index + i >= 0) ? intStr[index + i] : 0;
}
return result;
}
unsigned short int * intToBits(int i, int size)
{
unsigned short int * res = (unsigned short int *) calloc(size, sizeof(unsigned short int));
int index;
for (index = 0; index < size; index++)
{
res[size - index - 1] = i & 1;
i >>= 1;
}
return res;
}
unsigned short int ** getCharBits(int * ch, int size)
{
unsigned short int ** image = createImageBuffer(size);
int i;
for (i = 0; i < 8; i++)
{
image[i] = intToBits(ch[i], size);
}
return image;
}
unsigned short int ** buildImage(char * str, int size)
{
unsigned short int ** image = createImageBuffer(imageSize(str, size));
int width, height, strI, imageI = 0;
for (strI = 0; strI < size; strI++)
{
int * ch = getChar(str[strI]);
int size = getCharSize(str[strI]);
unsigned short int ** chBits = getCharBits(ch, size);
for (width = 0; width < size; width++)
{
for (height = 0; height < 8; height++)
{
image[height][imageI + width] = chBits[height][width];
}
}
imageI += size + 1;
for (height = 0; height < 8; height++)
{
image[height][imageI] = 0;
}
}
return image;
}
void displayFixed(unsigned short int ** image, int size, int scroll)
{
int i, display;
for (i = 0; i < 8; i++)
{
for (display = 0; display < DEVICES_NUMBER; display++)
{
command(COMMANDS_DIGIT[i], bitsToInt(image[i], display * 8 + scroll, size));
//for (res = DEVICES_NUMBER - 1; res > 0; res--) {
// command(COMMAND_NOOP, 0);
// }
// maxWrite();
}
maxWrite();
microSleep(2500);
}
}
void scrollRL(unsigned short int ** image, int size)
{
int i;
for (i = 0; i < size + 8 * DEVICES_NUMBER + 1; i++)
{
displayFixed(image, size, size - i);
}
}
char * revertString(char * str, int size)
{
char * res = calloc(size, sizeof(char));
int i;
for (i = 0; i < size; i++)
{
res[size - i - 1] = str[i];
}
return res;
}
void printImage(unsigned short int ** image, int size)
{
int width, height;
for (height = 7; height >= 0; height--)
{
for(width = 0; width < size; width ++)
{
printf(image[height][width] == 1 ? "* " : ". ");
}
printf("\n");
}
}
void displayString(char * str, int size, int effect)
{
unsigned short int ** image = buildImage(revertString(str, size), size);
if (effect == 1)
{
scrollRL(image, imageSize(str, size));
}
else if (effect == 2)
{
displayFixed(image, imageSize(str, size), 0);
}
else
{
//TODO display error (! on every display)
}
}