fork of what I have been writing

Dependencies:   Crypto

Files at this revision

API Documentation at this revision

Comitter:
kubitz
Date:
Sun Mar 01 15:02:27 2020 +0000
Parent:
6:5f4a954cb8bc
Child:
8:c30a4106d08c
Commit message:
adding rawSerial processor thread

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 12:45:22 2020 +0000
+++ b/ES_CW2_Starter_STARFISH/main.cpp	Sun Mar 01 15:02:27 2020 +0000
@@ -2,12 +2,17 @@
 #include "mbed.h"
 #include "SHA256.h"
 #include "rtos.h"
+#include<string.h>
+
 typedef struct {
   uint8_t hash[32]; /* hash of successful nonce             */
 } mail_t;
 Timer timer_nonce;
-Mail<mail_t, 16> mail_box;
+Mail<mail_t, 16> crypto_mail;
+Mail<uint8_t, 8> inCharQ;
+RawSerial pc;
 Thread thread_crypto;
+Thread thread_processor; 
 
 
 uint8_t sequence[] = {0x45,0x6D,0x62,0x65,0x64,0x64,0x65,0x64,
@@ -144,25 +149,51 @@
 }
 
 // Thread to print successful Hashes
-void print_crypto_thread() {
+void thread_crypto_print() {
     while (true) {
-        osEvent evt = mail_box.get();
+        osEvent evt = crypto_mail.get();
         if (evt.status == osEventMail) {
             mail_t *mail = (mail_t*)evt.value.p;
             for (int i = 0; i < 32; i++)
-                printf("%d", mail->hash[i]);
-            printf("/n");
-            mail_box.free(mail);
+                printf("%02x", mail->hash[i]);
+            printf("\n");
+            crypto_mail.free(mail);
         }
     }
 }
 
+// Thread processor raw serial inputs: 
+void thread_processor(){
+    while(true) {
+        osEvent newEvent = inCharQ.get();
+        if (evt.status == osEventMail){
+            uint8_t* newChar = (uint8_t*)newEvent.value.p;
+            //Store the new character
+            
+            inCharQ.free(newChar);
+        }
+        //Decode the command if it is complete
+        
+        }
+    }
+
 // Put message in Mail box 
-void putMessage(uint8_t *hash[32]){
-     mail_t *mail = mail_box.alloc();
-     strcpy(*hash, mail->hash);
-     mail_box.put(mail);
-}
+void putMessage(uint8_t* hash){
+    mail_t *mail = crypto_mail.alloc();
+    
+    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);
 
 //Main
 int main() {
@@ -172,7 +203,7 @@
     MotorPWM.pulsewidth_us(PWM_PRD);
     
     //Initialise the serial port
-    Serial pc(SERIAL_TX, SERIAL_RX);
+    RawSerial pc(SERIAL_TX, SERIAL_RX);
     pc.printf("Hello\n\r");
     
     //Run the motor synchronisation
@@ -189,8 +220,8 @@
     pc.printf("time is %d\n\r", timer_nonce.read_ms()); 
     pc.printf("time is %d\n\r", (timer_nonce.read_ms()-previous_time)); 
     uint8_t hash[32];
-    thread_crypto.start(print_crypto_thread);
-
+    thread_crypto.start(thread_crypto_print);
+    thread_processor.start(thread_processor); 
     while (1){
 
         *nonce = *nonce + 1;