-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathmain-oled-exp.c
332 lines (286 loc) · 9.76 KB
/
main-oled-exp.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
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#include <oled-exp.h>
// function prototypes:
void Usage (const char* progName);
int oledCommand (char *command, char *param);
// print the usage info
void usage(const char* progName)
{
onionPrint(ONION_SEVERITY_FATAL, "\n");
onionPrint(ONION_SEVERITY_FATAL, "Usage: oled-exp -i\n");
onionPrint(ONION_SEVERITY_FATAL, "\n");
onionPrint(ONION_SEVERITY_FATAL, "FUNCTIONALITY:\n");
onionPrint(ONION_SEVERITY_FATAL, " Initialize the OLED Display\n");
onionPrint(ONION_SEVERITY_FATAL, "\n\n");
onionPrint(ONION_SEVERITY_FATAL, "Usage: oled-exp -c\n");
onionPrint(ONION_SEVERITY_FATAL, "\n");
onionPrint(ONION_SEVERITY_FATAL, "FUNCTIONALITY:\n");
onionPrint(ONION_SEVERITY_FATAL, " Clear the OLED Display\n");
onionPrint(ONION_SEVERITY_FATAL, "\n\n");
onionPrint(ONION_SEVERITY_FATAL, "Usage: oled-exp [-icqv] COMMAND PARAMETER\n");
onionPrint(ONION_SEVERITY_FATAL, "\n");
onionPrint(ONION_SEVERITY_FATAL, "The following COMMANDs are available:\n");
onionPrint(ONION_SEVERITY_FATAL, " power <on|off> Turn the display on or off\n");
onionPrint(ONION_SEVERITY_FATAL, " write <message> Write the input string on the display\n");
onionPrint(ONION_SEVERITY_FATAL, " writeByte <byte> Write the input byte on the display\n");
onionPrint(ONION_SEVERITY_FATAL, " dim <on|off> Adjust the screen brightness\n");
onionPrint(ONION_SEVERITY_FATAL, " invert <on|off> Invert the colors on the display\n");
onionPrint(ONION_SEVERITY_FATAL, " cursor <row>,<column> Set the cursor to the specified row and column\n");
onionPrint(ONION_SEVERITY_FATAL, " cursorPixel <row>,<pixel> Set the cursor to the specified row and pixel\n");
onionPrint(ONION_SEVERITY_FATAL, " scroll <direction> Enable scrolling of screen content\n");
onionPrint(ONION_SEVERITY_FATAL, " available directions: left, right, diagonal-left, diagonal-right\n");
onionPrint(ONION_SEVERITY_FATAL, " to stop scrolling: stop\n");
onionPrint(ONION_SEVERITY_FATAL, " draw <lcd file> Draw the contents of an lcd file to the display\n");
onionPrint(ONION_SEVERITY_FATAL, "\n");
onionPrint(ONION_SEVERITY_FATAL, "COMMANDs can be cascaded one after another, they will execute in order.\n");
onionPrint(ONION_SEVERITY_FATAL, "\n");
onionPrint(ONION_SEVERITY_FATAL, "OPTIONS:\n");
onionPrint(ONION_SEVERITY_FATAL, " -i initialize display\n");
onionPrint(ONION_SEVERITY_FATAL, " -c clear the display\n");
onionPrint(ONION_SEVERITY_FATAL, " -q quiet: no output\n");
onionPrint(ONION_SEVERITY_FATAL, " -v verbose: lots of output\n");
onionPrint(ONION_SEVERITY_FATAL, " -h help: show this prompt\n");
onionPrint(ONION_SEVERITY_FATAL, "\n");
}
// execute a specified command
int oledCommand(char *command, char *param)
{
int status;
int val0, val1;
uint8_t *buffer;
// perform the specified command
onionPrint(ONION_SEVERITY_DEBUG_EXTRA, "command = '%s', param = '%s'\n", command, param);
if (strcmp(command, "write") == 0 ) {
status = oledWrite(param);
}
else if (strcmp(command, "writeByte") == 0 ) {
// parse the byte
if (param[0] == '0' && param[1] == 'x') {
sscanf(param, "0x%02x", &val0);
}
else {
sscanf(param, "%02x", &val0);
}
status = oledWriteByte(val0);
}
else if (strcmp(command, "brightness") == 0 ) {
status = oledSetBrightness( atoi(param) );
}
else if (strcmp(command, "invert") == 0 ) {
// interpret the parameter
val0 = 0; // off by default
if (strcmp(param, "on") == 0 ) {
val0 = 1;
}
status = oledSetDisplayMode( val0 );
}
else if (strcmp(command, "power") == 0 ) {
// interpret the parameter
val0 = 0; // off by default
if (strcmp(param, "on") == 0 ) {
val0 = 1;
}
status = oledSetDisplayPower(val0);
}
else if (strcmp(command, "dim") == 0 ) {
// interpret the parameter
val0 = 0; // off by default
if (strcmp(param, "on") == 0 ) {
val0 = 1;
}
status = oledSetDim(val0);
}
else if (strcmp(command, "cursor") == 0 ) {
// interpret the parameter
sscanf(param, "%d, %d", &val0, &val1);
onionPrint(ONION_SEVERITY_INFO, "> Setting cursor to (%d, %d)\n", val0, val1);
status = oledSetTextColumns();
status = oledSetCursor(val0, val1);
}
else if (strcmp(command, "cursorPixel") == 0 ) {
// interpret the parameter
sscanf(param, "%d, %d", &val0, &val1);
onionPrint(ONION_SEVERITY_INFO, "> Setting cursor to row: %d, pixel: %d\n", val0, val1);
status = oledSetImageColumns();
status = oledSetCursorByPixel(val0, val1);
}
else if (strcmp(command, "draw") == 0 ) {
// allocate memory for the buffer
buffer = malloc(OLED_EXP_WIDTH*OLED_EXP_HEIGHT/8 * sizeof *buffer);
memset(buffer, 0, OLED_EXP_WIDTH*OLED_EXP_HEIGHT/8 * sizeof *buffer); // FIXME: We should definitely do a #define for the buffer size calculation. This looks ugly.
// read the parameter
if ( strncmp(param, OLED_EXP_READ_LCD_DATA_IDENTIFIER, strlen(OLED_EXP_READ_LCD_DATA_IDENTIFIER) ) == 0 ) {
onionPrint(ONION_SEVERITY_INFO, "> Reading data from argument\n");
onionPrint(ONION_SEVERITY_DEBUG_EXTRA, " param length is %d\n", strlen(param) );
// remove the data identifier from the string
memmove ( param,
param + strlen(OLED_EXP_READ_LCD_DATA_IDENTIFIER),
strlen(param)
);
onionPrint(ONION_SEVERITY_DEBUG_EXTRA, " after move: param length is %d\n", strlen(param) );
// read the data into a buffer
status = oledReadLcdData(param, buffer);
}
else {
// read data from a file
onionPrint(ONION_SEVERITY_INFO, "> Reading data from file '%s'\n", param);
status = oledReadLcdFile(param, buffer);
}
if (status == EXIT_SUCCESS) {
status = oledDraw(buffer, OLED_EXP_WIDTH*OLED_EXP_HEIGHT/8);
}
else {
onionPrint(ONION_SEVERITY_FATAL, "ERROR: Cannot draw invalid data\n");
}
// deallocate memory for the buffer
if (buffer != NULL) {
onionPrint(ONION_SEVERITY_DEBUG_EXTRA, "> Deallocating buffer array\n");
free(buffer);
}
}
else if (strcmp(command, "scroll") == 0 ) {
// interpret the parameters
val0 = -1;
val1 = -1;
if (strcmp(param, "left") == 0) {
val0 = 0; // horizontal scrolling
val1 = 0; // scrolling left
}
else if (strcmp(param, "right") == 0) {
val0 = 0; // horizontal scrolling
val1 = 1; // scrolling right
}
else if (strcmp(param, "diagonal-left") == 0) {
val0 = 1; // vertical scrolling
val1 = 0; // scrolling up
}
else if (strcmp(param, "diagonal-right") == 0) {
val0 = 1; // vertical scrolling
val1 = 1; // scrolling down
}
if (val0 == -1) {
status = oledScrollStop();
}
else if (val0 == 0) {
// horizontal scrolling
status = oledScroll(val1, OLED_EXP_SCROLL_SPEED_5_FRAMES, 0, OLED_EXP_CHAR_ROWS-1);
}
else if (val0 == 1) {
// diagonal scrolling
status = oledScrollDiagonal ( val1, // direction
OLED_EXP_SCROLL_SPEED_5_FRAMES, // scroll speed
0, // # fixed rows
OLED_EXP_HEIGHT, // # scrolling rows
1, // rows to scroll by
0, // horizontal start page
OLED_EXP_CHAR_ROWS-1 // horizontal end page
);
}
}
else {
onionPrint(ONION_SEVERITY_FATAL, "> Unrecognized command '%s'\n", command );
}
return status;
}
int main(int argc, char** argv)
{
const char *progname;
char *command;
char *param;
int status;
int verbose;
int init;
int clear;
int ch;
// set the defaults
init = 0;
clear = 0;
verbose = ONION_VERBOSITY_NORMAL;
command = malloc(MAX_COMMAND_LENGTH * sizeof *command);
param = malloc(MAX_PARAM_LENGTH * sizeof *param);
// save the program name
progname = argv[0];
//// parse the option arguments
while ((ch = getopt(argc, argv, "vqhic")) != -1) {
switch (ch) {
case 'v':
// verbose output
verbose++;
break;
case 'q':
// quiet output
verbose = ONION_VERBOSITY_NONE;
break;
case 'i':
// perform init sequence
init = 1;
break;
case 'c':
// perform clear sequence
clear = 1;
break;
default:
usage(progname);
return 0;
}
}
// set the verbosity
onionSetVerbosity(verbose);
// advance past the option arguments
argc -= optind;
argv += optind;
//// OLED PROGRAMMING
// check if OLED Expansion is present
status = oledCheckInit();
// exit the app if i2c reads fail
if (status == EXIT_FAILURE) {
onionPrint(ONION_SEVERITY_FATAL, "> ERROR: OLED Expansion not found!\n");
return 0;
}
// initialize display
if ( init == 1 ) {
status = oledDriverInit();
if (status == EXIT_FAILURE) {
onionPrint(ONION_SEVERITY_FATAL, "main-oled-exp:: display init failed!\n");
}
}
// clear screen
if ( clear == 1 ) {
onionPrint(ONION_SEVERITY_INFO, "> Clearing display\n");
status = oledClear();
if (status == EXIT_FAILURE) {
onionPrint(ONION_SEVERITY_FATAL, "main-oled-exp:: display clear failed!\n");
}
}
// check if just option command
if ( argc == 0 ) {
// check if usage needs to be printed
if ( init == 0 && clear == 0) {
usage(progname);
}
return 0;
}
//// parse the command arguments
while ( argc > 0 ) {
if(strlen(argv[0]) > MAX_COMMAND_LENGTH || strlen(argv[1]) > MAX_PARAM_LENGTH) {
// FIXME: This error needs rewording. Also, the exit status should be less funny.
onionPrint(ONION_SEVERITY_FATAL, "Unsupported parameter length\n");
exit(13);
}
// first arg - command
strcpy(command, argv[0]);
// second arg - parameter (optional)
if ( argc > 1 ) {
strcpy(param, argv[1]);
}
// perform the specified command
status = oledCommand(command, param);
if (status != EXIT_SUCCESS) {
onionPrint(ONION_SEVERITY_FATAL, "ERROR: command '%s' failed!\n", command);
}
// decrement the number of arguments left
argc -= 2;
argv += 2;
onionPrint(ONION_SEVERITY_DEBUG, "> arguments remaining: %d\n", argc);
}
return 0;
}