.
https://www.mediafire.com/file/sjhxgn70gxshilg/protocol_spi_pwm.png/file
protocol.cpp@111:f11575e7c79b, 2022-04-07 (annotated)
- Committer:
- voltxd
- Date:
- Thu Apr 07 12:30:01 2022 +0000
- Revision:
- 111:f11575e7c79b
- Parent:
- 110:a6d1d3525014
.;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
voltxd | 110:a6d1d3525014 | 1 | #include "protocol.h" |
voltxd | 110:a6d1d3525014 | 2 | |
voltxd | 110:a6d1d3525014 | 3 | char newDataAvailable = 0; |
voltxd | 110:a6d1d3525014 | 4 | |
voltxd | 111:f11575e7c79b | 5 | //Bytewise XOR |
voltxd | 110:a6d1d3525014 | 6 | char calculateChecksum(uint32_t propulsion, uint32_t direction) |
voltxd | 110:a6d1d3525014 | 7 | { |
voltxd | 110:a6d1d3525014 | 8 | char checksum = 0xff; |
voltxd | 110:a6d1d3525014 | 9 | checksum ^= propulsion >> 8; |
voltxd | 110:a6d1d3525014 | 10 | checksum ^= propulsion & (0b11111111); |
voltxd | 110:a6d1d3525014 | 11 | checksum ^= direction >> 8; |
voltxd | 110:a6d1d3525014 | 12 | checksum ^= direction & (0b11111111); |
voltxd | 110:a6d1d3525014 | 13 | |
voltxd | 110:a6d1d3525014 | 14 | return checksum; |
voltxd | 110:a6d1d3525014 | 15 | } |
voltxd | 110:a6d1d3525014 | 16 | |
voltxd | 110:a6d1d3525014 | 17 | char receiveState = STATE_START_OF_FRAME; |
voltxd | 110:a6d1d3525014 | 18 | char receiveIndex = 0; |
voltxd | 110:a6d1d3525014 | 19 | uint32_t receivedPropulsion; |
voltxd | 110:a6d1d3525014 | 20 | uint32_t receivedDirection; |
voltxd | 110:a6d1d3525014 | 21 | |
voltxd | 110:a6d1d3525014 | 22 | uint32_t verifiedPropulsion = 1500; |
voltxd | 110:a6d1d3525014 | 23 | uint32_t verifiedDirection = 1150; |
voltxd | 110:a6d1d3525014 | 24 | |
voltxd | 111:f11575e7c79b | 25 | //Decode the bytes received according to their order of reception. |
voltxd | 110:a6d1d3525014 | 26 | void decodeMessage(char c) |
voltxd | 110:a6d1d3525014 | 27 | { |
voltxd | 110:a6d1d3525014 | 28 | switch(receiveState) |
voltxd | 110:a6d1d3525014 | 29 | { |
voltxd | 110:a6d1d3525014 | 30 | case STATE_START_OF_FRAME: |
voltxd | 110:a6d1d3525014 | 31 | //Checking if we received the start Byte |
voltxd | 110:a6d1d3525014 | 32 | if (c == 0xff) |
voltxd | 110:a6d1d3525014 | 33 | { |
voltxd | 110:a6d1d3525014 | 34 | receiveState = STATE_PROPULSION; |
voltxd | 110:a6d1d3525014 | 35 | receiveIndex = 0; |
voltxd | 110:a6d1d3525014 | 36 | } |
voltxd | 110:a6d1d3525014 | 37 | break; |
voltxd | 110:a6d1d3525014 | 38 | |
voltxd | 110:a6d1d3525014 | 39 | case STATE_PROPULSION: |
voltxd | 110:a6d1d3525014 | 40 | //Retrieving the propulsion PWM bytes (1. MSB; 2. LSB) |
voltxd | 110:a6d1d3525014 | 41 | if (receiveIndex++ == 0) |
voltxd | 110:a6d1d3525014 | 42 | receivedPropulsion = c << 8; |
voltxd | 110:a6d1d3525014 | 43 | else |
voltxd | 110:a6d1d3525014 | 44 | { |
voltxd | 110:a6d1d3525014 | 45 | receivedPropulsion += c; |
voltxd | 110:a6d1d3525014 | 46 | receiveState = STATE_DIRECTION; |
voltxd | 110:a6d1d3525014 | 47 | receiveIndex = 0; |
voltxd | 110:a6d1d3525014 | 48 | } |
voltxd | 110:a6d1d3525014 | 49 | break; |
voltxd | 110:a6d1d3525014 | 50 | |
voltxd | 110:a6d1d3525014 | 51 | case STATE_DIRECTION: |
voltxd | 110:a6d1d3525014 | 52 | //Retrieving the direction PWM bytes (1. MSB; 2. LSB) |
voltxd | 110:a6d1d3525014 | 53 | if (receiveIndex++ == 0) |
voltxd | 110:a6d1d3525014 | 54 | receivedDirection = c << 8; |
voltxd | 110:a6d1d3525014 | 55 | else |
voltxd | 110:a6d1d3525014 | 56 | { |
voltxd | 110:a6d1d3525014 | 57 | receivedDirection += c; |
voltxd | 110:a6d1d3525014 | 58 | receiveState = STATE_CHECKSUM; |
voltxd | 110:a6d1d3525014 | 59 | } |
voltxd | 110:a6d1d3525014 | 60 | break; |
voltxd | 110:a6d1d3525014 | 61 | |
voltxd | 110:a6d1d3525014 | 62 | case STATE_CHECKSUM: |
voltxd | 110:a6d1d3525014 | 63 | //Checksum |
voltxd | 110:a6d1d3525014 | 64 | if (c == calculateChecksum(receivedPropulsion, receivedDirection)) |
voltxd | 110:a6d1d3525014 | 65 | { |
voltxd | 111:f11575e7c79b | 66 | //The frame is correct, we can use the values retrieved. |
voltxd | 110:a6d1d3525014 | 67 | verifiedPropulsion = receivedPropulsion; |
voltxd | 110:a6d1d3525014 | 68 | verifiedDirection = receivedDirection; |
voltxd | 110:a6d1d3525014 | 69 | newDataAvailable = 1; |
voltxd | 110:a6d1d3525014 | 70 | } |
voltxd | 110:a6d1d3525014 | 71 | receiveState = STATE_START_OF_FRAME; |
voltxd | 110:a6d1d3525014 | 72 | break; |
voltxd | 110:a6d1d3525014 | 73 | |
voltxd | 110:a6d1d3525014 | 74 | default: |
voltxd | 110:a6d1d3525014 | 75 | receiveState = STATE_START_OF_FRAME; |
voltxd | 110:a6d1d3525014 | 76 | break; |
voltxd | 110:a6d1d3525014 | 77 | } |
voltxd | 110:a6d1d3525014 | 78 | } |
voltxd | 110:a6d1d3525014 | 79 | |
voltxd | 111:f11575e7c79b | 80 | //Return the values decoded |
voltxd | 110:a6d1d3525014 | 81 | void getVerifiedPWMValues(uint32_t *pwmPropulsion, uint32_t *pwmDirection) |
voltxd | 110:a6d1d3525014 | 82 | { |
voltxd | 110:a6d1d3525014 | 83 | *pwmPropulsion = verifiedPropulsion; |
voltxd | 110:a6d1d3525014 | 84 | *pwmDirection = verifiedDirection; |
voltxd | 110:a6d1d3525014 | 85 | newDataAvailable = 0; |
voltxd | 110:a6d1d3525014 | 86 | } |
voltxd | 110:a6d1d3525014 | 87 | |
voltxd | 111:f11575e7c79b | 88 | //Return a value telling if new informations are available. |
voltxd | 110:a6d1d3525014 | 89 | char isDataAvailable() |
voltxd | 110:a6d1d3525014 | 90 | { |
voltxd | 110:a6d1d3525014 | 91 | return newDataAvailable; |
voltxd | 110:a6d1d3525014 | 92 | } |