add code for trinkey qt2040 trng
This commit is contained in:
parent
5f20c50791
commit
eeb18cb3f4
4 changed files with 120 additions and 0 deletions
17
Trinkey_QT2040_TRNG_Gadget/python/trng_read_csv.py
Executable file
17
Trinkey_QT2040_TRNG_Gadget/python/trng_read_csv.py
Executable file
|
|
@ -0,0 +1,17 @@
|
|||
import serial
|
||||
|
||||
# open serial port
|
||||
ss = serial.Serial("/dev/ttyACM0")
|
||||
|
||||
# read string
|
||||
raw_string = ss.readline().strip().decode()
|
||||
|
||||
# read again if not complete
|
||||
if '' in raw_string.split(','):
|
||||
raw_string = ss.readline().strip().decode()
|
||||
|
||||
# create list of integers
|
||||
rnd_ints = [int(x) for x in raw_string.split(',')]
|
||||
|
||||
# print them
|
||||
print(rnd_ints)
|
||||
14
Trinkey_QT2040_TRNG_Gadget/python/trng_read_json.py
Executable file
14
Trinkey_QT2040_TRNG_Gadget/python/trng_read_json.py
Executable file
|
|
@ -0,0 +1,14 @@
|
|||
import json
|
||||
import serial
|
||||
|
||||
# open serial port
|
||||
ss = serial.Serial("/dev/ttyACM0")
|
||||
|
||||
# read string
|
||||
raw_string = ss.readline().strip().decode()
|
||||
|
||||
# load JSON
|
||||
json_data = json.loads(raw_string)
|
||||
|
||||
# print data
|
||||
print(json_data['trng'])
|
||||
13
Trinkey_QT2040_TRNG_Gadget/python/trng_read_raw.py
Executable file
13
Trinkey_QT2040_TRNG_Gadget/python/trng_read_raw.py
Executable file
|
|
@ -0,0 +1,13 @@
|
|||
import serial
|
||||
|
||||
# how many bytes to read?
|
||||
TRNG_SIZE = 4
|
||||
|
||||
# open serial port
|
||||
ss = serial.Serial("/dev/ttyACM0")
|
||||
|
||||
# read raw bytes
|
||||
raw_bytes = ss.read(TRNG_SIZE)
|
||||
|
||||
# print them
|
||||
print(raw_bytes)
|
||||
|
|
@ -0,0 +1,76 @@
|
|||
#include <Adafruit_NeoPixel.h>
|
||||
#include "OPTIGATrustM.h"
|
||||
|
||||
//--| User Config |-----------------------------------------------
|
||||
#define TRNG_FORMAT 2 // 0=raw, 1=CSV, 2=JSON
|
||||
#define TRNG_LENGTH 8 // random number length in bytes, 8 to 256
|
||||
#define TRNG_RATE 100 // generate new number ever X ms
|
||||
#define BEAT_RATE 1000 // neopixel heart beat rate in ms, 0=none
|
||||
#define BEAT_COLOR 0xADAF00 // neopixel heart beat color
|
||||
//----------------------------------------------------------------
|
||||
|
||||
uint8_t trng[TRNG_LENGTH];
|
||||
int current_time, last_trng, last_beat;
|
||||
|
||||
Adafruit_NeoPixel neopixel(1, PIN_NEOPIXEL, NEO_GRB + NEO_KHZ800);
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200); // USB CDC doesn't really care about baud rate
|
||||
|
||||
if (trustM.begin()) {
|
||||
Serial.println("Failed to initialize Trust M.");
|
||||
neoPanic();
|
||||
}
|
||||
|
||||
neopixel.begin();
|
||||
neopixel.fill(0);
|
||||
neopixel.show();
|
||||
|
||||
last_trng = last_beat = millis();
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
current_time = millis();
|
||||
|
||||
if (current_time - last_trng > TRNG_RATE) {
|
||||
trustM.getRandom(TRNG_LENGTH, trng);
|
||||
sendTRNG();
|
||||
last_trng = current_time;
|
||||
}
|
||||
|
||||
if ((BEAT_RATE) && (current_time - last_beat > BEAT_RATE)) {
|
||||
if (neopixel.getPixelColor(0)) {
|
||||
neopixel.fill(0);
|
||||
} else {
|
||||
neopixel.fill(BEAT_COLOR);
|
||||
}
|
||||
neopixel.show();
|
||||
last_beat = current_time;
|
||||
}
|
||||
}
|
||||
|
||||
void sendTRNG() {
|
||||
if (TRNG_FORMAT) {
|
||||
// formatted string output (CSV, JSON)
|
||||
if (TRNG_FORMAT==2) Serial.print("{\"trng\": \"");
|
||||
for (uint16_t i=0; i<TRNG_LENGTH; i++) {
|
||||
Serial.print(trng[i]);
|
||||
if (i != TRNG_LENGTH - 1) Serial.print(", ");
|
||||
}
|
||||
if (TRNG_FORMAT==2) Serial.print("\"}");
|
||||
Serial.println();
|
||||
} else {
|
||||
// raw output (bytes)
|
||||
Serial.write(trng, TRNG_LENGTH);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void neoPanic() {
|
||||
while (1) {
|
||||
neopixel.fill(0xFF0000); neopixel.show(); delay(100);
|
||||
neopixel.fill(0x000000); neopixel.show(); delay(100);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue