update protocol.h
Fork of Fork_Boss_Communication_Robot by
Utilities.cpp@0:0c88691e3904, 2015-03-01 (annotated)
- Committer:
- soulx
- Date:
- Sun Mar 01 06:28:01 2015 +0000
- Revision:
- 0:0c88691e3904
create lib communication for robot
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
soulx | 0:0c88691e3904 | 1 | /* Copyright (c) 2012 Georgios Petrou, MIT License |
soulx | 0:0c88691e3904 | 2 | * |
soulx | 0:0c88691e3904 | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
soulx | 0:0c88691e3904 | 4 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
soulx | 0:0c88691e3904 | 5 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
soulx | 0:0c88691e3904 | 6 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
soulx | 0:0c88691e3904 | 7 | * furnished to do so, subject to the following conditions: |
soulx | 0:0c88691e3904 | 8 | * |
soulx | 0:0c88691e3904 | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
soulx | 0:0c88691e3904 | 10 | * substantial portions of the Software. |
soulx | 0:0c88691e3904 | 11 | * |
soulx | 0:0c88691e3904 | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
soulx | 0:0c88691e3904 | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
soulx | 0:0c88691e3904 | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
soulx | 0:0c88691e3904 | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
soulx | 0:0c88691e3904 | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
soulx | 0:0c88691e3904 | 17 | */ |
soulx | 0:0c88691e3904 | 18 | |
soulx | 0:0c88691e3904 | 19 | #include "Utilities.h" |
soulx | 0:0c88691e3904 | 20 | |
soulx | 0:0c88691e3904 | 21 | uint8_t Utilities::GetCheckSum(const uint8_t *data, uint8_t length) |
soulx | 0:0c88691e3904 | 22 | { |
soulx | 0:0c88691e3904 | 23 | uint8_t checkSum = 0; |
soulx | 0:0c88691e3904 | 24 | |
soulx | 0:0c88691e3904 | 25 | for(int i = 0; i < length; i++) |
soulx | 0:0c88691e3904 | 26 | checkSum += data[i]; |
soulx | 0:0c88691e3904 | 27 | |
soulx | 0:0c88691e3904 | 28 | checkSum = ~checkSum; |
soulx | 0:0c88691e3904 | 29 | |
soulx | 0:0c88691e3904 | 30 | return checkSum; |
soulx | 0:0c88691e3904 | 31 | } |
soulx | 0:0c88691e3904 | 32 | |
soulx | 0:0c88691e3904 | 33 | void Utilities::ConvertUInt16ToUInt8Array(uint16_t value, uint8_t* data) |
soulx | 0:0c88691e3904 | 34 | { |
soulx | 0:0c88691e3904 | 35 | data[0] = value; |
soulx | 0:0c88691e3904 | 36 | data[1] = value >> 8; |
soulx | 0:0c88691e3904 | 37 | } |
soulx | 0:0c88691e3904 | 38 | |
soulx | 0:0c88691e3904 | 39 | void Utilities::ConvertInt16ToUInt8Array(int16_t value, uint8_t* data) |
soulx | 0:0c88691e3904 | 40 | { |
soulx | 0:0c88691e3904 | 41 | data[0] = value; |
soulx | 0:0c88691e3904 | 42 | data[1] = value >> 8; |
soulx | 0:0c88691e3904 | 43 | } |
soulx | 0:0c88691e3904 | 44 | |
soulx | 0:0c88691e3904 | 45 | void Utilities::ConvertUInt32ToUInt8Array(uint32_t value, uint8_t* data) |
soulx | 0:0c88691e3904 | 46 | { |
soulx | 0:0c88691e3904 | 47 | data[0] = value; |
soulx | 0:0c88691e3904 | 48 | data[1] = value >> 8; |
soulx | 0:0c88691e3904 | 49 | data[2] = value >> 16; |
soulx | 0:0c88691e3904 | 50 | data[3] = value >> 24; |
soulx | 0:0c88691e3904 | 51 | } |
soulx | 0:0c88691e3904 | 52 | |
soulx | 0:0c88691e3904 | 53 | void Utilities::ConvertInt32ToUInt8Array(int32_t value, uint8_t* data) |
soulx | 0:0c88691e3904 | 54 | { |
soulx | 0:0c88691e3904 | 55 | data[0] = value; |
soulx | 0:0c88691e3904 | 56 | data[1] = value >> 8; |
soulx | 0:0c88691e3904 | 57 | data[2] = value >> 16; |
soulx | 0:0c88691e3904 | 58 | data[3] = value >> 24; |
soulx | 0:0c88691e3904 | 59 | } |
soulx | 0:0c88691e3904 | 60 | |
soulx | 0:0c88691e3904 | 61 | uint16_t Utilities::ConvertUInt8ArrayToUInt16(uint8_t* data) |
soulx | 0:0c88691e3904 | 62 | { |
soulx | 0:0c88691e3904 | 63 | return (((uint16_t)data[1]) << 8) | ((uint16_t)data[0]); |
soulx | 0:0c88691e3904 | 64 | } |
soulx | 0:0c88691e3904 | 65 | |
soulx | 0:0c88691e3904 | 66 | int16_t Utilities::ConvertUInt8ArrayToInt16(uint8_t* data) |
soulx | 0:0c88691e3904 | 67 | { |
soulx | 0:0c88691e3904 | 68 | return (((int16_t)data[1]) << 8) | ((int16_t)data[0]); |
soulx | 0:0c88691e3904 | 69 | } |
soulx | 0:0c88691e3904 | 70 | |
soulx | 0:0c88691e3904 | 71 | int32_t Utilities::ConvertUInt8ArrayToInt32(uint8_t* data) |
soulx | 0:0c88691e3904 | 72 | { |
soulx | 0:0c88691e3904 | 73 | return (((int32_t)data[3]) << 24) | (((int32_t)data[2]) << 16) | (((int32_t)data[1]) << 8) | ((int32_t)data[0]); |
soulx | 0:0c88691e3904 | 74 | } |
soulx | 0:0c88691e3904 | 75 |