NerfUS / NerfUSXbee

Dependents:   NerfUS-Coord NerfUSTarget

Fork of APP3_xbee by Team APP

Committer:
dupm2216
Date:
Sun Feb 12 22:42:45 2017 +0000
Revision:
5:cd3c79853dc8
Parent:
4:e97cfe6cc18c
Child:
6:b70f32a80d51
AT command for powering the LED

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dupm2216 0:8f5379c94a69 1 #include "xbee.h"
dupm2216 4:e97cfe6cc18c 2 #include <cassert>
dupm2216 0:8f5379c94a69 3
dupm2216 0:8f5379c94a69 4 DigitalOut led_1(LED1);
dupm2216 4:e97cfe6cc18c 5 Mail<vector<char>, 30> parsed_frames;
dupm2216 0:8f5379c94a69 6 RawSerial xbee(p13, p14);
dupm2216 0:8f5379c94a69 7 const int FRAME_SPECIFIC_DATA_BEGIN[14] = {0x10, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFE, 0x00, 0x00};
dupm2216 5:cd3c79853dc8 8 const int AT_COMMAND_LED_FRAME_SPECIFIC_DATA_BEGIN[15] = {0x17, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFE, 0x02, 0x50, 0x32};
dupm2216 5:cd3c79853dc8 9 const char LED_COMMAND_POWER_ON = 0x05;
dupm2216 5:cd3c79853dc8 10 const char LED_COMMAND_POWER_OFF = 0x04;
dupm2216 0:8f5379c94a69 11 const int RECEIVE_PACKET_MESSAGE_START_INDEX = 15;
dupm2216 0:8f5379c94a69 12
dupm2216 0:8f5379c94a69 13 const char START_DELIMITER = 0x7E;
dupm2216 0:8f5379c94a69 14
dupm2216 0:8f5379c94a69 15 void send_message_via_xbee(const char* message, const int length)
dupm2216 0:8f5379c94a69 16 {
dupm2216 0:8f5379c94a69 17 const vector<char> transmit_request = generate_transmit_request(message, length);
dupm2216 0:8f5379c94a69 18
dupm2216 0:8f5379c94a69 19 for(int i=0; i<transmit_request.size(); i++)
dupm2216 0:8f5379c94a69 20 {
dupm2216 0:8f5379c94a69 21 xbee.putc(transmit_request[i]);
dupm2216 0:8f5379c94a69 22 }
dupm2216 0:8f5379c94a69 23 }
dupm2216 0:8f5379c94a69 24
dupm2216 0:8f5379c94a69 25 vector<char> generate_transmit_request(const char* message, const int length)
dupm2216 0:8f5379c94a69 26 {
dupm2216 0:8f5379c94a69 27 vector<char> request;
dupm2216 0:8f5379c94a69 28
dupm2216 0:8f5379c94a69 29 unsigned char checksum = 0xFF;
dupm2216 0:8f5379c94a69 30
dupm2216 0:8f5379c94a69 31 request.push_back(START_DELIMITER);
dupm2216 0:8f5379c94a69 32
dupm2216 0:8f5379c94a69 33 const uint16_t frame_length = 0x0E + length;
dupm2216 0:8f5379c94a69 34 const uint8_t frame_length_msb = frame_length >> 8;
dupm2216 0:8f5379c94a69 35 const uint8_t frame_length_lsb = (frame_length << 8) >> 8;
dupm2216 0:8f5379c94a69 36 request.push_back(frame_length_msb);
dupm2216 0:8f5379c94a69 37 request.push_back(frame_length_lsb);
dupm2216 0:8f5379c94a69 38
dupm2216 0:8f5379c94a69 39 for(int i=0; i<14; i++)
dupm2216 0:8f5379c94a69 40 {
dupm2216 0:8f5379c94a69 41 request.push_back(FRAME_SPECIFIC_DATA_BEGIN[i]);
dupm2216 0:8f5379c94a69 42 checksum -= FRAME_SPECIFIC_DATA_BEGIN[i];
dupm2216 0:8f5379c94a69 43 }
dupm2216 0:8f5379c94a69 44
dupm2216 0:8f5379c94a69 45 for(int i=0; i<length; i++)
dupm2216 0:8f5379c94a69 46 {
dupm2216 0:8f5379c94a69 47 request.push_back(message[i]);
dupm2216 0:8f5379c94a69 48 checksum -= message[i];
dupm2216 0:8f5379c94a69 49 }
dupm2216 0:8f5379c94a69 50
dupm2216 0:8f5379c94a69 51 request.push_back(checksum);
dupm2216 0:8f5379c94a69 52
dupm2216 0:8f5379c94a69 53 return request;
dupm2216 0:8f5379c94a69 54 }
dupm2216 0:8f5379c94a69 55
dupm2216 5:cd3c79853dc8 56 vector<char> generate_led_command(const bool power_on)
dupm2216 5:cd3c79853dc8 57 {
dupm2216 5:cd3c79853dc8 58 vector<char> request;
dupm2216 5:cd3c79853dc8 59
dupm2216 5:cd3c79853dc8 60 unsigned char checksum = 0xFF;
dupm2216 5:cd3c79853dc8 61
dupm2216 5:cd3c79853dc8 62 request.push_back(START_DELIMITER);
dupm2216 5:cd3c79853dc8 63
dupm2216 5:cd3c79853dc8 64 const uint16_t frame_length = 0x10;
dupm2216 5:cd3c79853dc8 65 const uint8_t frame_length_msb = frame_length >> 8;
dupm2216 5:cd3c79853dc8 66 const uint8_t frame_length_lsb = (frame_length << 8) >> 8;
dupm2216 5:cd3c79853dc8 67 request.push_back(frame_length_msb);
dupm2216 5:cd3c79853dc8 68 request.push_back(frame_length_lsb);
dupm2216 5:cd3c79853dc8 69
dupm2216 5:cd3c79853dc8 70 for(int i=0; i<15; i++)
dupm2216 5:cd3c79853dc8 71 {
dupm2216 5:cd3c79853dc8 72 request.push_back(AT_COMMAND_LED_FRAME_SPECIFIC_DATA_BEGIN[i]);
dupm2216 5:cd3c79853dc8 73 checksum -= AT_COMMAND_LED_FRAME_SPECIFIC_DATA_BEGIN[i];
dupm2216 5:cd3c79853dc8 74 }
dupm2216 5:cd3c79853dc8 75
dupm2216 5:cd3c79853dc8 76 const char led_power_state = power_on ? LED_COMMAND_POWER_ON : LED_COMMAND_POWER_OFF;
dupm2216 5:cd3c79853dc8 77 request.push_back(led_power_state);
dupm2216 5:cd3c79853dc8 78 checksum -= led_power_state;
dupm2216 5:cd3c79853dc8 79
dupm2216 5:cd3c79853dc8 80 request.push_back(checksum);
dupm2216 5:cd3c79853dc8 81
dupm2216 5:cd3c79853dc8 82 return request;
dupm2216 5:cd3c79853dc8 83 }
dupm2216 5:cd3c79853dc8 84
dupm2216 0:8f5379c94a69 85 void read_frame()
dupm2216 0:8f5379c94a69 86 {
dupm2216 0:8f5379c94a69 87 while(true)
dupm2216 0:8f5379c94a69 88 {
dupm2216 0:8f5379c94a69 89 while(xbee.getc() != 0x7E);
dupm2216 0:8f5379c94a69 90
dupm2216 0:8f5379c94a69 91 vector<char> frame;
dupm2216 0:8f5379c94a69 92
dupm2216 0:8f5379c94a69 93 frame.push_back(0x7E);
dupm2216 0:8f5379c94a69 94
dupm2216 0:8f5379c94a69 95 const uint8_t frame_size_msb = xbee.getc();
dupm2216 0:8f5379c94a69 96 const uint8_t frame_size_lsb = xbee.getc();
dupm2216 0:8f5379c94a69 97 frame.push_back(frame_size_msb);
dupm2216 0:8f5379c94a69 98 frame.push_back(frame_size_lsb);
dupm2216 0:8f5379c94a69 99 const uint16_t frame_size = (frame_size_msb << 8) + frame_size_lsb;
dupm2216 0:8f5379c94a69 100
dupm2216 0:8f5379c94a69 101 for(int i=0; i<frame_size + 1; i++)
dupm2216 0:8f5379c94a69 102 {
dupm2216 0:8f5379c94a69 103 frame.push_back(xbee.getc());
dupm2216 0:8f5379c94a69 104 }
dupm2216 4:e97cfe6cc18c 105
dupm2216 4:e97cfe6cc18c 106 handle_frame(frame);
dupm2216 0:8f5379c94a69 107 }
dupm2216 0:8f5379c94a69 108 }
dupm2216 0:8f5379c94a69 109
dupm2216 3:37ea92feece2 110 vector<char> parse_receive_packet(const vector<char>& frame)
dupm2216 0:8f5379c94a69 111 {
dupm2216 3:37ea92feece2 112 vector<char>::const_iterator first = frame.begin() + RECEIVE_PACKET_MESSAGE_START_INDEX;
dupm2216 3:37ea92feece2 113 vector<char>::const_iterator last = frame.end() - 1;
dupm2216 0:8f5379c94a69 114 vector<char> message(first, last);
dupm2216 0:8f5379c94a69 115 return message;
dupm2216 0:8f5379c94a69 116 }
dupm2216 0:8f5379c94a69 117
dupm2216 3:37ea92feece2 118 vector<char> parse_transmit_status(const vector<char>& frame)
dupm2216 3:37ea92feece2 119 {
dupm2216 3:37ea92feece2 120 vector<char> relevant_content;
dupm2216 3:37ea92feece2 121
dupm2216 3:37ea92feece2 122 const char delivery_status = frame.at(8);
dupm2216 3:37ea92feece2 123 relevant_content.push_back(delivery_status);
dupm2216 3:37ea92feece2 124
dupm2216 3:37ea92feece2 125 return relevant_content;
dupm2216 3:37ea92feece2 126 }
dupm2216 3:37ea92feece2 127
dupm2216 3:37ea92feece2 128 vector<char> parse_at_command_response(const vector<char>& frame)
dupm2216 3:37ea92feece2 129 {
dupm2216 3:37ea92feece2 130 vector<char> relevant_content;
dupm2216 3:37ea92feece2 131
dupm2216 3:37ea92feece2 132 const char command_status = frame.at(7);
dupm2216 3:37ea92feece2 133 relevant_content.push_back(command_status);
dupm2216 3:37ea92feece2 134
dupm2216 3:37ea92feece2 135 return relevant_content;
dupm2216 3:37ea92feece2 136 }
dupm2216 3:37ea92feece2 137
dupm2216 4:e97cfe6cc18c 138 void handle_parsed_frames_from_mailbox()
dupm2216 0:8f5379c94a69 139 {
dupm2216 0:8f5379c94a69 140 while(true)
dupm2216 0:8f5379c94a69 141 {
dupm2216 4:e97cfe6cc18c 142 osEvent event = parsed_frames.get();
dupm2216 4:e97cfe6cc18c 143 assert(event.status == osEventMail);
dupm2216 4:e97cfe6cc18c 144
dupm2216 4:e97cfe6cc18c 145 vector<char> *message = (vector<char>*)event.value.p;
dupm2216 4:e97cfe6cc18c 146
dupm2216 4:e97cfe6cc18c 147 const char parsed_frame_type = message->at(0);
dupm2216 4:e97cfe6cc18c 148 printf("Handling parsed frame. Type: %d\r\n", parsed_frame_type);
dupm2216 4:e97cfe6cc18c 149
dupm2216 4:e97cfe6cc18c 150 printf("Relevant content: ");
dupm2216 4:e97cfe6cc18c 151 for(int i=1; i<message->size(); i++)
dupm2216 0:8f5379c94a69 152 {
dupm2216 4:e97cfe6cc18c 153 printf("%d\r\n", message->at(i));
dupm2216 0:8f5379c94a69 154 }
dupm2216 4:e97cfe6cc18c 155
dupm2216 4:e97cfe6cc18c 156 parsed_frames.free(message);
dupm2216 0:8f5379c94a69 157 }
dupm2216 3:37ea92feece2 158 }
dupm2216 3:37ea92feece2 159
dupm2216 4:e97cfe6cc18c 160 void handle_frame(const vector<char>& frame)
dupm2216 4:e97cfe6cc18c 161 {
dupm2216 4:e97cfe6cc18c 162 vector<char> *parsed_frame = parsed_frames.alloc();
dupm2216 4:e97cfe6cc18c 163 *parsed_frame = parse_frame(frame);
dupm2216 4:e97cfe6cc18c 164 parsed_frames.put(parsed_frame);
dupm2216 4:e97cfe6cc18c 165 }
dupm2216 4:e97cfe6cc18c 166
dupm2216 3:37ea92feece2 167 vector<char> parse_frame(const vector<char>& frame)
dupm2216 3:37ea92feece2 168 {
dupm2216 3:37ea92feece2 169 vector<char> parsed_frame;
dupm2216 3:37ea92feece2 170
dupm2216 3:37ea92feece2 171 const char frame_type = frame.at(3);
dupm2216 3:37ea92feece2 172 parsed_frame.push_back(frame_type);
dupm2216 3:37ea92feece2 173
dupm2216 3:37ea92feece2 174 vector<char> parsed_frame_relevant_content;
dupm2216 3:37ea92feece2 175
dupm2216 3:37ea92feece2 176 switch(frame_type)
dupm2216 3:37ea92feece2 177 {
dupm2216 3:37ea92feece2 178 case FRAME_TYPE_RECEIVE_PACKET:
dupm2216 3:37ea92feece2 179 {
dupm2216 3:37ea92feece2 180 parsed_frame_relevant_content = parse_receive_packet(frame);
dupm2216 3:37ea92feece2 181 break;
dupm2216 3:37ea92feece2 182 }
dupm2216 3:37ea92feece2 183 case FRAME_TYPE_TRANSMIT_STATUS:
dupm2216 3:37ea92feece2 184 {
dupm2216 3:37ea92feece2 185 parsed_frame_relevant_content = parse_transmit_status(frame);
dupm2216 3:37ea92feece2 186 break;
dupm2216 3:37ea92feece2 187 }
dupm2216 3:37ea92feece2 188 case FRAME_TYPE_AT_COMMAND_RESPONSE:
dupm2216 3:37ea92feece2 189 {
dupm2216 3:37ea92feece2 190 parsed_frame_relevant_content = parse_at_command_response(frame);
dupm2216 3:37ea92feece2 191 break;
dupm2216 3:37ea92feece2 192 }
dupm2216 3:37ea92feece2 193 default: assert(false && "Unsupported frame type");
dupm2216 3:37ea92feece2 194 }
dupm2216 3:37ea92feece2 195
dupm2216 3:37ea92feece2 196 for(vector<char>::iterator it = parsed_frame_relevant_content.begin(); it < parsed_frame_relevant_content.end(); it++)
dupm2216 3:37ea92feece2 197 {
dupm2216 3:37ea92feece2 198 parsed_frame.push_back(*it);
dupm2216 3:37ea92feece2 199 }
dupm2216 3:37ea92feece2 200
dupm2216 3:37ea92feece2 201 return parsed_frame;
dupm2216 3:37ea92feece2 202 }