fork of what I have been writing
ES_CW2_Starter_STARFISH/SerialCommunication.h@11:038d3ba0d720, 2020-03-06 (annotated)
- Committer:
- kubitz
- Date:
- Fri Mar 06 14:27:16 2020 +0000
- Revision:
- 11:038d3ba0d720
- Child:
- 13:f6e37c21d31d
File tree - restructured
Who changed what in which revision?
User | Revision | Line number | New 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 | float NewRotation; |
kubitz | 11:038d3ba0d720 | 8 | float NewSpeed; |
kubitz | 11:038d3ba0d720 | 9 | uint64_t NewKey; |
kubitz | 11:038d3ba0d720 | 10 | |
kubitz | 11:038d3ba0d720 | 11 | // Serial port methods |
kubitz | 11:038d3ba0d720 | 12 | |
kubitz | 11:038d3ba0d720 | 13 | // Decode characters in the serial buffer |
kubitz | 11:038d3ba0d720 | 14 | void decode_serial_buffer(std::string serial_buffer); |
kubitz | 11:038d3ba0d720 | 15 | // Thread that receives character one by one until \r is received |
kubitz | 11:038d3ba0d720 | 16 | void thread_processor_callback(); |
kubitz | 11:038d3ba0d720 | 17 | // ISR - Interrupt service routine for the Serial communication |
kubitz | 11:038d3ba0d720 | 18 | void serialISR(); |
kubitz | 11:038d3ba0d720 | 19 | |
kubitz | 11:038d3ba0d720 | 20 | |
kubitz | 11:038d3ba0d720 | 21 | void decode_serial_buffer(std::string serial_buffer) |
kubitz | 11:038d3ba0d720 | 22 | { |
kubitz | 11:038d3ba0d720 | 23 | command_category = serial_buffer[0]; |
kubitz | 11:038d3ba0d720 | 24 | switch (command_category) |
kubitz | 11:038d3ba0d720 | 25 | { |
kubitz | 11:038d3ba0d720 | 26 | case 'R': |
kubitz | 11:038d3ba0d720 | 27 | // Rotation command - R-?\d{1,s4}(\.\d)? |
kubitz | 11:038d3ba0d720 | 28 | Rotation_mutex.lock(); |
kubitz | 11:038d3ba0d720 | 29 | sscanf(serial_buffer.c_str(), "%c %f", &command_category, &NewRotation); |
kubitz | 11:038d3ba0d720 | 30 | pc.printf("You have a new rotation: %f \r\n", NewRotation); |
kubitz | 11:038d3ba0d720 | 31 | Rotation_mutex.unlock(); |
kubitz | 11:038d3ba0d720 | 32 | |
kubitz | 11:038d3ba0d720 | 33 | break; |
kubitz | 11:038d3ba0d720 | 34 | case 'V': |
kubitz | 11:038d3ba0d720 | 35 | // Speed command - V\d{1,3}(\.\d)? |
kubitz | 11:038d3ba0d720 | 36 | Speed_mutex.lock(); |
kubitz | 11:038d3ba0d720 | 37 | sscanf(serial_buffer.c_str(), "%c %f", &command_category, &NewSpeed); |
kubitz | 11:038d3ba0d720 | 38 | pc.printf("You have a new speed: %f \r\n", NewSpeed); |
kubitz | 11:038d3ba0d720 | 39 | Speed_mutex.unlock(); |
kubitz | 11:038d3ba0d720 | 40 | // to implement ! |
kubitz | 11:038d3ba0d720 | 41 | break; |
kubitz | 11:038d3ba0d720 | 42 | |
kubitz | 11:038d3ba0d720 | 43 | case 'K': |
kubitz | 11:038d3ba0d720 | 44 | // Bitcoin key command - K[0-9a-fA-F]{16} |
kubitz | 11:038d3ba0d720 | 45 | NewKey_mutex.lock(); |
kubitz | 11:038d3ba0d720 | 46 | sscanf(serial_buffer.c_str(), "%c %llx", &command_category, &NewKey); |
kubitz | 11:038d3ba0d720 | 47 | pc.printf("You have entered a new bitcoin key: %llu \r\n",(long long) NewKey); |
kubitz | 11:038d3ba0d720 | 48 | NewKey_mutex.unlock(); |
kubitz | 11:038d3ba0d720 | 49 | break; |
kubitz | 11:038d3ba0d720 | 50 | |
kubitz | 11:038d3ba0d720 | 51 | case 'T': |
kubitz | 11:038d3ba0d720 | 52 | // Melody command - T([A-G][#^]?[1-8]){1,16} |
kubitz | 11:038d3ba0d720 | 53 | Music_mutex.lock(); |
kubitz | 11:038d3ba0d720 | 54 | pc.printf("You have entered a new melody: --> TO IMPLEMENT <--- \r\n"); |
kubitz | 11:038d3ba0d720 | 55 | Music_mutex.unlock(); |
kubitz | 11:038d3ba0d720 | 56 | // to implement ! |
kubitz | 11:038d3ba0d720 | 57 | |
kubitz | 11:038d3ba0d720 | 58 | default: |
kubitz | 11:038d3ba0d720 | 59 | printf("Input value out of format - please try again! \r\n"); |
kubitz | 11:038d3ba0d720 | 60 | } |
kubitz | 11:038d3ba0d720 | 61 | } |
kubitz | 11:038d3ba0d720 | 62 | |
kubitz | 11:038d3ba0d720 | 63 | // Thread processor raw serial inputs: |
kubitz | 11:038d3ba0d720 | 64 | void thread_processor_callback() |
kubitz | 11:038d3ba0d720 | 65 | { |
kubitz | 11:038d3ba0d720 | 66 | while (true) |
kubitz | 11:038d3ba0d720 | 67 | { |
kubitz | 11:038d3ba0d720 | 68 | osEvent evt = inCharQ.get(); |
kubitz | 11:038d3ba0d720 | 69 | uint8_t *newChar = (uint8_t *)evt.value.p; |
kubitz | 11:038d3ba0d720 | 70 | buffer_serial += (*newChar); |
kubitz | 11:038d3ba0d720 | 71 | //Store the new character |
kubitz | 11:038d3ba0d720 | 72 | if (buffer_serial.back() == '\r') |
kubitz | 11:038d3ba0d720 | 73 | { |
kubitz | 11:038d3ba0d720 | 74 | buffer_serial += '\0'; |
kubitz | 11:038d3ba0d720 | 75 | decode_serial_buffer(buffer_serial); |
kubitz | 11:038d3ba0d720 | 76 | buffer_serial = ""; |
kubitz | 11:038d3ba0d720 | 77 | } |
kubitz | 11:038d3ba0d720 | 78 | inCharQ.free(newChar); |
kubitz | 11:038d3ba0d720 | 79 | } |
kubitz | 11:038d3ba0d720 | 80 | } |
kubitz | 11:038d3ba0d720 | 81 | |
kubitz | 11:038d3ba0d720 | 82 | void serialISR() |
kubitz | 11:038d3ba0d720 | 83 | { |
kubitz | 11:038d3ba0d720 | 84 | uint8_t *newChar = inCharQ.alloc(); |
kubitz | 11:038d3ba0d720 | 85 | *newChar = pc.getc(); |
kubitz | 11:038d3ba0d720 | 86 | inCharQ.put(newChar); |
kubitz | 11:038d3ba0d720 | 87 | } |