AKM Development Platform. This is the D7.014 version.
Dependencies: AK09970 AK099XX AK7401 AK7451 AK8963X AK9750 AK9752 AkmSensor BLE_API I2CNano MCP342x SerialNano SpiNano TCA9554A mbed nRF51822
Fork of AKDP by
Message.cpp@0:c240899240e7, 2016-04-28 (annotated)
- Committer:
- masahikofukasawa
- Date:
- Thu Apr 28 21:24:22 2016 +0000
- Revision:
- 0:c240899240e7
- Child:
- 12:522a22a23f8a
First commit. ; ; Tested sensors:; EM1781, EM1771, AK8776, EQ-432L, AK7451, AK8963, AK09911, AK09912, AK09915, AK09916
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
masahikofukasawa | 0:c240899240e7 | 1 | #include "Message.h" |
masahikofukasawa | 0:c240899240e7 | 2 | #include "ctype.h" |
masahikofukasawa | 0:c240899240e7 | 3 | #include "stdlib.h" |
masahikofukasawa | 0:c240899240e7 | 4 | |
masahikofukasawa | 0:c240899240e7 | 5 | #define HEXADECIMAL 16 |
masahikofukasawa | 0:c240899240e7 | 6 | |
masahikofukasawa | 0:c240899240e7 | 7 | Message::Status Message::asciiToChar(char *out, const char *in) { |
masahikofukasawa | 0:c240899240e7 | 8 | Status status = SUCCESS; |
masahikofukasawa | 0:c240899240e7 | 9 | |
masahikofukasawa | 0:c240899240e7 | 10 | if (isxdigit((int)in[0]) && isxdigit((int)in[1])) { // Sanity check |
masahikofukasawa | 0:c240899240e7 | 11 | // Given input is valid. |
masahikofukasawa | 0:c240899240e7 | 12 | char tmp[] = {in[0], in[1], '\0'}; |
masahikofukasawa | 0:c240899240e7 | 13 | *out = (char)strtol(tmp, NULL, HEXADECIMAL); |
masahikofukasawa | 0:c240899240e7 | 14 | } else if (in[0] == '\0' || in[0] == '\n') { |
masahikofukasawa | 0:c240899240e7 | 15 | // End of the string |
masahikofukasawa | 0:c240899240e7 | 16 | status = END_OF_STR; |
masahikofukasawa | 0:c240899240e7 | 17 | } else { |
masahikofukasawa | 0:c240899240e7 | 18 | // Contained an illegal character. |
masahikofukasawa | 0:c240899240e7 | 19 | status = ERROR_ILLEGAL_CHAR; |
masahikofukasawa | 0:c240899240e7 | 20 | } |
masahikofukasawa | 0:c240899240e7 | 21 | return status; |
masahikofukasawa | 0:c240899240e7 | 22 | } |
masahikofukasawa | 0:c240899240e7 | 23 | |
masahikofukasawa | 0:c240899240e7 | 24 | |
masahikofukasawa | 0:c240899240e7 | 25 | void Message::charToAscii(char *out, const char *in) { |
masahikofukasawa | 0:c240899240e7 | 26 | const char table[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; |
masahikofukasawa | 0:c240899240e7 | 27 | |
masahikofukasawa | 0:c240899240e7 | 28 | // higher byte |
masahikofukasawa | 0:c240899240e7 | 29 | out[0] = table[((*in >> 4) & 0x0f)]; |
masahikofukasawa | 0:c240899240e7 | 30 | // lower byte |
masahikofukasawa | 0:c240899240e7 | 31 | out[1] = table[(*in & 0x0f)]; |
masahikofukasawa | 0:c240899240e7 | 32 | } |
masahikofukasawa | 0:c240899240e7 | 33 | |
masahikofukasawa | 0:c240899240e7 | 34 | |
masahikofukasawa | 0:c240899240e7 | 35 | Message::Command Message::getCommand() const { |
masahikofukasawa | 0:c240899240e7 | 36 | return cmd; |
masahikofukasawa | 0:c240899240e7 | 37 | } |
masahikofukasawa | 0:c240899240e7 | 38 | |
masahikofukasawa | 0:c240899240e7 | 39 | |
masahikofukasawa | 0:c240899240e7 | 40 | int Message::getArgNum() const { |
masahikofukasawa | 0:c240899240e7 | 41 | return num_arg; |
masahikofukasawa | 0:c240899240e7 | 42 | } |
masahikofukasawa | 0:c240899240e7 | 43 | |
masahikofukasawa | 0:c240899240e7 | 44 | void Message::getArguments(char *buf, int maxNum) const { |
masahikofukasawa | 0:c240899240e7 | 45 | for (int i=0; i < maxNum; i++) { |
masahikofukasawa | 0:c240899240e7 | 46 | if (i >= num_arg) { |
masahikofukasawa | 0:c240899240e7 | 47 | break; |
masahikofukasawa | 0:c240899240e7 | 48 | } |
masahikofukasawa | 0:c240899240e7 | 49 | buf[i] = arg[i]; |
masahikofukasawa | 0:c240899240e7 | 50 | } |
masahikofukasawa | 0:c240899240e7 | 51 | } |
masahikofukasawa | 0:c240899240e7 | 52 | |
masahikofukasawa | 0:c240899240e7 | 53 | |
masahikofukasawa | 0:c240899240e7 | 54 | char Message::getArgument(int index) const { |
masahikofukasawa | 0:c240899240e7 | 55 | char c = 0; |
masahikofukasawa | 0:c240899240e7 | 56 | |
masahikofukasawa | 0:c240899240e7 | 57 | if ( index < num_arg) { |
masahikofukasawa | 0:c240899240e7 | 58 | c = arg[index]; |
masahikofukasawa | 0:c240899240e7 | 59 | } |
masahikofukasawa | 0:c240899240e7 | 60 | |
masahikofukasawa | 0:c240899240e7 | 61 | return c; |
masahikofukasawa | 0:c240899240e7 | 62 | } |
masahikofukasawa | 0:c240899240e7 | 63 | |
masahikofukasawa | 0:c240899240e7 | 64 | int Message::getMaxMessageLength() { |
masahikofukasawa | 0:c240899240e7 | 65 | return MAX_LEN; |
masahikofukasawa | 0:c240899240e7 | 66 | } |
masahikofukasawa | 0:c240899240e7 | 67 | |
masahikofukasawa | 0:c240899240e7 | 68 | |
masahikofukasawa | 0:c240899240e7 | 69 | Message::Status Message::parse(Message *msg, const char *str) { |
masahikofukasawa | 0:c240899240e7 | 70 | Status status = SUCCESS; |
masahikofukasawa | 0:c240899240e7 | 71 | |
masahikofukasawa | 0:c240899240e7 | 72 | // Parses command |
masahikofukasawa | 0:c240899240e7 | 73 | if ((status=asciiToChar((char*)&msg->cmd, &str[0])) != SUCCESS) { |
masahikofukasawa | 0:c240899240e7 | 74 | return status; |
masahikofukasawa | 0:c240899240e7 | 75 | } |
masahikofukasawa | 0:c240899240e7 | 76 | |
masahikofukasawa | 0:c240899240e7 | 77 | // Parses arguments |
masahikofukasawa | 0:c240899240e7 | 78 | int i = 0; |
masahikofukasawa | 0:c240899240e7 | 79 | while ((status=asciiToChar(&msg->arg[i], &str[2+i*2])) == SUCCESS) { |
masahikofukasawa | 0:c240899240e7 | 80 | i++; |
masahikofukasawa | 0:c240899240e7 | 81 | } |
masahikofukasawa | 0:c240899240e7 | 82 | if (status == ERROR_ILLEGAL_CHAR) { |
masahikofukasawa | 0:c240899240e7 | 83 | // Given string contains illegal character. |
masahikofukasawa | 0:c240899240e7 | 84 | return status; |
masahikofukasawa | 0:c240899240e7 | 85 | } else { |
masahikofukasawa | 0:c240899240e7 | 86 | status = SUCCESS; |
masahikofukasawa | 0:c240899240e7 | 87 | } |
masahikofukasawa | 0:c240899240e7 | 88 | |
masahikofukasawa | 0:c240899240e7 | 89 | msg->num_arg = i+1; |
masahikofukasawa | 0:c240899240e7 | 90 | |
masahikofukasawa | 0:c240899240e7 | 91 | return status; |
masahikofukasawa | 0:c240899240e7 | 92 | } |
masahikofukasawa | 0:c240899240e7 | 93 | |
masahikofukasawa | 0:c240899240e7 | 94 | |
masahikofukasawa | 0:c240899240e7 | 95 | void Message::setCommand(Message::Command cmd) { |
masahikofukasawa | 0:c240899240e7 | 96 | this->cmd = cmd; |
masahikofukasawa | 0:c240899240e7 | 97 | } |
masahikofukasawa | 0:c240899240e7 | 98 | |
masahikofukasawa | 0:c240899240e7 | 99 | void Message::setArguments(const char *arg, int len) { |
masahikofukasawa | 0:c240899240e7 | 100 | if (len > NUM_MAX_ARG) { // sanity check |
masahikofukasawa | 0:c240899240e7 | 101 | return; |
masahikofukasawa | 0:c240899240e7 | 102 | } |
masahikofukasawa | 0:c240899240e7 | 103 | |
masahikofukasawa | 0:c240899240e7 | 104 | for (int i=0; i < len; i++) { |
masahikofukasawa | 0:c240899240e7 | 105 | this->arg[i] = arg[i]; |
masahikofukasawa | 0:c240899240e7 | 106 | } |
masahikofukasawa | 0:c240899240e7 | 107 | |
masahikofukasawa | 0:c240899240e7 | 108 | num_arg = len; |
masahikofukasawa | 0:c240899240e7 | 109 | } |
masahikofukasawa | 0:c240899240e7 | 110 | |
masahikofukasawa | 0:c240899240e7 | 111 | |
masahikofukasawa | 0:c240899240e7 | 112 | void Message::setArgument(int index, char arg) { |
masahikofukasawa | 0:c240899240e7 | 113 | if (index > (NUM_MAX_ARG - 1)) { // sanity check |
masahikofukasawa | 0:c240899240e7 | 114 | return; |
masahikofukasawa | 0:c240899240e7 | 115 | } |
masahikofukasawa | 0:c240899240e7 | 116 | |
masahikofukasawa | 0:c240899240e7 | 117 | this->arg[index] = arg; |
masahikofukasawa | 0:c240899240e7 | 118 | |
masahikofukasawa | 0:c240899240e7 | 119 | int n = index + 1; |
masahikofukasawa | 0:c240899240e7 | 120 | if (num_arg < n) { |
masahikofukasawa | 0:c240899240e7 | 121 | num_arg = n; |
masahikofukasawa | 0:c240899240e7 | 122 | } |
masahikofukasawa | 0:c240899240e7 | 123 | } |
masahikofukasawa | 0:c240899240e7 | 124 |