/
pi.c
210 lines (167 loc) · 3.54 KB
/
pi.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
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <pigpio.h>
/* default number of pi digits to calculate */
#define N 100
/* speed for blinking the LEDs */
#define DELAY 0.002
/* segment and digit arrays are for the GPIO pins on the raspberry pi */
#define NSEGMENTS 7
const int segment[] = {
26, 19, 13, 6, 5, 0, 11
};
#define NDIGITS 4
const int digit[] = {
25, 8, 7, 1
};
/* led array contains the patterns of lights to toggle for each digit */
const int leds[10][7] = {
{1, 1, 1, 1, 1, 1, 0},
{0, 1, 1, 0, 0, 0, 0},
{1, 1, 0, 1, 1, 0, 1},
{1, 1, 1, 1, 0, 0, 1},
{0, 1, 1, 0, 0, 1, 1},
{1, 0, 1, 1, 0, 1, 1},
{1, 0, 1, 1, 1, 1, 1},
{1, 1, 1, 0, 0, 0, 0},
{1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 0, 1, 1}
};
/* array to store digits of pi as they are calculated */
int *pi = NULL;
int max = 0;
/* setup - initialize GPIO library and set up the pins for use */
void setup(void)
{
int i;
if (gpioInitialise() == PI_INIT_FAILED) {
printf("ERROR: Failed to initialize the GPIO interface.\n");
exit(1);
}
for (i = 0; i < NSEGMENTS; i++) {
gpioSetMode(segment[i], PI_OUTPUT);
gpioWrite(segment[i], PI_HIGH);
}
for (i = 0; i < NDIGITS; i++) {
gpioSetMode(digit[i], PI_OUTPUT);
gpioWrite(digit[i], PI_LOW);
}
}
/* cleanup - shut down GPIO library and pins when complete */
void cleanup(void)
{
int i;
for (i = 0; i < NSEGMENTS; i++) {
gpioWrite(segment[i], PI_HIGH);
gpioSetMode(segment[i], PI_INPUT);
}
for (i = 0; i < NDIGITS; i++) {
gpioWrite(digit[i], PI_LOW);
gpioSetMode(digit[i], PI_INPUT);
}
gpioTerminate();
}
/* drip - store digits in the pi array */
void drip(int n)
{
static int i = 0;
if (i < max) {
pi[i] = n;
i = i + 1;
}
}
/* show - display the marquee of four digits */
void show(int n)
{
int i, j;
for (j = 0; j < NDIGITS; j++) {
/* turn on digit */
gpioWrite(digit[j], PI_HIGH);
/* error check - turn off all leds */
if (pi[n + j] < 0 || pi[n + j] > 9) {
for (i = 0; i < NSEGMENTS; i++)
gpioWrite(segment[i], PI_HIGH);
} else {
for (i = 0; i < NSEGMENTS; i++)
if (leds[pi[n + j]][i])
gpioWrite(segment[i], PI_LOW);
}
time_sleep(DELAY);
/* turn off digit */
gpioWrite(digit[j], PI_LOW);
/* turn off segments */
for (i = 0; i < NSEGMENTS; i++)
gpioWrite(segment[i], PI_HIGH);
}
}
/* spigot - calculate n digits of pi */
void spigot(int n)
{
int *a;
int i, j, k, q, x;
int len = floor(10 * n / 3);
int predigit = 0;
int nines = 0;
int trim = 2;
a = (int *) malloc(len * sizeof(int));
for (j = 0; j < len; j++)
a[j] = 2;
for (j = 0; j < n; j++) {
q = 0;
for(i = len; i > 0; i--) {
x = 10 * a[i - 1] + q * i;
a[i - 1] = x % (2 * i - 1);
q = floor(x / (2 * i - 1));
}
a[0] = q % 10;
q = floor(q / 10);
if (q == 9) {
nines += 1;
} else if (q == 10) {
drip(predigit + 1);
for (k = 0; k < nines; k++)
drip(0);
predigit = 0;
nines = 0;
} else {
drip(predigit);
predigit = q;
if (nines != 0) {
for (k = 0; k < nines; k++)
drip(9);
nines = 0;
}
}
}
drip(predigit);
}
int main(int argc, char **argv)
{
int i, cycle;
/* input how many digits to compute */
if (argc < 2)
max = N;
else
max = atoi(argv[1]);
/* generate list of digits */
pi = (int *) malloc (max * sizeof (int));
if (pi == NULL) {
printf("malloc error\n");
return 1;
}
for (i = 0; i < max; i++)
pi[i] = -1;
spigot(max);
/* display as marquee */
setup();
for (i = 0; i < max - NDIGITS; i++) {
cycle = 40;
while(cycle--) {
show(i);
time_sleep(DELAY);
}
}
cleanup();
return 0;
}