-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDianaTest.java
More file actions
208 lines (150 loc) · 4.17 KB
/
Copy pathDianaTest.java
File metadata and controls
208 lines (150 loc) · 4.17 KB
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
class LedRunner implements Runnable{
public void run(){
int led = 0;
int dir = 1;
int t;
while(true){
UC.gpioSetHi(3, led, true);
t = UC.clkGet();
while(UC.clkGet() - t < 1953); //10 LED switches per second
UC.gpioSetHi(3, led, false);
led += dir;
if(led < 0 || led > 7){
dir = -dir;
led += 2 * dir;
}
}
}
}
public class DianaTest implements Runnable
{
private static synchronized void lcdString(int row, int col, String s){
int i, L = s.length();
UC.lcdGotoXY(col, row);
for(i = 0; i < L; i++) UC.lcdChar(s.charAt(i));
}
private static void lcdCenteredString(int row, String s){
int L = s.length();
if(L > 16) return;
lcdString(row, (16 - s.length()) / 2, s);
}
public void run(){
int cand, t;
final String rotChars[] = {"\0", "\1", "\2", "\3"};
int rotIdx = 0;
int animCtr = 0;
//if we already ran, we'd have a candidate in the eeprom - read it
cand = 0;
for(t = 0; t < 4; t++) cand = (cand << 8) + UC.eepromRead(t);
//if cand is 0xFFFFFFFF (that is, negative 1), then we had no candidate and strat from scratch
if(cand < 0) cand = 1;
while(true){
cand++;
for(t = 2; t < cand; t++){
if((cand % t) == 0) break;
if(++animCtr == 100){
animCtr = 0;
lcdString(1, 15, rotChars[rotIdx]);
if(++rotIdx == rotChars.length) rotIdx = 0;
}
}
if(t == cand){
lcdCenteredString(1, intToString(cand));
//save the value to EEPROM
for(t = 0; t < 4; t++) UC.eepromWrite(3 - t, (byte)(cand >> (t << 3)));
}
}
}
private static void initCustomChars(){
//see here for help: http://www.quinapalus.com/hd44780udg.html
int i, j;
final byte customChars[][] = {
{0x00, 0x0E, 0x15, 0x15, 0x11, 0x0E, 0x00, 0x00},
{0x00, 0x0E, 0x11, 0x17, 0x11, 0x0E, 0x00, 0x00},
{0x00, 0x0E, 0x11, 0x15, 0x15, 0x0E, 0x00, 0x00},
{0x00, 0x0E, 0x11, 0x1D, 0x11, 0x0E, 0x00, 0x00}
};
//init custom LCD characters 1..4
UC.lcdRawWrite(0x40); //enter configuration mode
for(i = 0; i < customChars.length; i++){
for(j = 0; j < customChars[i].length; j++){
UC.lcdChar((char)customChars[i][j]);
}
}
UC.lcdRawWrite(0x80); //exit configuration mode
}
public static void main(){
int h = 0, m = 0, s = 0, d, t, L;
char hc1, hc2;
Thread primeThread, ledThread;
String time;
//init things
initCustomChars();
UC.lcdClear();
for(t = 0; t < 8; t++){
UC.gpioSetOutput(3, t, false); //use pull-ups to light leds
UC.gpioSetHi(3, t, false);
}
UC.pwmSetBri(1, 64);
UC.pwmSetBri(0, 1);
//start the other threads
primeThread = new Thread(new DianaTest());
ledThread = new Thread(new LedRunner());
//adc test
{
UC.rawMemWrite(0x7C,(byte)0x5E); //ADMUX: measure 1.1V with ref at Vcc
UC.rawMemWrite(0x7A,(byte)0xC7); //ADCSRA: ADC on, single conversion, no int, 1:128 prescaler
while((UC.rawMemRead(0x7A) & 0x40) != 0); //wait
t = UC.rawMemRead(0x79); //read result.hi
t <<= 8;
t += UC.rawMemRead(0x78); //read result.lo
t = (1024 * 1100 + (t >> 1)) / t;
lcdCenteredString(0, "Batt: " + intToString(t / 1000) + "." + intToString(t % 1000) + "V");
t = UC.clkGet();
while(UC.clkGet() - t < 39063); //wait 2 sec
}
//begin clock work
d = UC.clkGet();
while(true){
t = h % 12;
if(t == 0) t = 12;
time = intToString(t / 10) + intToString(t % 10);
time += ":" + intToString(m / 10) + intToString(m % 10);
time += ":" + intToString(s / 10) + intToString(s % 10);
time += " " + (h >= 12 ? "PM" : "AM");
lcdCenteredString(0, time);
if(60 == ++s){
s = 0;
if(60 == ++m){
m = 0;
if(24 == ++h) h = 0;
}
}
while((t = UC.clkGet()) - d < 19531);
d = d + 19531;
}
}
public static String intToString(int v){
int t;
int len = 0;
byte [] ret;
boolean neg = false;
if(v < 0){
neg = true;
len++;
v = -v;
}
t = v;
do{
len++;
t /= 10;
}while(t != 0);
ret = new byte[len];
do{
ret[--len] = (byte)((v % 10) + (int)'0');
v /= 10;
}while(v != 0);
if(neg) ret[--len] = '-';
return uj.lang.MiniString.Xnew_(ret);
}
}