![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
First Commit
Dependencies: mbed Crypto_light mbed-rtos
Spin it 2 win it
main.cpp@12:1b2e2540e4e1, 2018-03-20 (annotated)
- Committer:
- TrebleStick
- Date:
- Tue Mar 20 13:03:29 2018 +0000
- Revision:
- 12:1b2e2540e4e1
- Parent:
- 11:14ccee7c6b59
- Child:
- 13:ecccfc611025
Working Input;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
TrebleStick | 0:88c3d6c8a4eb | 1 | #include "mbed.h" |
andrebharath | 3:2e32d7974962 | 2 | #include "Crypto_light/hash/SHA256.h" |
andrebharath | 3:2e32d7974962 | 3 | #include "mbed-rtos/rtos/rtos.h" |
TrebleStick | 0:88c3d6c8a4eb | 4 | |
TrebleStick | 0:88c3d6c8a4eb | 5 | //Photointerrupter input pins |
TrebleStick | 0:88c3d6c8a4eb | 6 | #define I1pin D2 |
TrebleStick | 0:88c3d6c8a4eb | 7 | #define I2pin D11 |
TrebleStick | 0:88c3d6c8a4eb | 8 | #define I3pin D12 |
andrebharath | 9:ecef1e8cbe3d | 9 | |
TrebleStick | 0:88c3d6c8a4eb | 10 | //Incremental encoder input pins |
TrebleStick | 0:88c3d6c8a4eb | 11 | #define CHA D7 |
TrebleStick | 12:1b2e2540e4e1 | 12 | #define CHB D8 |
andrebharath | 9:ecef1e8cbe3d | 13 | |
TrebleStick | 0:88c3d6c8a4eb | 14 | //Motor Drive output pins //Mask in output byte |
TrebleStick | 0:88c3d6c8a4eb | 15 | #define L1Lpin D4 //0x01 |
TrebleStick | 0:88c3d6c8a4eb | 16 | #define L1Hpin D5 //0x02 |
TrebleStick | 0:88c3d6c8a4eb | 17 | #define L2Lpin D3 //0x04 |
TrebleStick | 0:88c3d6c8a4eb | 18 | #define L2Hpin D6 //0x08 |
TrebleStick | 0:88c3d6c8a4eb | 19 | #define L3Lpin D9 //0x10 |
TrebleStick | 0:88c3d6c8a4eb | 20 | #define L3Hpin D10 //0x20 |
TrebleStick | 0:88c3d6c8a4eb | 21 | |
andrebharath | 9:ecef1e8cbe3d | 22 | #define CHAR_ARR_SIZE 18 //Max length of input codes |
andrebharath | 3:2e32d7974962 | 23 | |
TrebleStick | 0:88c3d6c8a4eb | 24 | //Mapping from sequential drive states to motor phase outputs |
TrebleStick | 0:88c3d6c8a4eb | 25 | /* |
TrebleStick | 0:88c3d6c8a4eb | 26 | State L1 L2 L3 |
TrebleStick | 0:88c3d6c8a4eb | 27 | 0 H - L |
TrebleStick | 0:88c3d6c8a4eb | 28 | 1 - H L |
TrebleStick | 0:88c3d6c8a4eb | 29 | 2 L H - |
TrebleStick | 0:88c3d6c8a4eb | 30 | 3 L - H |
TrebleStick | 0:88c3d6c8a4eb | 31 | 4 - L H |
TrebleStick | 0:88c3d6c8a4eb | 32 | 5 H L - |
TrebleStick | 0:88c3d6c8a4eb | 33 | 6 - - - |
TrebleStick | 0:88c3d6c8a4eb | 34 | 7 - - - |
TrebleStick | 0:88c3d6c8a4eb | 35 | */ |
TrebleStick | 0:88c3d6c8a4eb | 36 | //Drive state to output table |
TrebleStick | 0:88c3d6c8a4eb | 37 | const int8_t driveTable[] = {0x12,0x18,0x09,0x21,0x24,0x06,0x00,0x00}; |
andrebharath | 9:ecef1e8cbe3d | 38 | |
TrebleStick | 0:88c3d6c8a4eb | 39 | //Mapping from interrupter inputs to sequential rotor states. 0x00 and 0x07 are not valid |
TrebleStick | 12:1b2e2540e4e1 | 40 | const int8_t stateMap[] = {0x07,0x05,0x03,0x04,0x01,0x00,0x02,0x07}; |
TrebleStick | 0:88c3d6c8a4eb | 41 | //const int8_t stateMap[] = {0x07,0x01,0x03,0x02,0x05,0x00,0x04,0x07}; //Alternative if phase order of input or drive is reversed |
andrebharath | 9:ecef1e8cbe3d | 42 | |
TrebleStick | 0:88c3d6c8a4eb | 43 | //Phase lead to make motor spin |
TrebleStick | 0:88c3d6c8a4eb | 44 | const int8_t lead = 2; //2 for forwards, -2 for backwards |
andrebharath | 9:ecef1e8cbe3d | 45 | |
andrebharath | 9:ecef1e8cbe3d | 46 | //Rotor offset at motor state 0 |
andrebharath | 9:ecef1e8cbe3d | 47 | int8_t orState = 0; |
andrebharath | 9:ecef1e8cbe3d | 48 | |
andrebharath | 9:ecef1e8cbe3d | 49 | |
TrebleStick | 12:1b2e2540e4e1 | 50 | enum MSG {MSG_RESET, MSG_HASHCOUNT, MSG_NONCE_OK, |
TrebleStick | 12:1b2e2540e4e1 | 51 | MSG_OVERFLOW, MSG_NEW_KEY, MSG_ASSIGN_KEY, MSG_TEST}; |
andrebharath | 9:ecef1e8cbe3d | 52 | |
andrebharath | 3:2e32d7974962 | 53 | //Instantiate the serial port |
andrebharath | 9:ecef1e8cbe3d | 54 | RawSerial pc(SERIAL_TX, SERIAL_RX); |
andrebharath | 9:ecef1e8cbe3d | 55 | |
andrebharath | 9:ecef1e8cbe3d | 56 | //Status LED |
andrebharath | 9:ecef1e8cbe3d | 57 | DigitalOut led1(LED1); |
andrebharath | 9:ecef1e8cbe3d | 58 | |
andrebharath | 9:ecef1e8cbe3d | 59 | //Photointerrupter inputs |
andrebharath | 9:ecef1e8cbe3d | 60 | InterruptIn I1(I1pin); |
andrebharath | 9:ecef1e8cbe3d | 61 | InterruptIn I2(I2pin); |
andrebharath | 9:ecef1e8cbe3d | 62 | InterruptIn I3(I3pin); |
andrebharath | 9:ecef1e8cbe3d | 63 | |
andrebharath | 9:ecef1e8cbe3d | 64 | //Motor Drive outputs |
andrebharath | 9:ecef1e8cbe3d | 65 | DigitalOut L1L(L1Lpin); |
andrebharath | 9:ecef1e8cbe3d | 66 | DigitalOut L1H(L1Hpin); |
andrebharath | 9:ecef1e8cbe3d | 67 | DigitalOut L2L(L2Lpin); |
andrebharath | 9:ecef1e8cbe3d | 68 | DigitalOut L2H(L2Hpin); |
andrebharath | 9:ecef1e8cbe3d | 69 | DigitalOut L3L(L3Lpin); |
andrebharath | 9:ecef1e8cbe3d | 70 | DigitalOut L3H(L3Hpin); |
andrebharath | 9:ecef1e8cbe3d | 71 | |
TrebleStick | 0:88c3d6c8a4eb | 72 | |
andrebharath | 3:2e32d7974962 | 73 | |
andrebharath | 3:2e32d7974962 | 74 | typedef struct { |
andrebharath | 3:2e32d7974962 | 75 | uint8_t code; |
andrebharath | 3:2e32d7974962 | 76 | uint32_t data; |
andrebharath | 3:2e32d7974962 | 77 | } message_t ; |
andrebharath | 3:2e32d7974962 | 78 | |
andrebharath | 3:2e32d7974962 | 79 | Mail<message_t,16> outMessages; |
andrebharath | 3:2e32d7974962 | 80 | |
TrebleStick | 12:1b2e2540e4e1 | 81 | |
TrebleStick | 12:1b2e2540e4e1 | 82 | void putMessage(uint8_t code, uint32_t data) {//uint64_t |
TrebleStick | 12:1b2e2540e4e1 | 83 | message_t *pMessage = outMessages.alloc(); |
TrebleStick | 12:1b2e2540e4e1 | 84 | pMessage->code = code; |
TrebleStick | 12:1b2e2540e4e1 | 85 | pMessage->data = data; |
TrebleStick | 12:1b2e2540e4e1 | 86 | outMessages.put(pMessage); |
TrebleStick | 12:1b2e2540e4e1 | 87 | } |
TrebleStick | 12:1b2e2540e4e1 | 88 | |
andrebharath | 3:2e32d7974962 | 89 | Thread commOutT; |
andrebharath | 3:2e32d7974962 | 90 | |
TrebleStick | 12:1b2e2540e4e1 | 91 | void commOutFn() { |
andrebharath | 3:2e32d7974962 | 92 | while(1) { |
andrebharath | 3:2e32d7974962 | 93 | osEvent newEvent = outMessages.get(); |
andrebharath | 3:2e32d7974962 | 94 | message_t *pMessage = (message_t*)newEvent.value.p; |
andrebharath | 3:2e32d7974962 | 95 | pc.printf("Message %d with data 0x%016x\r\n", |
andrebharath | 3:2e32d7974962 | 96 | pMessage->code,pMessage->data); |
andrebharath | 3:2e32d7974962 | 97 | outMessages.free(pMessage); |
andrebharath | 3:2e32d7974962 | 98 | } |
andrebharath | 3:2e32d7974962 | 99 | } |
andrebharath | 3:2e32d7974962 | 100 | |
andrebharath | 9:ecef1e8cbe3d | 101 | |
andrebharath | 9:ecef1e8cbe3d | 102 | |
TrebleStick | 12:1b2e2540e4e1 | 103 | //Global varible for the Bitcoin Key |
TrebleStick | 12:1b2e2540e4e1 | 104 | volatile uint64_t newKey = 0; //check initialise value? **** |
TrebleStick | 12:1b2e2540e4e1 | 105 | |
TrebleStick | 12:1b2e2540e4e1 | 106 | Mutex newKey_mutex; //for mutex locking |
andrebharath | 9:ecef1e8cbe3d | 107 | |
andrebharath | 9:ecef1e8cbe3d | 108 | //Instantiate a Queue to buffer incoming characters |
andrebharath | 9:ecef1e8cbe3d | 109 | Queue<void, 8> inCharQ; |
andrebharath | 9:ecef1e8cbe3d | 110 | //serial port ISR to receive each incoming byte and place into queue |
andrebharath | 9:ecef1e8cbe3d | 111 | void serialISR() { |
andrebharath | 9:ecef1e8cbe3d | 112 | uint8_t newChar = pc.getc(); |
andrebharath | 9:ecef1e8cbe3d | 113 | inCharQ.put((void*)newChar); |
andrebharath | 9:ecef1e8cbe3d | 114 | } |
andrebharath | 9:ecef1e8cbe3d | 115 | |
andrebharath | 9:ecef1e8cbe3d | 116 | |
andrebharath | 9:ecef1e8cbe3d | 117 | |
andrebharath | 9:ecef1e8cbe3d | 118 | |
andrebharath | 9:ecef1e8cbe3d | 119 | Thread decodeT; |
andrebharath | 9:ecef1e8cbe3d | 120 | |
andrebharath | 9:ecef1e8cbe3d | 121 | void setNewCmd(char newCmd[CHAR_ARR_SIZE]){ |
andrebharath | 9:ecef1e8cbe3d | 122 | //regex error checking **** |
TrebleStick | 12:1b2e2540e4e1 | 123 | |
TrebleStick | 12:1b2e2540e4e1 | 124 | //K |
andrebharath | 9:ecef1e8cbe3d | 125 | if(newCmd[0] == 'K'){ |
andrebharath | 9:ecef1e8cbe3d | 126 | newKey_mutex.lock(); |
andrebharath | 9:ecef1e8cbe3d | 127 | sscanf(newCmd, "K%x", &newKey); //Decode the command |
andrebharath | 9:ecef1e8cbe3d | 128 | newKey_mutex.unlock(); |
andrebharath | 9:ecef1e8cbe3d | 129 | putMessage(MSG_NEW_KEY, newKey); |
andrebharath | 9:ecef1e8cbe3d | 130 | } |
TrebleStick | 12:1b2e2540e4e1 | 131 | |
andrebharath | 9:ecef1e8cbe3d | 132 | //V |
andrebharath | 9:ecef1e8cbe3d | 133 | if(newCmd[0] == 'V'){ |
andrebharath | 9:ecef1e8cbe3d | 134 | //set new velocity*** |
andrebharath | 9:ecef1e8cbe3d | 135 | } |
TrebleStick | 12:1b2e2540e4e1 | 136 | |
andrebharath | 9:ecef1e8cbe3d | 137 | //R |
andrebharath | 9:ecef1e8cbe3d | 138 | if(newCmd[0] == 'R'){ |
andrebharath | 9:ecef1e8cbe3d | 139 | //set new rotation*** |
andrebharath | 9:ecef1e8cbe3d | 140 | } |
TrebleStick | 12:1b2e2540e4e1 | 141 | |
andrebharath | 9:ecef1e8cbe3d | 142 | } |
andrebharath | 9:ecef1e8cbe3d | 143 | |
andrebharath | 9:ecef1e8cbe3d | 144 | |
andrebharath | 9:ecef1e8cbe3d | 145 | void decodeFn() { |
andrebharath | 9:ecef1e8cbe3d | 146 | pc.attach(&serialISR); |
andrebharath | 9:ecef1e8cbe3d | 147 | char charSeq[CHAR_ARR_SIZE] = ""; |
andrebharath | 9:ecef1e8cbe3d | 148 | uint8_t bufPos = 0; |
andrebharath | 9:ecef1e8cbe3d | 149 | while(1) { |
TrebleStick | 12:1b2e2540e4e1 | 150 | |
andrebharath | 9:ecef1e8cbe3d | 151 | if(bufPos >= CHAR_ARR_SIZE) { |
andrebharath | 9:ecef1e8cbe3d | 152 | putMessage(MSG_OVERFLOW, bufPos); |
TrebleStick | 12:1b2e2540e4e1 | 153 | bufPos = 0; |
andrebharath | 9:ecef1e8cbe3d | 154 | break; |
andrebharath | 9:ecef1e8cbe3d | 155 | } |
TrebleStick | 12:1b2e2540e4e1 | 156 | |
andrebharath | 9:ecef1e8cbe3d | 157 | osEvent newEvent = inCharQ.get(); |
andrebharath | 9:ecef1e8cbe3d | 158 | uint8_t newChar = (uint8_t)newEvent.value.p; |
TrebleStick | 12:1b2e2540e4e1 | 159 | |
TrebleStick | 12:1b2e2540e4e1 | 160 | if(newChar == '\r' || newChar == '\n') { |
TrebleStick | 12:1b2e2540e4e1 | 161 | charSeq[bufPos] = '\0'; |
andrebharath | 9:ecef1e8cbe3d | 162 | bufPos = 0; |
andrebharath | 9:ecef1e8cbe3d | 163 | setNewCmd(charSeq); |
TrebleStick | 12:1b2e2540e4e1 | 164 | } |
andrebharath | 9:ecef1e8cbe3d | 165 | else { |
andrebharath | 9:ecef1e8cbe3d | 166 | charSeq[bufPos] = newChar; |
andrebharath | 9:ecef1e8cbe3d | 167 | bufPos++; |
TrebleStick | 12:1b2e2540e4e1 | 168 | } |
andrebharath | 9:ecef1e8cbe3d | 169 | } |
andrebharath | 9:ecef1e8cbe3d | 170 | } |
andrebharath | 9:ecef1e8cbe3d | 171 | |
andrebharath | 9:ecef1e8cbe3d | 172 | |
andrebharath | 9:ecef1e8cbe3d | 173 | |
andrebharath | 9:ecef1e8cbe3d | 174 | |
andrebharath | 9:ecef1e8cbe3d | 175 | |
andrebharath | 9:ecef1e8cbe3d | 176 | |
andrebharath | 3:2e32d7974962 | 177 | volatile uint16_t hashcount = 0; |
TrebleStick | 0:88c3d6c8a4eb | 178 | |
andrebharath | 9:ecef1e8cbe3d | 179 | void do_hashcount() { |
andrebharath | 3:2e32d7974962 | 180 | putMessage(MSG_HASHCOUNT, hashcount); |
andrebharath | 3:2e32d7974962 | 181 | hashcount = 0; |
andrebharath | 3:2e32d7974962 | 182 | } |
andrebharath | 9:ecef1e8cbe3d | 183 | |
andrebharath | 9:ecef1e8cbe3d | 184 | |
TrebleStick | 0:88c3d6c8a4eb | 185 | //Set a given drive state |
andrebharath | 9:ecef1e8cbe3d | 186 | void motorOut(int8_t driveState){ |
TrebleStick | 12:1b2e2540e4e1 | 187 | |
TrebleStick | 0:88c3d6c8a4eb | 188 | //Lookup the output byte from the drive state. |
TrebleStick | 0:88c3d6c8a4eb | 189 | int8_t driveOut = driveTable[driveState & 0x07]; |
TrebleStick | 12:1b2e2540e4e1 | 190 | |
TrebleStick | 0:88c3d6c8a4eb | 191 | //Turn off first |
TrebleStick | 0:88c3d6c8a4eb | 192 | if (~driveOut & 0x01) L1L = 0; |
TrebleStick | 0:88c3d6c8a4eb | 193 | if (~driveOut & 0x02) L1H = 1; |
TrebleStick | 0:88c3d6c8a4eb | 194 | if (~driveOut & 0x04) L2L = 0; |
TrebleStick | 0:88c3d6c8a4eb | 195 | if (~driveOut & 0x08) L2H = 1; |
TrebleStick | 0:88c3d6c8a4eb | 196 | if (~driveOut & 0x10) L3L = 0; |
TrebleStick | 0:88c3d6c8a4eb | 197 | if (~driveOut & 0x20) L3H = 1; |
TrebleStick | 12:1b2e2540e4e1 | 198 | |
TrebleStick | 0:88c3d6c8a4eb | 199 | //Then turn on |
TrebleStick | 0:88c3d6c8a4eb | 200 | if (driveOut & 0x01) L1L = 1; |
TrebleStick | 0:88c3d6c8a4eb | 201 | if (driveOut & 0x02) L1H = 0; |
TrebleStick | 0:88c3d6c8a4eb | 202 | if (driveOut & 0x04) L2L = 1; |
TrebleStick | 0:88c3d6c8a4eb | 203 | if (driveOut & 0x08) L2H = 0; |
TrebleStick | 0:88c3d6c8a4eb | 204 | if (driveOut & 0x10) L3L = 1; |
TrebleStick | 0:88c3d6c8a4eb | 205 | if (driveOut & 0x20) L3H = 0; |
andrebharath | 9:ecef1e8cbe3d | 206 | } |
TrebleStick | 12:1b2e2540e4e1 | 207 | |
andrebharath | 9:ecef1e8cbe3d | 208 | //Convert photointerrupter inputs to a rotor state |
andrebharath | 9:ecef1e8cbe3d | 209 | inline int8_t readRotorState(){ |
TrebleStick | 0:88c3d6c8a4eb | 210 | return stateMap[I1 + 2*I2 + 4*I3]; |
andrebharath | 9:ecef1e8cbe3d | 211 | } |
andrebharath | 9:ecef1e8cbe3d | 212 | |
TrebleStick | 12:1b2e2540e4e1 | 213 | //Basic synchronisation routine |
andrebharath | 9:ecef1e8cbe3d | 214 | int8_t motorHome() { |
TrebleStick | 0:88c3d6c8a4eb | 215 | //Put the motor in drive state 0 and wait for it to stabilise |
TrebleStick | 0:88c3d6c8a4eb | 216 | motorOut(0); |
andrebharath | 3:2e32d7974962 | 217 | wait(2.0); |
TrebleStick | 12:1b2e2540e4e1 | 218 | |
TrebleStick | 0:88c3d6c8a4eb | 219 | //Get the rotor state |
TrebleStick | 0:88c3d6c8a4eb | 220 | return readRotorState(); |
TrebleStick | 0:88c3d6c8a4eb | 221 | } |
andrebharath | 3:2e32d7974962 | 222 | |
andrebharath | 3:2e32d7974962 | 223 | void photointerrupter_isr() |
andrebharath | 3:2e32d7974962 | 224 | { |
andrebharath | 3:2e32d7974962 | 225 | int8_t intState = readRotorState(); |
andrebharath | 3:2e32d7974962 | 226 | motorOut((intState-orState+lead+6)%6); //+6 to make sure the remainder is positive |
andrebharath | 3:2e32d7974962 | 227 | } |
andrebharath | 9:ecef1e8cbe3d | 228 | |
TrebleStick | 12:1b2e2540e4e1 | 229 | |
TrebleStick | 0:88c3d6c8a4eb | 230 | //Main |
andrebharath | 9:ecef1e8cbe3d | 231 | int main() { |
TrebleStick | 12:1b2e2540e4e1 | 232 | |
andrebharath | 9:ecef1e8cbe3d | 233 | putMessage(MSG_RESET, 0); |
andrebharath | 9:ecef1e8cbe3d | 234 | |
TrebleStick | 12:1b2e2540e4e1 | 235 | |
andrebharath | 9:ecef1e8cbe3d | 236 | commOutT.start(&commOutFn); |
TrebleStick | 12:1b2e2540e4e1 | 237 | |
TrebleStick | 12:1b2e2540e4e1 | 238 | decodeT.start(&decodeFn); |
TrebleStick | 12:1b2e2540e4e1 | 239 | |
andrebharath | 9:ecef1e8cbe3d | 240 | // pc.printf("Hello\n\r"); |
TrebleStick | 12:1b2e2540e4e1 | 241 | |
andrebharath | 9:ecef1e8cbe3d | 242 | //Run the motor synchronisation |
andrebharath | 9:ecef1e8cbe3d | 243 | orState = motorHome(); |
andrebharath | 9:ecef1e8cbe3d | 244 | // pc.printf("Rotor origin: %x\n\r",orState); |
andrebharath | 9:ecef1e8cbe3d | 245 | //orState is subtracted from future rotor state inputs to align rotor and motor states |
TrebleStick | 12:1b2e2540e4e1 | 246 | |
andrebharath | 3:2e32d7974962 | 247 | I1.rise(&photointerrupter_isr); |
andrebharath | 3:2e32d7974962 | 248 | I2.rise(&photointerrupter_isr); |
andrebharath | 3:2e32d7974962 | 249 | I3.rise(&photointerrupter_isr); |
TrebleStick | 12:1b2e2540e4e1 | 250 | |
andrebharath | 3:2e32d7974962 | 251 | I1.fall(&photointerrupter_isr); |
andrebharath | 3:2e32d7974962 | 252 | I2.fall(&photointerrupter_isr); |
andrebharath | 3:2e32d7974962 | 253 | I3.fall(&photointerrupter_isr); |
TrebleStick | 12:1b2e2540e4e1 | 254 | |
andrebharath | 9:ecef1e8cbe3d | 255 | //Calling the ISR once starts the motor movement |
andrebharath | 9:ecef1e8cbe3d | 256 | photointerrupter_isr(); |
TrebleStick | 12:1b2e2540e4e1 | 257 | |
andrebharath | 9:ecef1e8cbe3d | 258 | SHA256 sha256; |
TrebleStick | 12:1b2e2540e4e1 | 259 | |
andrebharath | 3:2e32d7974962 | 260 | uint8_t sequence[] = {0x45,0x6D,0x62,0x65,0x64,0x64,0x65,0x64, |
andrebharath | 3:2e32d7974962 | 261 | 0x20,0x53,0x79,0x73,0x74,0x65,0x6D,0x73, |
andrebharath | 3:2e32d7974962 | 262 | 0x20,0x61,0x72,0x65,0x20,0x66,0x75,0x6E, |
andrebharath | 3:2e32d7974962 | 263 | 0x20,0x61,0x6E,0x64,0x20,0x64,0x6F,0x20, |
andrebharath | 3:2e32d7974962 | 264 | 0x61,0x77,0x65,0x73,0x6F,0x6D,0x65,0x20, |
andrebharath | 3:2e32d7974962 | 265 | 0x74,0x68,0x69,0x6E,0x67,0x73,0x21,0x20, |
andrebharath | 3:2e32d7974962 | 266 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, |
andrebharath | 3:2e32d7974962 | 267 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}; |
andrebharath | 3:2e32d7974962 | 268 | uint64_t* key = (uint64_t*)((int)sequence + 48); |
andrebharath | 3:2e32d7974962 | 269 | uint64_t* nonce = (uint64_t*)((int)sequence + 56); |
andrebharath | 3:2e32d7974962 | 270 | uint8_t hash[32]; |
TrebleStick | 12:1b2e2540e4e1 | 271 | |
andrebharath | 9:ecef1e8cbe3d | 272 | Ticker hashcounter; |
andrebharath | 9:ecef1e8cbe3d | 273 | hashcounter.attach(&do_hashcount, 1.0); |
TrebleStick | 12:1b2e2540e4e1 | 274 | |
TrebleStick | 0:88c3d6c8a4eb | 275 | //Poll the rotor state and set the motor outputs accordingly to spin the motor |
TrebleStick | 0:88c3d6c8a4eb | 276 | while (1) { |
TrebleStick | 12:1b2e2540e4e1 | 277 | |
andrebharath | 9:ecef1e8cbe3d | 278 | *key = newKey; |
TrebleStick | 12:1b2e2540e4e1 | 279 | |
TrebleStick | 12:1b2e2540e4e1 | 280 | //putMessage(MSG_ASSIGN_KEY, newKey); |
TrebleStick | 12:1b2e2540e4e1 | 281 | |
andrebharath | 9:ecef1e8cbe3d | 282 | sha256.computeHash(hash, sequence, 64); |
TrebleStick | 12:1b2e2540e4e1 | 283 | |
andrebharath | 3:2e32d7974962 | 284 | if (hash[0] == 0 && hash[1] == 0) { |
andrebharath | 3:2e32d7974962 | 285 | putMessage(MSG_NONCE_OK, *nonce); |
andrebharath | 3:2e32d7974962 | 286 | } |
andrebharath | 3:2e32d7974962 | 287 | |
andrebharath | 3:2e32d7974962 | 288 | (*nonce)++; |
andrebharath | 3:2e32d7974962 | 289 | hashcount++; |
TrebleStick | 0:88c3d6c8a4eb | 290 | } |
TrebleStick | 0:88c3d6c8a4eb | 291 | } |
andrebharath | 9:ecef1e8cbe3d | 292 | |
TrebleStick | 12:1b2e2540e4e1 | 293 | |
TrebleStick | 12:1b2e2540e4e1 | 294 | // K12345678\r |
TrebleStick | 12:1b2e2540e4e1 | 295 | // K12345678 |