Allwize Library
main.cpp
1 #include "Allwize.h"
2 #include "StreamInjector.h"
3 
4 StreamInjector mock;
5 Allwize * allwize;
6 
7 // -----------------------------------------------------------------------------
8 
9 uint8_t _rx_buffer[32];
10 uint8_t _rx_count = 0;
11 
12 void _reset() {
13  _rx_count = 0;
14 }
15 
16 bool _compare(uint8_t * expected, size_t len) {
17  if (len != _rx_count) return false;
18  for (uint8_t i=0; i<len; i++) {
19  if (_rx_buffer[i] != expected[i]) return false;
20  }
21  return true;
22 }
23 
24 void _mock_response(unsigned char ch) {
25 
26  static uint8_t pending_payload = 1;
27 
28  if (pending_payload > 0) --pending_payload;
29  if (0 == pending_payload) mock.inject('>');
30 
31  _rx_buffer[_rx_count++] = ch;
32 
33 }
34 
35 // -----------------------------------------------------------------------------
36 
37 bool test_set_channel(void) {
38  allwize->setChannel(3);
39  uint8_t expected[4] = {0x00, 'C', 3, 0xFF};
40  return _compare(expected, 4);
41 }
42 
43 // -----------------------------------------------------------------------------
44 
45 void test(const char * name, bool (*callback)(void)) {
46  _reset();
47  Serial.print(name);
48  Serial.print(": ");
49  bool response = (callback)();
50  Serial.println(response ? "OK" : "FAIL");
51 
52 }
53 void setup() {
54 
55  Serial.begin(115200);
56  while (!Serial);
57  Serial.println();
58  Serial.println("Allwize library test suite");
59  Serial.println("--------------------------");
60  Serial.println();
61 
62  mock.callback(_mock_response);
63  allwize = new Allwize(mock);
64 
65  test("setChannel", test_set_channel);
66 
67 }
68 
69 void loop() {}
void setChannel(uint8_t channel)
Sets the communications channel.
Definition: Allwize.cpp:63