Fixed more bogues

Files at this revision

API Documentation at this revision

Comitter:
dupm2216
Date:
Mon Feb 13 19:13:50 2017 +0000
Parent:
5:cd3c79853dc8
Child:
7:c65c4c98d237
Commit message:
Convert a parsed frame into a human readable string

Changed in this revision

test.cpp Show annotated file Show diff for this revision Revisions of this file
test.h Show annotated file Show diff for this revision Revisions of this file
xbee.cpp Show annotated file Show diff for this revision Revisions of this file
xbee.h Show annotated file Show diff for this revision Revisions of this file
--- a/test.cpp	Sun Feb 12 22:42:45 2017 +0000
+++ b/test.cpp	Mon Feb 13 19:13:50 2017 +0000
@@ -13,6 +13,13 @@
     parse_frame_parses_transmit_status_test();
     parse_frame_parses_at_command_response_test();
     handle_frame_adds_parsed_frame_to_mailbox_test();
+    
+    parsed_frame_to_string_button_event_released_test();
+    parsed_frame_to_string_button_event_pressed_test();
+    parsed_frame_to_string_button_event_invalid_state_test();
+    parsed_frame_to_string_accelerometer_test();
+    parsed_frame_to_string_accelerometer_negative_values_test();
+    parsed_frame_to_string_unsupported_event_type_test();
 }
 
 void vectors_are_equal_test()
@@ -157,4 +164,76 @@
     
     assert(vectors_are_equal(expected_parsed_frame, *parsed_frame)); 
     parsed_frames.free(parsed_frame);   
-}
\ No newline at end of file
+}
+
+void parsed_frame_to_string_button_event_released_test()
+{
+    const char parsed_frame_array[] = {FRAME_TYPE_RECEIVE_PACKET, EVENT_TYPE_BUTTON, 0x00};
+    const vector<char> parsed_frame = construct_vector(parsed_frame_array, 3);
+    const char expected_string[] = "Button state: released";
+    
+    char actual_string[40];
+    parsed_frame_to_string(parsed_frame, actual_string);
+    
+    assert(0 == strcmp(expected_string, actual_string));
+}
+
+void parsed_frame_to_string_button_event_pressed_test()
+{
+    const char parsed_frame_array[] = {FRAME_TYPE_RECEIVE_PACKET, EVENT_TYPE_BUTTON, 0x01};
+    const vector<char> parsed_frame = construct_vector(parsed_frame_array, 3);
+    const char expected_string[] = "Button state: pressed";
+    
+    char actual_string[40];
+    parsed_frame_to_string(parsed_frame, actual_string);
+    
+    assert(0 == strcmp(expected_string, actual_string));
+}
+
+void parsed_frame_to_string_button_event_invalid_state_test()
+{
+    const char parsed_frame_array[] = {FRAME_TYPE_RECEIVE_PACKET, EVENT_TYPE_BUTTON, 'X'};
+    const vector<char> parsed_frame = construct_vector(parsed_frame_array, 3);
+    const char expected_string[] = "Button state: invalid state";
+    
+    char actual_string[40];
+    parsed_frame_to_string(parsed_frame, actual_string);
+    
+    assert(0 == strcmp(expected_string, actual_string));
+}
+
+void parsed_frame_to_string_accelerometer_test()
+{
+    const char parsed_frame_array[] = {FRAME_TYPE_RECEIVE_PACKET, EVENT_TYPE_ACCELEROMETER, 0x01, 0x02, 0x03};
+    const vector<char> parsed_frame = construct_vector(parsed_frame_array, 5);
+    const char expected_string[] = "Accelerometer state: x=0x1, y=0x2, z=0x3";
+    
+    char actual_string[40];
+    parsed_frame_to_string(parsed_frame, actual_string);
+    
+    assert(0 == strcmp(expected_string, actual_string));
+}
+
+void parsed_frame_to_string_accelerometer_negative_values_test()
+{
+    const char parsed_frame_array[] = {FRAME_TYPE_RECEIVE_PACKET, EVENT_TYPE_ACCELEROMETER, 0xFF, 0xFE, 0xFD};
+    const vector<char> parsed_frame = construct_vector(parsed_frame_array, 5);
+    const char expected_string[] = "Accelerometer state: x=0xFF, y=0xFE, z=0xFD";
+    
+    char actual_string[40];
+    parsed_frame_to_string(parsed_frame, actual_string);
+    
+    assert(0 == strcmp(expected_string, actual_string));
+}
+
+void parsed_frame_to_string_unsupported_event_type_test()
+{
+    const char parsed_frame_array[] = {FRAME_TYPE_RECEIVE_PACKET, 'X'};
+    const vector<char> parsed_frame = construct_vector(parsed_frame_array, 2);
+    const char expected_string[] = "Invalid event type";
+    
+    char actual_string[40];
+    parsed_frame_to_string(parsed_frame, actual_string);
+    
+    assert(0 == strcmp(expected_string, actual_string));
+}
--- a/test.h	Sun Feb 12 22:42:45 2017 +0000
+++ b/test.h	Mon Feb 13 19:13:50 2017 +0000
@@ -16,4 +16,11 @@
 void parse_frame_parses_at_command_response_test();
 void handle_frame_adds_parsed_frame_to_mailbox_test();
 
