fork of what I have been writing

Dependencies:   Crypto

Files at this revision

API Documentation at this revision

Comitter:
kubitz
Date:
Sun Mar 01 12:45:22 2020 +0000
Parent:
5:de6430aee646
Child:
7:aef5b29d7a7c
Commit message:
added thread for printing crypto coins

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:34:10 2020 +0000
+++ b/ES_CW2_Starter_STARFISH/main.cpp	Sun Mar 01 12:45:22 2020 +0000
@@ -1,8 +1,13 @@
 
 #include "mbed.h"
 #include "SHA256.h"
-
+#include "rtos.h"
+typedef struct {
+  uint8_t hash[32]; /* hash of successful nonce             */
+} mail_t;
 Timer timer_nonce;
+Mail<mail_t, 16> mail_box;
+Thread thread_crypto;
 
 
 uint8_t sequence[] = {0x45,0x6D,0x62,0x65,0x64,0x64,0x65,0x64,
@@ -138,6 +143,27 @@
     intStateOld = intState;
 }
 
+// Thread to print successful Hashes
+void print_crypto_thread() {
+    while (true) {
+        osEvent evt = mail_box.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);
+        }
+    }
+}
+
+// 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);
+}
+
 //Main
 int main() {
 
@@ -163,6 +189,7 @@
     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);
 
     while (1){
 
@@ -173,10 +200,7 @@
         if ((hash[0]==0)&&(hash[1]==0)){
             last_nonce_number = successful_nonce;
             successful_nonce++;
-            pc.printf("Successful Nonce -- hash: ");
-            for(int i = 0; i < 32; ++i)
-                pc.printf("%02x", hash[i]);
-            pc.printf("\n\r");
+            putMessage(hash);
             }
             
         if ((timer_nonce.read_ms()-previous_time) > 1000){