fork of what I have been writing

Dependencies:   Crypto

Files at this revision

API Documentation at this revision

Comitter:
kubitz
Date:
Wed Mar 04 13:49:46 2020 +0000
Parent:
8:c30a4106d08c
Child:
10:3669e3d832ed
Commit message:
attempted to add the serial com -debugged but not working

Changed in this revision

ES_CW2_Starter_STARFISH/main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/ES_CW2_Starter_STARFISH/main.cpp	Sun Mar 01 16:07:53 2020 +0000
+++ b/ES_CW2_Starter_STARFISH/main.cpp	Wed Mar 04 13:49:46 2020 +0000
@@ -2,61 +2,73 @@
 #include "mbed.h"
 #include "SHA256.h"
 #include "rtos.h"
-#include<string.h>
+#include <stdlib.h>
+#include <string.h>
 
-typedef struct {
-  uint8_t hash[32]; /* hash of successful nonce             */
+// Mail variables to pass data to threads
+typedef struct
+{
+    uint8_t hash[32]; /* hash of successful nonce             */
 } mail_t;
-
-
-Timer timer_nonce;
 Mail<mail_t, 16> crypto_mail;
 Mail<uint8_t, 8> inCharQ;
-RawSerial pc;
+
+// Declaration of threads
 Thread thread_crypto;
-Thread thread_processor; 
-
+Thread thread_processor;
+Mutex NewKey_mutex;
 
-uint8_t sequence[] = {0x45,0x6D,0x62,0x65,0x64,0x64,0x65,0x64,
-0x20,0x53,0x79,0x73,0x74,0x65,0x6D,0x73,
-0x20,0x61,0x72,0x65,0x20,0x66,0x75,0x6E,
-0x20,0x61,0x6E,0x64,0x20,0x64,0x6F,0x20,
-0x61,0x77,0x65,0x73,0x6F,0x6D,0x65,0x20,
-0x74,0x68,0x69,0x6E,0x67,0x73,0x21,0x20,
-0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
-0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
-uint64_t* key = (uint64_t*)&sequence[48];
-uint64_t* nonce = (uint64_t*)&sequence[56];
+// Crypto mining variables
+uint8_t sequence[] = {0x45, 0x6D, 0x62, 0x65, 0x64, 0x64, 0x65, 0x64,
+                      0x20, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6D, 0x73,
+                      0x20, 0x61, 0x72, 0x65, 0x20, 0x66, 0x75, 0x6E,
+                      0x20, 0x61, 0x6E, 0x64, 0x20, 0x64, 0x6F, 0x20,
+                      0x61, 0x77, 0x65, 0x73, 0x6F, 0x6D, 0x65, 0x20,
+                      0x74, 0x68, 0x69, 0x6E, 0x67, 0x73, 0x21, 0x20,
+                      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+                      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
+uint64_t *key = (uint64_t *)&sequence[48];
+uint64_t *nonce = (uint64_t *)&sequence[56];
 uint32_t successful_nonce = 0;
 uint32_t last_nonce_number = 0;
+uint8_t hash[32];
+uint64_t NewKey;
 
-uint8_t hash[32];
+// Timing variables for printing calculation rate
+Timer timer_nonce;
+uint32_t previous_time;
+
+// Serial port variables
 // Max size of serial input is 49 + Null character
-char serial_input[50]; 
+char serial_buffer[50];
 
-uint32_t previous_time;
+RawSerial pc(USBTX, USBRX);
+char command_category;
+char *trimmed_serial_buffer;
+uint64_t extracted_value_serial_hex;
+uint8_t idx;
 //Photointerrupter input pins
 #define I1pin D3
 #define I2pin D6
 #define I3pin D5
 
 //Incremental encoder input pins
-#define CHApin   D12
-#define CHBpin   D11
+#define CHApin D12
+#define CHBpin D11
 
 //Motor Drive output pins   //Mask in output byte
-#define L1Lpin D1           //0x01
-#define L1Hpin A3           //0x02
-#define L2Lpin D0           //0x04
-#define L2Hpin A6          //0x08
-#define L3Lpin D10           //0x10
-#define L3Hpin D2          //0x20
+#define L1Lpin D1  //0x01
+#define L1Hpin A3  //0x02
+#define L2Lpin D0  //0x04
+#define L2Hpin A6  //0x08
+#define L3Lpin D10 //0x10
+#define L3Hpin D2  //0x20
 
 #define PWMpin D9
 
 //Motor current sense
-#define MCSPpin   A1
-#define MCSNpin   A0
+#define MCSPpin A1
+#define MCSNpin A0
 
 //Test outputs
 #define TP0pin D4
@@ -76,14 +88,14 @@
 7       -   -   -
 */
 //Drive state to output table
-const int8_t driveTable[] = {0x12,0x18,0x09,0x21,0x24,0x06,0x00,0x00};
+const int8_t driveTable[] = {0x12, 0x18, 0x09, 0x21, 0x24, 0x06, 0x00, 0x00};
 
 //Mapping from interrupter inputs to sequential rotor states. 0x00 and 0x07 are not valid
-const int8_t stateMap[] = {0x07,0x05,0x03,0x04,0x01,0x00,0x02,0x07};  
+const int8_t stateMap[] = {0x07, 0x05, 0x03, 0x04, 0x01, 0x00, 0x02, 0x07};
 //const int8_t stateMap[] = {0x07,0x01,0x03,0x02,0x05,0x00,0x04,0x07}; //Alternative if phase order of input or drive is reversed
 
 //Phase lead to make motor spin
-const int8_t lead = 2;  //2 for forwards, -2 for backwards
+const int8_t lead = 2; //2 for forwards, -2 for backwards
 
 //Status LED
 DigitalOut led1(LED1);
@@ -104,179 +116,221 @@
 DigitalOut TP1(TP1pin);
 PwmOut MotorPWM(PWMpin);
 
-int8_t orState = 0;    //Rotot offset at motor state 0
+int8_t orState = 0; //Rotot offset at motor state 0
 int8_t intState = 0;
 int8_t intStateOld = 0;
-    
 
 //Set a given drive state
-void motorOut(int8_t driveState){
-    
+void motorOut(int8_t driveState)
+{
+
     //Lookup the output byte from the drive state.
     int8_t driveOut = driveTable[driveState & 0x07];
-      
+
     //Turn off first
-    if (~driveOut & 0x01) L1L = 0;
-    if (~driveOut & 0x02) L1H = 1;
-    if (~driveOut & 0x04) L2L = 0;
-    if (~driveOut & 0x08) L2H = 1;
-    if (~driveOut & 0x10) L3L = 0;
-    if (~driveOut & 0x20) L3H = 1;
-    
+    if (~driveOut & 0x01)
+        L1L = 0;
+    if (~driveOut & 0x02)
+        L1H = 1;
+    if (~driveOut & 0x04)
+        L2L = 0;
+    if (~driveOut & 0x08)
+        L2H = 1;
+    if (~driveOut & 0x10)
+        L3L = 0;
+    if (~driveOut & 0x20)
+        L3H = 1;
+
     //Then turn on
-    if (driveOut & 0x01) L1L = 1;
-    if (driveOut & 0x02) L1H = 0;
-    if (driveOut & 0x04) L2L = 1;
-    if (driveOut & 0x08) L2H = 0;
-    if (driveOut & 0x10) L3L = 1;
-    if (driveOut & 0x20) L3H = 0;
-    }
-    
-    //Convert photointerrupter inputs to a rotor state
-inline int8_t readRotorState(){
-    return stateMap[I1 + 2*I2 + 4*I3];
-    }
+    if (driveOut & 0x01)
+        L1L = 1;
+    if (driveOut & 0x02)
+        L1H = 0;
+    if (driveOut & 0x04)
+        L2L = 1;
+    if (driveOut & 0x08)
+        L2H = 0;
+    if (driveOut & 0x10)
+        L3L = 1;
+    if (driveOut & 0x20)
+        L3H = 0;
+}
 
-//Basic synchronisation routine    
-int8_t motorHome() {
+//Convert photointerrupter inputs to a rotor state
+inline int8_t readRotorState()
+{
+    return stateMap[I1 + 2 * I2 + 4 * I3];
+}
+
+//Basic synchronisation routine
+int8_t motorHome()
+{
     //Put the motor in drive state 0 and wait for it to stabilise
     motorOut(0);
     wait(2.0);
-    
+
     //Get the rotor state
     return readRotorState();
 }
 
-void move() {
+void move()
+{
     intState = readRotorState();
-    motorOut((intState-orState+lead+6)%6); //+6 to make sure the remainder is positive
+    motorOut((intState - orState + lead + 6) % 6); //+6 to make sure the remainder is positive
     intStateOld = intState;
 }
 
 // Thread to print successful Hashes
-void thread_crypto_print() {
-    while (true) {
+void thread_crypto_print()
+{
+    while (true)
+    {
         osEvent evt = crypto_mail.get();
-        if (evt.status == osEventMail) {
-            mail_t *mail = (mail_t*)evt.value.p;
+        if (evt.status == osEventMail)
+        {
+            mail_t *mail = (mail_t *)evt.value.p;
             for (int i = 0; i < 32; i++)
                 printf("%02x", mail->hash[i]);
-            printf("\n");
+            printf("\n\r");
             crypto_mail.free(mail);
         }
     }
 }
 
-// Thread processor raw serial inputs: 
-void thread_processor(){
-    uint8_t index = 0; 
-    while(true) {
-        osEvent newEvent = inCharQ.get();
-        if (evt.status == osEventMail){
-            uint8_t* newChar = (uint8_t*)newEvent.value.p;
-            //Store the new character
-            serial_input[index] = *newChar; 
-            inCharQ.free(newChar);
-        }
-        //Decode the command if it is complete
-        if (serial_input[index]=='\r'){
-            char command_type;
-            *command_type = serial_input[0]; 
-            
-            switch(command_type){
-                case 'R':
-                    // Rotation command
-                    break; 
-                    
-                case 'V': 
-                    // Speed command
-                    break; 
-                    
-                case 'K':
-                    // Bitcoin key command 
-                    break;   
-                                 
-                case 'T':
-                    // Melody command
-                    break; 
-                    
-                default: 
-                    // Out of format input
-                    printf("Wrong input command - please try again"); 
-                }
+void decode_serial_buffer(char*serial_buffer)
+{
+    command_category = serial_buffer[0];
+    trimmed_serial_buffer = serial_buffer + 1;
+    switch (command_category)
+    {
+    case 'R':
+        // Rotation command - R-?\d{1,s4}(\.\d)?
+        pc.printf("You have entered a rotational command\n");
+
+        // to implement !
+
+        break;
+    case 'V':
+        // Speed command - V\d{1,3}(\.\d)?
+        pc.printf("You have entered a speed command\n");
+        // to implement !
+        break;
+
+    case 'K':
+        // Bitcoin key command - K[0-9a-fA-F]{16}
+        NewKey_mutex.lock();
+        NewKey = (uint64_t)strtoll(trimmed_serial_buffer, NULL, 16);
+        pc.printf("\nYou have entered a new bitcoin key: %llu\n", extracted_value_serial_hex);
+        NewKey_mutex.unlock();
+        break;
+
+    case 'T':
+        // Melody command - T([A-G][#^]?[1-8]){1,16}
+        pc.printf("You have entered a melody\n");
+
+        // to implement !
 
-            }
-        index++; 
-        }
+    default:
+        printf("Input value out of format - please try again\n");
     }
+}
+
+// Thread processor raw serial inputs:
+void thread_processor_callback()
+{
+    idx = 0;
+    memset(serial_buffer, 0, 50);
 
-// Put message in Mail box 
-void putMessage(uint8_t* hash){
+    while (true)
+    {
+        osEvent evt = inCharQ.get();
+        
+        uint8_t *newChar = (uint8_t *)evt.value.p;
+        //Store the new character
+        serial_buffer[idx] = *newChar;
+        idx = idx + 1;
+        pc.printf("inside thread index: %d", idx);
+        if (serial_buffer[idx-1] = '\r')
+          {
+            serial_buffer[idx] = '\0';
+            decode_serial_buffer(serial_buffer);
+            idx = 0;
+            memset(serial_buffer, 0, 50);
+            }
+        inCharQ.free(newChar);
+        
+    }
+}
+
+
+// Put message in Mail box for Crypto printing
+void putMessageCrypto(uint8_t *hash)
+{
     mail_t *mail = crypto_mail.alloc();
-    
-    for(int loop = 0; loop < 32; loop++) {
-      mail->hash[loop] = hash[loop];
+
+    for (int loop = 0; loop < 32; loop++)
+    {
+        mail->hash[loop] = hash[loop];
     }
     crypto_mail.put(mail);
-    }
+}
+
 // ISR routine to get charachter from Serial command
-void serialISR(){
-     uint8_t* newChar = inCharQ.alloc();
-     *newChar = pc.getc();
-     inCharQ.put(newChar);
- }
-// Attach interrupt routine on received character
-pc.attach(&serialISR);
+void serialISR()
+{
+    uint8_t *newChar = inCharQ.alloc();
+    *newChar = pc.getc();
+    inCharQ.put(newChar);
+}
+
+// Attach interrupt routine on serial port
 
 //Main
-int main() {
-
+int main()
+{
+    pc.attach(&serialISR);
     const int32_t PWM_PRD = 2500;
     MotorPWM.period_us(PWM_PRD);
     MotorPWM.pulsewidth_us(PWM_PRD);
-    
-    //Initialise the serial port
-    RawSerial pc(SERIAL_TX, SERIAL_RX);
+
     pc.printf("Hello\n\r");
-    
+
     //Run the motor synchronisation
     orState = motorHome();
-    pc.printf("Rotor origin: %x\n\r",orState);
-    
+    pc.printf("Rotor origin: %x\n\r", orState);
+
     I1.rise(&move);
     I1.fall(&move);
     I2.rise(&move);
     I2.fall(&move);
     I3.rise(&move);
     I3.fall(&move);
-    timer_nonce.start();
-    pc.printf("time is %d\n\r", timer_nonce.read_ms()); 
-    pc.printf("time is %d\n\r", (timer_nonce.read_ms()-previous_time)); 
 
+    // Initialize threads and timers
+    timer_nonce.start();
     thread_crypto.start(thread_crypto_print);
-    thread_processor.start(thread_processor); 
-    while (1){
+    thread_processor.start(thread_processor_callback);
+    uint8_t hash[32];
 
+    while (1)
+    {
+        SHA256::computeHash(hash, (uint8_t *)sequence, 64);
         *nonce = *nonce + 1;
 
-        SHA256::computeHash(hash, (uint8_t*)sequence, 64);
-
-        if ((hash[0]==0)&&(hash[1]==0)){
+        if ((hash[0] == 0) && (hash[1] == 0))
+        {
             last_nonce_number = successful_nonce;
+            putMessageCrypto(hash);
             successful_nonce++;
-            putMessage(hash);
-            }
-            
-        if ((timer_nonce.read_ms()-previous_time) > 1000){
-            pc.printf("Computation Rate: %lu computation /sec\n\r" , (*nonce-last_nonce_number));
+        }
+
+        if ((timer_nonce.read_ms() - previous_time) > 1000)
+        {
+            //pc.printf("Computation Rate: %lu computation /sec\n\r", (*nonce - last_nonce_number));
             last_nonce_number = *nonce;
             previous_time = timer_nonce.read_ms();
-
-            }   
-        
         }
-
-        return 0;
-    
     }
 
+    return 0;
+}