+void parsed_frame_to_string_button_event_released_test();
+void parsed_frame_to_string_button_event_pressed_test();
+void parsed_frame_to_string_button_event_invalid_state_test();
+void parsed_frame_to_string_accelerometer_test();
+void parsed_frame_to_string_accelerometer_negative_values_test();
+void parsed_frame_to_string_unsupported_event_type_test();
+
 #endif
\ No newline at end of file
--- a/xbee.cpp	Sun Feb 12 22:42:45 2017 +0000
+++ b/xbee.cpp	Mon Feb 13 19:13:50 2017 +0000
@@ -200,3 +200,49 @@
     
     return parsed_frame;
 }
+
+void parsed_frame_to_string(const vector<char>& parsed_frame, char* readable_string_output)
+{
+    const char event_type = parsed_frame[1];
+    switch(event_type)
+    {
+        case EVENT_TYPE_BUTTON:
+            parsed_button_event_frame_to_string(parsed_frame, readable_string_output);
+            break;
+            
+        case EVENT_TYPE_ACCELEROMETER:
+            parsed_accelerometer_event_frame_to_string(parsed_frame, readable_string_output);
+            break;
+            
+        default:
+            strcpy(readable_string_output, "Invalid event type");
+            break;
+    }
+}
+
+void parsed_button_event_frame_to_string(const vector<char>& parsed_frame, char* readable_string_output)
+{
+    const char button_state = parsed_frame[2];
+    switch(button_state)
+    {
+        case 0x00:
+            strcpy(readable_string_output, "Button state: released");
+            break;
+            
+        case 0x01:
+            strcpy(readable_string_output, "Button state: pressed");
+            break;
+            
+        default:
+            strcpy(readable_string_output, "Button state: invalid state");
+            break;
+    }
+}
+
+void parsed_accelerometer_event_frame_to_string(const vector<char>& parsed_frame, char* readable_string_output)
+{
+    const uint8_t x = parsed_frame[2];
+    const uint8_t y = parsed_frame[3];
+    const uint8_t z = parsed_frame[4];
+    sprintf(readable_string_output, "Accelerometer state: x=0x%X, y=0x%X, z=0x%X", x, y, z);
+}
\ No newline at end of file
--- a/xbee.h	Sun Feb 12 22:42:45 2017 +0000
+++ b/xbee.h	Mon Feb 13 19:13:50 2017 +0000
@@ -14,6 +14,9 @@
 const char TRANSMIT_STATUS_SUCCESS = 0x00;
 const char AT_COMMAND_RESPONSE_STATUS_OK = 0x00;
 
+const char EVENT_TYPE_BUTTON = 0x00;
+const char EVENT_TYPE_ACCELEROMETER = 0x01;
+
 extern Mail<vector<char>, 30> parsed_frames;
 extern RawSerial xbee;
 
@@ -27,6 +30,10 @@
 void handle_parsed_frames_from_mailbox();
 void handle_frame(const vector<char>& frame);
 
+void parsed_frame_to_string(const vector<char>& parsed_frame, char* readable_string_output);
+void parsed_button_event_frame_to_string(const vector<char>& parsed_frame, char* readable_string_output);
+void parsed_accelerometer_event_frame_to_string(const vector<char>& parsed_frame, char* readable_string_output);
+
 //Simplify the frame to only contain the frame type and the relevant data for our application
 vector<char> parse_frame(const vector<char>& frame);