summaryrefslogtreecommitdiff
path: root/7segment.c
blob: 4251eda118110ef1bd662d1c3e2c23eb5ecf3dc6 (plain)
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
/*
 * Raspberry Pi 4 digit 7 segment display effects without GPIO libraries
 * Copyright Daniel Jones <daniel@danieljon.es> 2018
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/*
   diagram
   =======
     A
     _
   F| |B
   G -
   E| |C
     -
     D

   GPIO memory mapping and associated snippets of code from Gert van Loo & Dom
   https://elinux.org/RPi_GPIO_Code_Samples#Direct_register_access
 */


#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <unistd.h>
#include <ctype.h>
#include <time.h>
#include <string.h>

#define BCM2708_PERI_BASE        0x20000000
#define GPIO_BASE                (BCM2708_PERI_BASE + 0x200000) /* GPIO controller */
#define PAGE_SIZE (4*1024)
#define BLOCK_SIZE (4*1024)

int  mem_fd;
void *gpio_map;

// I/O access
volatile unsigned *gpio;

// GPIO setup macros. Always use INP_GPIO(x) before using OUT_GPIO(x) or SET_GPIO_ALT(x,y)
#define INP_GPIO(g) *(gpio+((g)/10)) &= ~(7<<(((g)%10)*3))
#define OUT_GPIO(g) *(gpio+((g)/10)) |=  (1<<(((g)%10)*3))
#define SET_GPIO_ALT(g,a) *(gpio+(((g)/10))) |= (((a)<=3?(a)+4:(a)==4?3:2)<<(((g)%10)*3))

#define GPIO_SET *(gpio+7)  // sets   bits which are 1 ignores bits which are 0
#define GPIO_CLR *(gpio+10) // clears bits which are 1 ignores bits which are 0

#define GET_GPIO(g) (*(gpio+13)&(1<<g)) // 0 if LOW, (1<<g) if HIGH

#define GPIO_PULL *(gpio+37) // Pull up/pull down
#define GPIO_PULLCLK0 *(gpio+38) // Pull up/pull down clock

/*
 * pins:
 * real	represents
 * 17 	-> digit 0
 * 18 	-> digit 1
 * 27	-> digit 2
 * 22	-> digit 4
 * 23	-> segment A
 * 24	-> segment B
 * 25	-> segment C
 * 4	-> segment D
 * 2	-> segment E
 * 3	-> segment F
 * 8	-> segment G
 * 7	-> segment DOT
 */

enum segments
{
	/* refer to the diagram to know which value represents which segment */
	SEG_A 	= 1 << 0,
	SEG_B 	= 1 << 1,
	SEG_C	= 1 << 2,
	SEG_D	= 1 << 3,
	SEG_E	= 1 << 4,
	SEG_F	= 1 << 5,
	SEG_G	= 1 << 6,
	SEG_DOT	= 1 << 7,
};

enum segmentpins
{
	PIN_SELECT1 	= 17,
	PIN_SELECT2	= 18,
	PIN_SELECT3	= 27,
	PIN_SELECT4 	= 22,
	PIN_A		= 23,
	PIN_B		= 24,
	PIN_C		= 25,
	PIN_D		= 4,
	PIN_E		= 2,
	PIN_F		= 3,
	PIN_G		= 8,
	PIN_DOT		= 7,
};

