fork of what I have been writing
Diff: ES_CW2_Starter_STARFISH/main.cpp
- Revision:
- 10:3669e3d832ed
- Parent:
- 9:4135d0c8dc10
- Child:
- 11:038d3ba0d720
--- a/ES_CW2_Starter_STARFISH/main.cpp Wed Mar 04 13:49:46 2020 +0000 +++ b/ES_CW2_Starter_STARFISH/main.cpp Wed Mar 04 15:56:19 2020 +0000 @@ -3,20 +3,21 @@ #include "SHA256.h" #include "rtos.h" #include <stdlib.h> -#include <string.h> +#include <string> // Mail variables to pass data to threads -typedef struct -{ - uint8_t hash[32]; /* hash of successful nonce */ -} mail_t; -Mail<mail_t, 16> crypto_mail; + +Mail<uint64_t, 16> crypto_mail; Mail<uint8_t, 8> inCharQ; // Declaration of threads Thread thread_crypto; Thread thread_processor; + Mutex NewKey_mutex; +Mutex Speed_mutex; +Mutex Music_mutex; +Mutex Rotation_mutex; // Crypto mining variables uint8_t sequence[] = {0x45, 0x6D, 0x62, 0x65, 0x64, 0x64, 0x65, 0x64, @@ -34,6 +35,9 @@ uint8_t hash[32]; uint64_t NewKey; +float NewRotation; +float NewSpeed; + // Timing variables for printing calculation rate Timer timer_nonce; uint32_t previous_time; @@ -41,7 +45,7 @@ // Serial port variables // Max size of serial input is 49 + Null character char serial_buffer[50]; - +std::string buffer_serial = ""; RawSerial pc(USBTX, USBRX); char command_category; char *trimmed_serial_buffer; @@ -188,96 +192,86 @@ 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("%02x", mail->hash[i]); - printf("\n\r"); - crypto_mail.free(mail); + uint64_t *matching_nonce = (uint64_t *)evt.value.p; + pc.printf("Matching nonce found: %llu \n\r",(long long) *matching_nonce); + crypto_mail.free(matching_nonce); } } } -void decode_serial_buffer(char*serial_buffer) -{ - command_category = serial_buffer[0]; - trimmed_serial_buffer = serial_buffer + 1; +void decode_serial_buffer(std::string serial_buffer) +{ + command_category = serial_buffer[0]; switch (command_category) { case 'R': // Rotation command - R-?\d{1,s4}(\.\d)? - pc.printf("You have entered a rotational command\n"); - - // to implement ! + Rotation_mutex.lock(); + sscanf(serial_buffer.c_str(), "%c-%f", &command_category, &NewRotation); + pc.printf("You have a new rotation: %f \r\n", NewRotation); + Rotation_mutex.unlock(); break; case 'V': // Speed command - V\d{1,3}(\.\d)? - pc.printf("You have entered a speed command\n"); + Speed_mutex.lock(); + sscanf(serial_buffer.c_str(), "%c %f", &command_category, &NewSpeed); + pc.printf("You have a new speed: %f \r\n", NewSpeed); + Speed_mutex.unlock(); // 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); + sscanf(serial_buffer.c_str(), "%c %llx", &command_category, &NewKey); + pc.printf("You have entered a new bitcoin key: %llu \r\n",(long long) NewKey); NewKey_mutex.unlock(); break; case 'T': // Melody command - T([A-G][#^]?[1-8]){1,16} - pc.printf("You have entered a melody\n"); - + Music_mutex.lock(); + pc.printf("You have entered a new melody: --> TO IMPLEMENT <--- \r\n"); + Music_mutex.unlock(); // to implement ! default: - printf("Input value out of format - please try again\n"); + printf("Input value out of format - please try again! \r\n"); } } // Thread processor raw serial inputs: void thread_processor_callback() { - idx = 0; - memset(serial_buffer, 0, 50); - while (true) { osEvent evt = inCharQ.get(); - uint8_t *newChar = (uint8_t *)evt.value.p; + buffer_serial += (*newChar); //Store the new character - serial_buffer[idx] = *newChar; - idx = idx + 1; - pc.printf("inside thread index: %d", idx); - if (serial_buffer[idx-1] = '\r') + if (buffer_serial.back() == '\r') { - serial_buffer[idx] = '\0'; - decode_serial_buffer(serial_buffer); - idx = 0; - memset(serial_buffer, 0, 50); + buffer_serial += '\0'; + decode_serial_buffer(buffer_serial); + buffer_serial = ""; } inCharQ.free(newChar); - } } // Put message in Mail box for Crypto printing -void putMessageCrypto(uint8_t *hash) +void putMessageCrypto(uint64_t nonce) { - mail_t *mail = crypto_mail.alloc(); - - for (int loop = 0; loop < 32; loop++) - { - mail->hash[loop] = hash[loop]; - } + uint64_t *mail = crypto_mail.alloc(); + *mail = nonce; 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); @@ -314,13 +308,19 @@ while (1) { + // Set main as lowest priority thread + + NewKey_mutex.lock(); + *key = NewKey; + NewKey_mutex.unlock(); + SHA256::computeHash(hash, (uint8_t *)sequence, 64); *nonce = *nonce + 1; if ((hash[0] == 0) && (hash[1] == 0)) { last_nonce_number = successful_nonce; - putMessageCrypto(hash); + putMessageCrypto(*nonce); successful_nonce++; }