-
Notifications
You must be signed in to change notification settings - Fork 0
/
littledicetrng.ino
377 lines (316 loc) · 8.59 KB
/
littledicetrng.ino
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
#include <OPTIGATrustM.h>
#include <fprint.h>
#include <Adafruit_GFX.h> // Core graphics library
#include <Adafruit_ST7789.h> // Hardware-specific library for ST7789
#include <SPI.h>
#define RND_MAXLENGTH 256
#define SUPPRESSCOLLORS
#define optiga_rng_type OPTIGA_RNG_TYPE_TRNG
const int buttonPin0 = 0; //High by default, near top left corner of screen, back
int buttonState0 = 1;
int lastButtonState0 = 1;
unsigned long lastDebounceTime0 = 0;
const int buttonPin1 = 1; //Low by default, mid screen, get
int buttonState1 = 0;
int lastButtonState1 = 0;
unsigned long lastDebounceTime1 = 0;
const int buttonPin2 = 2; //Low by default, bottom of screen, next
int buttonState2 = 0;
int lastButtonState2 = 0;
unsigned long lastDebounceTime2 = 0;
unsigned long debounceDelay = 50;
unsigned int stateMachineIsh = 1;
unsigned int biggestState = 6;
unsigned int valueRange[] = {4,6,8,10,12,20,100};
bool badNum = false;
String myString = "";
int thePrintyNum = 0;
// Use dedicated hardware SPI pins
Adafruit_ST7789 tft = Adafruit_ST7789(TFT_CS, TFT_DC, TFT_RST);
float p = 3.1415926;
void setup(void) {
uint32_t ret = 0;
Serial.begin(9600);
Serial.print(F("Hello! Feather TFT Test"));
// turn on backlight
pinMode(TFT_BACKLITE, OUTPUT);
digitalWrite(TFT_BACKLITE, HIGH);
// turn on the TFT / I2C power supply
pinMode(TFT_I2C_POWER, OUTPUT);
digitalWrite(TFT_I2C_POWER, HIGH);
delay(10);
// initialize TFT
tft.init(135, 240); // Init ST7789 240x135
tft.setRotation(3);
tft.fillScreen(ST77XX_BLACK);
Serial.println(F("Initialized"));
uint16_t time = millis();
tft.fillScreen(ST77XX_BLACK);
time = millis() - time;
Serial.println(time, DEC);
delay(400);
tft.fillScreen(ST77XX_BLACK);
/*
* Initialise an OPTIGA™ Trust X Board
*/
tft.setTextSize(2);
tft.println("Begin to trust ... ");
ret = trustM.begin();
if (ret) {
tft.println("Failed");
while (true);
}
tft.println("OK");
/*
* Speedup the board (from 6 mA to 15 mA)
*/
tft.println("Setting current limit to 15 mA ... ");
ret = trustM.setCurrentLimit(8);
if (ret) {
tft.println("Failed");
while (true);
}
tft.println("OK");
delay(80);
// a single pixel
tft.drawPixel(tft.width() / 2, tft.height() / 2, ST77XX_GREEN);
delay(80);
Serial.println("done");
delay(100);
pinMode(buttonPin0, INPUT);
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
}
void buttonlabels() {
tft.fillScreen(ST77XX_BLACK);
if (buttonState0 == LOW) {
tft.fillTriangle(1, 10, 20, 1, 20, 20, ST77XX_RED);
}
else {
tft.fillTriangle(1, 10, 20, 1, 20, 20, ST77XX_WHITE);
}
if (buttonState1 == HIGH) {
tft.fillCircle(10, 68, 10, ST77XX_RED);
}
else {
tft.fillCircle(10, 68, 10, ST77XX_WHITE);
}
if (buttonState2 == HIGH) {
tft.fillTriangle(1, 115, 1, 135, 20, 125, ST77XX_RED);
}
else {
tft.fillTriangle(1, 115, 1, 135, 20, 125, ST77XX_WHITE);
}
tft.setCursor(22, 64);
tft.print(valueRange[stateMachineIsh]);
tft.setCursor(22, 120);
if (stateMachineIsh < biggestState) {
tft.print(valueRange[stateMachineIsh+1]);
}
else {
tft.print(valueRange[0]);
}
}
static void output_result(char* tag, uint8_t* in, uint16_t in_len){
tft.setCursor(0, 32);
tft.print(in_len);
tft.print(": ");
myString = "";
for (int i = 0; i < in_len; i++) {
myString += (char)in[i];
}
tft.println(myString);
badNum = true;
unsigned int theNumberIPick = 0;
uint8_t inFirst = in[theNumberIPick];
while (badNum && theNumberIPick < in_len) {
inFirst = in[theNumberIPick];
if (valueRange[stateMachineIsh] == 6) {
if (inFirst > 251) {
tft.setCursor(0, 90);
tft.println("Retrying...");
badNum = true;
}
else {
thePrintyNum = ((inFirst/42)+1);
tft.setCursor(120, 90);
tft.println(thePrintyNum);
badNum = false;
}
}
else if (valueRange[stateMachineIsh] == 8) {
thePrintyNum = ((inFirst/32)+1);
tft.setCursor(120, 90);
tft.println(thePrintyNum);
badNum = false;
}
else if (valueRange[stateMachineIsh] == 4) {
thePrintyNum = ((inFirst/64)+1);
tft.setCursor(120, 90);
tft.println(thePrintyNum);
badNum = false;
}
else if (valueRange[stateMachineIsh] == 12) {
if (inFirst > 251) {
tft.setCursor(0, 90);
tft.println("Retrying...");
badNum = true;
}
else {
thePrintyNum = ((inFirst/21)+1);
tft.setCursor(120, 90);
tft.println(thePrintyNum);
badNum = false;
}
}
else if (valueRange[stateMachineIsh] == 20) {
if (inFirst > 239) {
tft.setCursor(0, 90);
tft.println("Retrying...");
badNum = true;
}
else {
thePrintyNum = ((inFirst/12)+1);
tft.setCursor(120, 90);
tft.println(thePrintyNum);
badNum = false;
}
}
else if (valueRange[stateMachineIsh] == 10) {
if (inFirst > 249) {
tft.setCursor(0, 90);
tft.println("Retrying...");
badNum = true;
}
else {
thePrintyNum = ((inFirst/25)+1);
tft.setCursor(120, 90);
tft.println(thePrintyNum);
badNum = false;
}
}
else if (valueRange[stateMachineIsh] == 100) {
if (inFirst > 199) {
tft.setCursor(0, 90);
tft.println("Retrying...");
badNum = true;
}
else {
thePrintyNum = ((inFirst/2)+1);
tft.setCursor(120, 90);
tft.println(thePrintyNum);
badNum = false;
}
}
else {
thePrintyNum = inFirst;
tft.setCursor(120, 90);
tft.println(thePrintyNum);
badNum = false;
}
if (badNum) {
theNumberIPick++;
}
}
if (theNumberIPick == in_len) {
tft.print(" failed!");
delay(100);
}
//HEXDUMP(in, in_len);
}
static void outPrinty() {
tft.setCursor(0, 32);
tft.print(16);
tft.print(": ");
tft.println(myString);
tft.setCursor(120, 90);
tft.println(thePrintyNum);
}
/** The main loop*/
void loop() {
uint32_t ret = 0;
uint8_t cntr = 10;
uint8_t *rnd = new uint8_t[RND_MAXLENGTH];
uint32_t ts = 0;
/* Initialise Memmory Area */
memset(rnd, 0, RND_MAXLENGTH);
tft.setCursor(0, 0);
tft.fillScreen(ST77XX_BLACK);
// Get readings
int reading0 = digitalRead(buttonPin0);
int reading1 = digitalRead(buttonPin1);
int reading2 = digitalRead(buttonPin2);
// Button debouncing
if (reading0 != lastButtonState0) {
// reset the debouncing timer
lastDebounceTime0 = millis();
}
if (reading1 != lastButtonState1) {
// reset the debouncing timer
lastDebounceTime0 = millis();
}
if (reading2 != lastButtonState2) {
// reset the debouncing timer
lastDebounceTime0 = millis();
}
if ((millis() - lastDebounceTime0) > debounceDelay) {
// whatever the reading is at, it's been there for longer than the debounce
// delay, so take it as the actual current state:
// if the button state has changed:
if (reading0 != buttonState0) {
buttonState0 = reading0;
// only toggle the LED if the new button state is HIGH
if (buttonState0 == LOW) {
// ledState = !ledState;
if (stateMachineIsh == 0) {
stateMachineIsh = biggestState;
}
else {
stateMachineIsh = stateMachineIsh - 1;
}
}
}
}
if ((millis() - lastDebounceTime1) > debounceDelay) {
// whatever the reading is at, it's been there for longer than the debounce
// delay, so take it as the actual current state:
// if the button state has changed:
if (reading1 != buttonState1) {
buttonState1 = reading1;
if (buttonState1 == HIGH) {
ts = millis();
ret = trustM.getRandom(16, rnd);//OPTIGA_RNG_TYPE_TRNG
ts = millis() - ts;
if (ret) {
tft.println("Failed");
while (true);
}
trustM.getRandom(16, rnd);//OPTIGA_RNG_TYPE_TRNG
output_result("Random", rnd, 16);
}
}
}
if ((millis() - lastDebounceTime2) > debounceDelay) {
// whatever the reading is at, it's been there for longer than the debounce
// delay, so take it as the actual current state:
// if the button state has changed:
if (reading2 != buttonState2) {
buttonState2 = reading2;
if (buttonState2 == HIGH) {
if (stateMachineIsh == biggestState) {
stateMachineIsh = 0;
}
else {
stateMachineIsh++;
}
}
}
}
buttonlabels();
outPrinty();
tft.setCursor(50, 60);
delay(100);
lastButtonState0 = reading0;
lastButtonState1 = reading1;
lastButtonState2 = reading2;
}