-
Notifications
You must be signed in to change notification settings - Fork 0
/
flash.c
309 lines (292 loc) · 5.98 KB
/
flash.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
/* Flash an LED on GPIO 17 using /sys/class/gpio
Usage:
Run as root (so the program can access /sys/class/gpio )
flash <number of flashes> <milliseconds per flash>
Circuit description:
GPIO17 is on pin 11 on the J8 header of the Raspberry Pi
There is a ground at neighboring pin 9.
I put an LED and 270 ohm resistor in series between GPIO17 and ground.
The value of the resistor in not sensitive for this, but should probably be >=100 ohms.
Jeff Kennedy, Fall 2014
This is a test
*/
#include <stdlib.h> // needed for exit
#include <unistd.h> // needed for usleep
#include <stdio.h>
#include <ctype.h> // for making strings all uppercase (save space)
/* A routine to open a file, write a string, and close the file */
int writeOnce(char *fname, char *data){
FILE *f;
if (f=fopen(fname,"w")){
fprintf(f,"%s",data);
fclose(f);
} else {
fprintf(stderr,"Failed to open %s\n",fname);
exit(-1);
}
}
int toMorse(char *message, FILE *fvalue)
{
int T = (500*300); // T is 500*300 microseconds
int i=0, j; // iterators
int value = 0;
int limit = 6;
char ch;
// some small amount of error checking (making numbers illegal)
ch = message[i];
while(ch != '\0')
{
if((int)(ch) >= 48 && (int)(ch) <= 57)
{
fprintf(stderr, "Cannot convert numbers\n");
return 0;
}
++i;
ch = message[i];
}
// iterate through letters and use switch statement to assign appropriate times
// note: the on-flashes are odd (e.g. j=0 -> off, j=1 -> on)
i = 0;
ch = message[i];
while(ch != '\0')
{
for(j = 0; j < limit; ++j)
{
fprintf(fvalue,"%d",value);
fflush(fvalue);
switch(toupper(ch))
{
case 'A':
limit = 4;
if(j == 3)
usleep(3*T);
else
usleep(T);
value = value ^ 1;
break;
case 'B':
limit = 8;
if(j == 1)
usleep(3*T);
else
usleep(T);
value = value ^ 1;
break;
case 'C':
limit = 8;
if(j == 1 || j == 5)
usleep(3*T);
else
usleep(T);
value = value ^ 1;
break;
case 'D':
limit = 6;
if(j == 1)
usleep(3*T);
else
usleep(T);
value = value ^ 1;
break;
case 'E':
limit = 2;
usleep(T);
value = value ^ 1;
break;
case 'F':
limit = 8;
if(j == 5)
usleep(3*T);
else
usleep(T);
value = value ^ 1;
break;
case 'G':
limit = 6;
if(j == 1 || j == 3)
usleep(3*T);
else
usleep(T);
value = value ^ 1;
break;
case 'H':
limit = 8;
usleep(T);
value = value ^ 1;
break;
case 'I':
limit = 4;
usleep(T);
value = value ^ 1;
break;
case 'J':
limit = 8;
if(j == 3 || j == 5 || j == 7)
usleep(3*T);
else
usleep(T);
value = value ^ 1;
break;
case 'K':
limit = 6;
if(j == 1 || j == 5)
usleep(3*T);
else
usleep(T);
value = value ^ 1;
break;
case 'L':
limit = 8;
if(j == 3)
usleep(3*T);
else
usleep(T);
value = value ^ 1;
break;
case 'M':
limit = 4;
if(value == 1)
usleep(3*T);
else
usleep(T);
value = value ^ 1;
break;
case 'N':
limit = 4;
if(j == 1)
usleep(3*T);
else
usleep(T);
value = value ^ 1;
break;
case 'O':
limit = 6;
if(value == 1)
usleep(3*T);
else
usleep(T);
value = value ^ 1;
break;
case 'P':
limit = 8;
if(j == 3 || j == 5)
usleep(3*T);
else
usleep(T);
value = value ^ 1;
break;
case 'Q':
limit = 8;
if(value == 1 && j != 5)
usleep(3*T);
else
usleep(T);
value = value ^ 1;
break;
case 'R':
limit = 6;
if(j == 3)
usleep(3*T);
else
usleep(T);
value = value ^ 1;
break;
case 'S':
limit = 6;
usleep(T);
value = value ^ 1;
break;
case 'T':
limit = 2;
if(value == 1)
usleep(3*T);
else
usleep(T);
value = value ^ 1;
break;
case 'U':
limit = 6;
if(j == 5)
usleep(3*T);
else
usleep(T);
value = value ^ 1;
break;
case 'V':
limit = 8;
if(j == 7)
usleep(3*T);
else
usleep(T);
value = value ^ 1;
break;
case 'W':
limit = 6;
if(j == 3 || j == 5)
usleep(3*T);
else
usleep(T);
value = value ^ 1;
break;
case 'X':
limit = 8;
if(j == 1 || j == 7)
usleep(3*T);
else
usleep(T);
value = value ^ 1;
break;
case 'Y':
limit = 8;
if(value == 1 && j != 3)
usleep(3*T);
else
usleep(T);
value = value ^ 1;
break;
case 'Z':
limit = 8;
if(j == 1 || j == 3)
usleep(3*T);
else
usleep(T);
value = value ^ 1;
break;
default:
break;
}
}
++i;
ch = message[i];
value = 0;
fprintf(fvalue,"%d",value);
fflush(fvalue);
usleep(3*T); // sleep for 3T microseconds between letters in a word
}
usleep(4*T); // sleep for an additional 4T (total 7T) microseconds at end of a word
return 1;
}
#define COUNT 100
int main(int argc, char *argv[]){
FILE *fvalue;
int i;
char * message;
/* Check that there are the right number of arguments */
if (argc<2){
fprintf(stderr,"Usage: %s <message to output in morse>\n",argv[0]);
exit(-1);
}
/* Prepare GPIO */
writeOnce("/sys/class/gpio/export","17"); /* export GPIO17*/
writeOnce("/sys/class/gpio/gpio17/direction","out");/* CPIO17 is to be output*/
/* Open GPIO17 value "file" */
fvalue=fopen("/sys/class/gpio/gpio17/value","w");
/*get the parameters from the command line */
for(i = 1; i < argc; i++)
{
message = argv[1];
toMorse(message, fvalue);
}
fclose(fvalue);
writeOnce("/sys/class/gpio/unexport","17"); /* unexport gpio 17 */
}