enum characters
{
	CHAR_ZERO	= SEG_A | SEG_B | SEG_C | SEG_D | SEG_E | SEG_F,
	CHAR_ONE	= SEG_B | SEG_C,
	CHAR_TWO	= SEG_A | SEG_B | SEG_D | SEG_E | SEG_G,
	CHAR_THREE	= SEG_A | SEG_B | SEG_C | SEG_D | SEG_G,
	CHAR_FOUR	= SEG_B | SEG_C | SEG_F | SEG_G,
	CHAR_FIVE	= SEG_A | SEG_C | SEG_D | SEG_F | SEG_G,
	CHAR_SIX	= SEG_A | SEG_C | SEG_D | SEG_E | SEG_F | SEG_G,
	CHAR_SEVEN	= SEG_A | SEG_B | SEG_C,
	CHAR_EIGHT	= SEG_A | SEG_B | SEG_C | SEG_D | SEG_E | SEG_F | SEG_G,
	CHAR_NINE	= SEG_A | SEG_B | SEG_C | SEG_D | SEG_F | SEG_G,
	CHAR_A		= SEG_A | SEG_B | SEG_C | SEG_E | SEG_F | SEG_G,
	CHAR_B		= SEG_C | SEG_D | SEG_E | SEG_F | SEG_G,
	CHAR_C		= SEG_A | SEG_D | SEG_E | SEG_F,
	CHAR_D		= SEG_B | SEG_C | SEG_D | SEG_E | SEG_G,
	CHAR_E		= SEG_A | SEG_D | SEG_E | SEG_F | SEG_G,
	CHAR_F		= SEG_A | SEG_E | SEG_F | SEG_G,
	CHAR_G		= SEG_A | SEG_B | SEG_C | SEG_D | SEG_F | SEG_G,
	CHAR_H		= SEG_B | SEG_C | SEG_E | SEG_F | SEG_G,
	CHAR_I		= SEG_B | SEG_C,
	CHAR_J		= SEG_B | SEG_C | SEG_D | SEG_E,
	CHAR_L		= SEG_D | SEG_E | SEG_F,
	CHAR_N		= SEG_C | SEG_E | SEG_G,
	CHAR_O		= SEG_C | SEG_D | SEG_E | SEG_G,
	CHAR_P		= SEG_A | SEG_B | SEG_E | SEG_F | SEG_G,
	CHAR_R		= SEG_E | SEG_G,
	CHAR_S		= SEG_A | SEG_C | SEG_D | SEG_F | SEG_G,
	CHAR_T		= SEG_D | SEG_E | SEG_F | SEG_G,
	CHAR_U		= SEG_C | SEG_D | SEG_E,
	CHAR_Y		= SEG_B | SEG_C | SEG_D | SEG_F | SEG_G,
	CHAR_Z		= SEG_A | SEG_B | SEG_D | SEG_E | SEG_G,
	CHAR_DOT	= SEG_DOT,
};

int digits[4] =
{
	PIN_SELECT1,
	PIN_SELECT2,
	PIN_SELECT3,
	PIN_SELECT4,
};

char hexcharacters[16] =
{
	CHAR_ZERO,
	CHAR_ONE,
	CHAR_TWO,
	CHAR_THREE,
	CHAR_FOUR,
	CHAR_FIVE,
	CHAR_SIX,
	CHAR_SEVEN,
	CHAR_EIGHT,
	CHAR_NINE,
	CHAR_A,
	CHAR_B,
	CHAR_C,
	CHAR_D,
	CHAR_E,
	CHAR_F
};

char ascii[127] =
{
	[47] = 0,
	CHAR_ZERO,
	CHAR_ONE,
	CHAR_TWO,
	CHAR_THREE,
	CHAR_FOUR,
	CHAR_FIVE,
	CHAR_SIX,
	CHAR_SEVEN,
	CHAR_EIGHT,
	CHAR_NINE,
	[31] = 0,
	0, /* a space */
	[64] = 0,
	CHAR_A,
	CHAR_B,
	CHAR_C,
	CHAR_D,
	CHAR_E,
	CHAR_F,
	CHAR_G,
	CHAR_H,
	CHAR_I,
	CHAR_J,
	0,
	CHAR_L,
	0,
	CHAR_N,
	CHAR_O,
	CHAR_P,
	0,
	CHAR_R,
	CHAR_S,
	CHAR_T,
	CHAR_U,
	0,
	0,
	0,
	CHAR_Y,
	CHAR_Z,
};

