fork of what I have been writing

Dependencies:   Crypto

Committer:
le1917
Date:
Mon Mar 09 17:19:39 2020 +0000
Revision:
13:f6e37c21d31d
Parent:
11:038d3ba0d720
Thread start stopping not working;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kubitz 11:038d3ba0d720 1
kubitz 11:038d3ba0d720 2 // Serial port variables
kubitz 11:038d3ba0d720 3 std::string buffer_serial = "";
kubitz 11:038d3ba0d720 4 RawSerial pc(USBTX, USBRX);
kubitz 11:038d3ba0d720 5 char command_category;
kubitz 11:038d3ba0d720 6 Mail<uint8_t, 8> inCharQ;
kubitz 11:038d3ba0d720 7
kubitz 11:038d3ba0d720 8 // Serial port methods
kubitz 11:038d3ba0d720 9
kubitz 11:038d3ba0d720 10 // Decode characters in the serial buffer
kubitz 11:038d3ba0d720 11 void decode_serial_buffer(std::string serial_buffer);
kubitz 11:038d3ba0d720 12 // Thread that receives character one by one until \r is received
kubitz 11:038d3ba0d720 13 void thread_processor_callback();
kubitz 11:038d3ba0d720 14 // ISR - Interrupt service routine for the Serial communication
kubitz 11:038d3ba0d720 15 void serialISR();
kubitz 11:038d3ba0d720 16
kubitz 11:038d3ba0d720 17
kubitz 11:038d3ba0d720 18 void decode_serial_buffer(std::string serial_buffer)
kubitz 11:038d3ba0d720 19 {
kubitz 11:038d3ba0d720 20 command_category = serial_buffer[0];
kubitz 11:038d3ba0d720 21 switch (command_category)
kubitz 11:038d3ba0d720 22 {
kubitz 11:038d3ba0d720 23 case 'R':
kubitz 11:038d3ba0d720 24 // Rotation command - R-?\d{1,s4}(\.\d)?
le1917 13:f6e37c21d31d 25 NewMotorCommand_mutex.lock();
kubitz 11:038d3ba0d720 26 sscanf(serial_buffer.c_str(), "%c %f", &command_category, &NewRotation);
kubitz 11:038d3ba0d720 27 pc.printf("You have a new rotation: %f \r\n", NewRotation);
le1917 13:f6e37c21d31d 28 NewMotorCommand_mutex.unlock();
kubitz 11:038d3ba0d720 29
kubitz 11:038d3ba0d720 30 break;
kubitz 11:038d3ba0d720 31 case 'V':
kubitz 11:038d3ba0d720 32 // Speed command - V\d{1,3}(\.\d)?
le1917 13:f6e37c21d31d 33 NewMotorCommand_mutex.lock();
kubitz 11:038d3ba0d720 34 sscanf(serial_buffer.c_str(), "%c %f", &command_category, &NewSpeed);
kubitz 11:038d3ba0d720 35 pc.printf("You have a new speed: %f \r\n", NewSpeed);
le1917 13:f6e37c21d31d 36 NewSpeed_flag = true;
le1917 13:f6e37c21d31d 37 NewMotorCommand_mutex.unlock();
kubitz 11:038d3ba0d720 38 // to implement !
kubitz 11:038d3ba0d720 39 break;
kubitz 11:038d3ba0d720 40
kubitz 11:038d3ba0d720 41 case 'K':
kubitz 11:038d3ba0d720 42 // Bitcoin key command - K[0-9a-fA-F]{16}
kubitz 11:038d3ba0d720 43 NewKey_mutex.lock();
kubitz 11:038d3ba0d720 44 sscanf(serial_buffer.c_str(), "%c %llx", &command_category, &NewKey);
kubitz 11:038d3ba0d720 45 pc.printf("You have entered a new bitcoin key: %llu \r\n",(long long) NewKey);
kubitz 11:038d3ba0d720 46 NewKey_mutex.unlock();
kubitz 11:038d3ba0d720 47 break;
kubitz 11:038d3ba0d720 48
kubitz 11:038d3ba0d720 49 case 'T':
kubitz 11:038d3ba0d720 50 // Melody command - T([A-G][#^]?[1-8]){1,16}
kubitz 11:038d3ba0d720 51 Music_mutex.lock();
kubitz 11:038d3ba0d720 52 pc.printf("You have entered a new melody: --> TO IMPLEMENT <--- \r\n");
kubitz 11:038d3ba0d720 53 Music_mutex.unlock();
kubitz 11:038d3ba0d720 54 // to implement !
le1917 13:f6e37c21d31d 55
kubitz 11:038d3ba0d720 56 default:
kubitz 11:038d3ba0d720 57 printf("Input value out of format - please try again! \r\n");
kubitz 11:038d3ba0d720 58 }
kubitz 11:038d3ba0d720 59 }
kubitz 11:038d3ba0d720 60
kubitz 11:038d3ba0d720 61 // Thread processor raw serial inputs:
kubitz 11:038d3ba0d720 62 void thread_processor_callback()
kubitz 11:038d3ba0d720 63 {
kubitz 11:038d3ba0d720 64 while (true)
kubitz 11:038d3ba0d720 65 {
kubitz 11:038d3ba0d720 66 osEvent evt = inCharQ.get();
kubitz 11:038d3ba0d720 67 uint8_t *newChar = (uint8_t *)evt.value.p;
kubitz 11:038d3ba0d720 68 buffer_serial += (*newChar);
kubitz 11:038d3ba0d720 69 //Store the new character
kubitz 11:038d3ba0d720 70 if (buffer_serial.back() == '\r')
kubitz 11:038d3ba0d720 71 {
kubitz 11:038d3ba0d720 72 buffer_serial += '\0';
kubitz 11:038d3ba0d720 73 decode_serial_buffer(buffer_serial);
kubitz 11:038d3ba0d720 74 buffer_serial = "";
kubitz 11:038d3ba0d720 75 }
kubitz 11:038d3ba0d720 76 inCharQ.free(newChar);
kubitz 11:038d3ba0d720 77 }
kubitz 11:038d3ba0d720 78 }
kubitz 11:038d3ba0d720 79
kubitz 11:038d3ba0d720 80 void serialISR()
kubitz 11:038d3ba0d720 81 {
kubitz 11:038d3ba0d720 82 uint8_t *newChar = inCharQ.alloc();
kubitz 11:038d3ba0d720 83 *newChar = pc.getc();
kubitz 11:038d3ba0d720 84 inCharQ.put(newChar);
kubitz 11:038d3ba0d720 85 }