/
mm7segment.c
189 lines (162 loc) · 4.36 KB
/
mm7segment.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
// http://olimex.wordpress.com/2012/09/11/imx233-olinuxino-gpios-faster-and-faster/
// http://www.jann.cc/2013/05/04/imx233_olinuxino_current_state.html
// gcc -o mm7segment mm7segment.c
#include "gpio-mmap.h"
#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include <inttypes.h>
#include <stdlib.h>
#include <stdbool.h>
#include <sched.h>
#include <string.h>
#define BBGPIO_01 0,1 // where BB stands for Bank Bit
#define BBGPIO_02 0,2
#define BBGPIO_04 0,4
#define BBGPIO_05 0,5
#define BBGPIO_06 0,6
#define BBGPIO_07 0,7
#define BBGPIO_16 0,16
#define BBGPIO_51 1,19
///bit number 76543210 least significant 7 bits contain the decoder
// hgfedcba
#define SSDISP_0 0x3F // 0b00111111
#define SSDISP_1 0x06 // 0b00000110
#define SSDISP_2 0x5B // 0b01011011
#define SSDISP_3 0x4F // 0b01001111
#define SSDISP_4 0x66 // 0b01100110
#define SSDISP_5 0x6D // 0b01101101
#define SSDISP_6 0x7D // 0b01111101
#define SSDISP_7 0x07 // 0b00000111
#define SSDISP_8 0x7F // 0b01111111
#define SSDISP_9 0x6F // 0b01101111
#define SSDISP_a 0b00000001
#define SSDISP_b 0b00000010
#define SSDISP_c 0b00000100
#define SSDISP_d 0b00001000
#define SSDISP_e 0b00010000
#define SSDISP_f 0b00100000
#define SSDISP_g 0b01000000
#define SSDISP_h 0b10000000
const char ssdisp[10] = {
SSDISP_0,
SSDISP_1,
SSDISP_2,
SSDISP_3,
SSDISP_4,
SSDISP_5,
SSDISP_6,
SSDISP_7,
SSDISP_8,
SSDISP_9
};
enum ssdigit {
eSEGA,
eSEGB,
eSEGC,
eSEGD,
eSEGE,
eSEGF,
eSEGG,
eSEGH,
eSEGDP,
item_count
};
typedef int digit [item_count];
//digit digit1; //= {16,7,6,5,4,51,2,1};
const digit digit1 = {16,7,6,5,4,51,2,1};
const unsigned int PWMTable[] = {
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 13,
16, 19, 23, 26, 29, 32, 35, 39, 43, 47, 51, 55, 60, 66,
71, 77, 84, 91, 98, 106, 114, 123, 133, 143, 154, 166,
179, 192, 207, 222, 239, 257, 276, 296, 317, 341, 366,
392, 421, 451, 483, 518, 555, 595, 638, 684, 732, 784,
840, 900, 964, 1032, 1105, 1184, 1267, 1357, 1453, 1555,
1665, 1782, 1907, 2042, 2185, 2339, 2503, 2679, 2867, 3069,
3284, 3514, 3761, 4024, 4096};
const unsigned char max_brightness = 55;
void initialise_io() {
gpio_map();
gpio_output(BBGPIO_01);
gpio_output(BBGPIO_02);
gpio_output(BBGPIO_04);
gpio_output(BBGPIO_05);
gpio_output(BBGPIO_06);
gpio_output(BBGPIO_07);
gpio_output(BBGPIO_16);
gpio_output(BBGPIO_51);
}
// turn off all segments and dp of a digit
void initialise_digit(const digit ldigit) {
for ( int x = 0; x < item_count; x++) {
GPIO_WRITE_PIN(ldigit[x],0);
}
}
int display_number(const digit ldigig, const unsigned char num) {
/* takes a digit of type digit, and a number 0-9 and displays that number on the digit */
if ((num < 0) || (num > 9) ) {
//printf("display_number can only accept num from 0-9\n");
return 1;
} else {
for ( int x = 0; x < eSEGH; x++) {
GPIO_WRITE_PIN(digit1[x],(ssdisp[num] >> x) & 1);
//printf("displayed %d\n",num);
}
}
return 0;
}
#define sleepon 5
#define sleepoff 1995
int main() {
/* struct sched_param sp; */
/* memset( &sp, 0, sizeof(sp) ); */
/* sp.sched_priority = 99; */
/* sched_setscheduler( 0, SCHED_FIFO, &sp ); */
uint16_t pwm_counter = 0;
time_t rawtime;
struct tm * timeinfo;
initialise_io();
initialise_digit(digit1);
/* initialise_digit(digit2); */
/* initialise_digit(digit3); */
/* initialise_digit(digit4); */
#ifdef gperf
uint32_t loop_counter = 0;
#endif
char cursecond = 0;
char oldsecond = 11; //out of usual bounds
bool displayed = false;
while (1) {
// get current time
time ( &rawtime );
timeinfo = localtime ( &rawtime );
cursecond = (timeinfo->tm_sec) % 10;
if (pwm_counter < PWMTable[10]) {
// display last digit of seconds of current time
if (!displayed) {
display_number (digit1,(cursecond));
displayed = true;
}
oldsecond = cursecond;
} else {
if (displayed) {
initialise_digit (digit1);
displayed = false;
}
};
// reset counter to 0
if (pwm_counter++ > PWMTable[max_brightness]) {
pwm_counter = 0;
//printf("reset pwm_counter from %u to 0\n",pwm_counter-1);
};
#ifdef gperf
if (loop_counter++ > 6000000) {
exit(0); };
#endif
}
}
/*
# Local Variables:
# compile-command: "make mm7segment"
# End:
*/