#include #include #include #include #include #include #include #include #include #define OLED_RESET PIN_D3 #define SET_BIT(lvalue, bit) (lvalue) |= (1 << (bit)) #define CLR_BIT(lvalue, bit) (lvalue) &= ~(1 << (bit)) Adafruit_SSD1305 display(OLED_RESET); Adafruit_Trellis matrix0 = Adafruit_Trellis(); Adafruit_TrellisSet trellis = Adafruit_TrellisSet(&matrix0); #define INTPIN PIN_D2 #define TRELLIS_ADDR 0x70 #define OLED_ADDR 0x3C #define TICKS_PER_SEC 0xF400 #define HWRNG_SAMPLE_DELAY 0x7F #define HWRNG_STATE_ADDR 0x0000 #define HWRNG_STATE_LEN 49 void InitDisplay() { Serial.println("display init"); display.begin(); display.setTextSize(1); display.setTextWrap(false); display.setTextColor(1); display.clearDisplay(); display.setCursor(20, 8); display.print("Initializing..."); display.display(); } void InitADC() { /* ADMUX = 0b01000000; ADCSRB = 0b00000000; ADCSRA = 0b11100000; DIDR0 = 0b00000001; */ } AVRTransistorNoiseSource ns; void InitHWRNG() { RNG.begin("AT90TEST", HWRNG_STATE_ADDR); RNG.addNoiseSource(ns); ADCSRA = 0b11001000; } ISR(ADC_vect) { // stir the RNG RNG.loop(); } void InitKeypad() { Serial.println("kp init"); // KEYPAD INT pin requires a pullup pinMode(INTPIN, INPUT); digitalWrite(INTPIN, HIGH); trellis.begin(TRELLIS_ADDR); trellis.readSwitches(); /*for (uint8_t i=0; i<16; i++) { trellis.setLED(i); trellis.writeDisplay(); delay(50); } // then turn them off for (uint8_t i=0; i<16; i++) { trellis.clrLED(i); trellis.writeDisplay(); delay(50); }*/ } volatile bool switch_waiting = true; volatile int ticks = 0; volatile int frames = 0, last_frames = 0; ISR(INT2_vect) { switch_waiting = true; } ISR(TIMER1_COMPA_vect) { ticks += 1; OCR1A = TICKS_PER_SEC; last_frames = frames; frames = 0; } //ISR(TIMER2_OVF_vect) { ISR(TIMER2_COMPA_vect) { //Serial.write("IT2\n"); // restart the next conversion ADCSRA |= (1 << ADSC); OCR2A = HWRNG_SAMPLE_DELAY; } void setup() { Serial.begin(9600); InitDisplay(); InitHWRNG(); InitKeypad(); EIFR |= (1 << INTF2); EIMSK &= ~(1 << INT2); // INT2 falling edge EICRA |= (1 << ISC20); EICRA &= ~(1 << ISC21); // INT2 enable EIMSK |= (1 << INT2); // set up the 16-bit timer1 for 1-second interrupts OCR1A = TICKS_PER_SEC; TCCR1A = 0b00000000; TCCR1B = 0b00001100; TIMSK1 = 0b00000010; TIMSK2 = 0b00000000; OCR2A = HWRNG_SAMPLE_DELAY; //TCCR2A = 0b00000000; TCCR2A = 0b00000010; TCCR2B = 0b00000010; TIMSK2 = 0b00000010; } char b64_to_char(uint8_t n) { n = n & 63; if(n >= 0 && n < 10) { return '0' + n; } else if(n >= 10 && n < 36) { return 'a' + (n - 10); } else if(n >= 36 && n < 62) { return 'A' + (n - 36); } else { return '-' + (n - 62); } } /* * Tries to turn 24 bits of RNG entropy into 4 alphanumeric characters. * */ bool getAlphanumCharacterBlock(char * tgtBuffer) { if(!RNG.available(3)) { return false; } uint8_t n1, n2, n3; RNG.rand(&n1, 1); RNG.rand(&n2, 1); RNG.rand(&n3, 1); } bool rng_ready = false; uint16_t rng_val = 0; void loop() { //delay(30); display.clearDisplay(); display.setCursor(0, 0); if(switch_waiting) { switch_waiting = false; trellis.readSwitches(); } /* for(uint8_t y = 0; y < 4; y++) { for(uint8_t x = 0; x < 4; x++) { if(trellis.isKeyPressed(y * 4 + x)) { display.setTextColor(0, 1); trellis.setLED(y*4+x); display.print(" "); } else { display.setTextColor(1, 0); trellis.clrLED(y*4+x); display.print(y * 4 + x, HEX); } } display.print('\n'); } */ display.setTextColor(1, 0); //display.print("0x"); display.drawFastVLine(25, 0, 32, 1); display.setCursor(28, 0); /* if(RNG.available(sizeof(uint16_t))) { rng_ready = true; RNG.rand((uint8_t*)&rng_val, sizeof(uint16_t)); } display.print("RN "); if(rng_ready) { display.print(rng_val, HEX); } else { display.print("wait..."); }*/ display.print("entropy: "); display.print(RNG.getCredits()); display.setCursor(28, 8); display.print("number: "); if(RNG.available(sizeof(uint16_t))) { trellis.setLED(5); } else { trellis.clrLED(5); } if(ADC > ns.getThreshold()) { trellis.setLED(0); trellis.setLED(3); trellis.setLED(12); trellis.setLED(15); } else { trellis.clrLED(0); trellis.clrLED(3); trellis.clrLED(12); trellis.clrLED(15); } if(trellis.justPressed(5) && RNG.available(sizeof(uint16_t))) { RNG.rand((uint8_t*)&rng_val, sizeof(uint16_t)); } display.print(rng_val, HEX); display.setCursor(28, 16); display.print(ticks); //display.print(ADC, HEX); display.setCursor(28, 24); display.print(last_frames); display.print(" FPS"); //if(switch_waiting) { trellis.writeDisplay(); //switch_waiting = false; //} display.display(); frames += 1; //Serial.println(ADC, BIN); }