void
setup_io(void)
{
	/* open /dev/gpiomem */
	if ((mem_fd = open("/dev/gpiomem", O_RDWR|O_SYNC) ) < 0) {
		printf("can't open /dev/mem \n");
		exit(-1);
	}

	/* mmap GPIO */
	gpio_map = mmap(
			NULL,             //Any adddress in our space will do
			BLOCK_SIZE,       //Map length
			PROT_READ|PROT_WRITE,// Enable reading & writting to mapped memory
			MAP_SHARED,       //Shared with other processes
			mem_fd,           //File to map
			GPIO_BASE         //Offset to GPIO peripheral
		       );

	close(mem_fd); //No need to keep mem_fd open after mmap

	if (gpio_map == MAP_FAILED) {
		printf("mmap error %d\n", (int)gpio_map);//errno also set!
		exit(-1);
	}

	// Always use volatile pointer!
	gpio = (volatile unsigned *)gpio_map;
}

void
displaycharacter(unsigned char segs)
{
	(segs & SEG_A) ? GPIO_SET = 1 << PIN_A : (GPIO_CLR = 1 << PIN_A);
	(segs & SEG_B) ? GPIO_SET = 1 << PIN_B : (GPIO_CLR = 1 << PIN_B);
	(segs & SEG_C) ? GPIO_SET = 1 << PIN_C : (GPIO_CLR = 1 << PIN_C);
	(segs & SEG_D) ? GPIO_SET = 1 << PIN_D : (GPIO_CLR = 1 << PIN_D);
	(segs & SEG_E) ? GPIO_SET = 1 << PIN_E : (GPIO_CLR = 1 << PIN_E);
	(segs & SEG_F) ? GPIO_SET = 1 << PIN_F : (GPIO_CLR = 1 << PIN_F);
	(segs & SEG_G) ? GPIO_SET = 1 << PIN_G : (GPIO_CLR = 1 << PIN_G);
	(segs & SEG_DOT) ? GPIO_SET = 1 << PIN_DOT : (GPIO_CLR = 1 << PIN_DOT);
}

void
clearpins(void)
{
	GPIO_SET = 1 << PIN_SELECT1;
	GPIO_SET = 1 << PIN_SELECT2;
	GPIO_SET = 1 << PIN_SELECT3;
	GPIO_SET = 1 << PIN_SELECT4;
	GPIO_CLR = 1 << PIN_A;
	GPIO_CLR = 1 << PIN_B;
	GPIO_CLR = 1 << PIN_C; 
	GPIO_CLR = 1 << PIN_D;
	GPIO_CLR = 1 << PIN_E;
	GPIO_CLR = 1 << PIN_F;
	GPIO_CLR = 1 << PIN_G;
	GPIO_CLR = 1 << PIN_DOT;
}

void
pinsetup(void)
{
	INP_GPIO(PIN_SELECT1);
	OUT_GPIO(PIN_SELECT1);
	INP_GPIO(PIN_SELECT2);
	OUT_GPIO(PIN_SELECT2);
	INP_GPIO(PIN_SELECT3);
	OUT_GPIO(PIN_SELECT3);
	INP_GPIO(PIN_SELECT4);
	OUT_GPIO(PIN_SELECT4);


	INP_GPIO(PIN_A);
	OUT_GPIO(PIN_A);
	INP_GPIO(PIN_B);
	OUT_GPIO(PIN_B);
	INP_GPIO(PIN_C);
	OUT_GPIO(PIN_C);
	INP_GPIO(PIN_D);
	OUT_GPIO(PIN_D);
	INP_GPIO(PIN_E);
	OUT_GPIO(PIN_E);
	INP_GPIO(PIN_F);
	OUT_GPIO(PIN_F);
	INP_GPIO(PIN_G);
	OUT_GPIO(PIN_G);
	INP_GPIO(PIN_DOT);
	OUT_GPIO(PIN_DOT);
	clearpins();
}

void
displayword(unsigned char str[4])
{
	for (int i = 0; i < 4; i++)
	{
		GPIO_SET = 1 << digits[0];
		GPIO_SET = 1 << digits[1];
		GPIO_SET = 1 << digits[2];
		GPIO_SET = 1 << digits[3];
		GPIO_CLR = 1 << digits[i];
		displaycharacter(ascii[toupper(str[i])]);
		usleep(1000);
	}
}

void
blinkword(unsigned char str[4], int repeat)
{
	clock_t start, end;
	start = clock();
	unsigned char original[5];
	unsigned char print[5];
	strcpy(original, str);
	strcpy(print, str);
	int state = 1;
	int repeats = 0;
	while (1)
	{
		if ((double)(end-start) >= 10000)
		{
			if (state)
				strcpy(print, "kkkkk");

			else
				strcpy(print, original);
			state = !state;
			if (repeat >= 0)
			{
				if (repeats >= (repeat*2)+1) /* *2 -> 5 flashes of the word */
					return;
				repeats++;
			}
			start = clock();
		}
		for (int i = 0; i < 4; i++)
		{
			GPIO_SET = 1 << digits[0];
			GPIO_SET = 1 << digits[1];
			GPIO_SET = 1 << digits[2];
			GPIO_SET = 1 << digits[3];
			GPIO_CLR = 1 << digits[i];
			displaycharacter(ascii[toupper(print[i])]);
			usleep(1000);
		}

		end = clock();
	}
}
int
converthour(int hour)
{
	switch (hour)
	{
		case 0:	 return 12;
		case 13: return 1;
		case 14: return 2;
		case 15: return 3;
		case 16: return 4;
		case 17: return 5;
		case 18: return 6;
		case 19: return 7;
		case 20: return 8;
		case 21: return 9;
		case 22: return 10;
		case 23: return 11;
		default: return hour;
	}
}

void
runclock(void)
{
	clock_t start, end;
	start = clock();
	unsigned char timenow[5] = "0000";
	while (1)
	{
		if ((double)(end-start) >= 50000)
		{
			time_t t = time(NULL);
			struct tm *tm = localtime(&t);
			if (converthour(tm->tm_hour) < 10)
				snprintf(timenow, 5, " %d%02d", converthour(tm->tm_hour), tm->tm_min);
			else
				snprintf(timenow, 5, "%02d%02d", converthour(tm->tm_hour), tm->tm_min);
			start = clock();
		}
		for (int i = 0; i < 4; i++)
		{
			GPIO_SET = 1 << digits[0];
			GPIO_SET = 1 << digits[1];
			GPIO_SET = 1 << digits[2];
			GPIO_SET = 1 << digits[3];
			GPIO_CLR = 1 << digits[i];
			displaycharacter(ascii[toupper(timenow[i])]);
			usleep(1000);
		}
		end = clock();
	}
}

void
scrollword(unsigned char *string)
{
	size_t stringlen = strlen(string);
	char buffer[5] = "     ";
	clock_t start, end;
	start = clock();
	size_t wordsize = strlen(string);
	size_t buffsize = strlen(buffer);
	size_t circbuffsize = wordsize + buffsize;
	size_t position = 0; // start at character zero of word

	while (1)
	{
		if ((double)(end-start) >= 5000)
		{
		// https://stackoverflow.com/questions/48331360/scroll-a-word-through-an-array-in-c-from-right-to-left
			// shift the buffer
			for (size_t i = 0; i < (buffsize - 1); i++)
				buffer[i] = buffer[i + 1];
			// fill in the last character
			buffer[buffsize - 1] = position < wordsize ? string[position] : 'k';
			// increment the position in the imaginary circular source text
			position = (position + 1) % circbuffsize;
			start = clock();
		}
		for (int i = 0; i < 4; i++)
		{
			GPIO_SET = 1 << digits[0];
			GPIO_SET = 1 << digits[1];
			GPIO_SET = 1 << digits[2];
			GPIO_SET = 1 << digits[3];
			GPIO_CLR = 1 << digits[i];
			displaycharacter(ascii[toupper(buffer[i])]);
			usleep(1000);
		}
		end = clock();
	}
}

int
main(int argc, char *argv[])
{
	setup_io();
	pinsetup();
	runclock();
	if (argv[1])
		scrollword(argv[1]);
	clearpins();
	return 0;
}