-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHelium.cpp
More file actions
208 lines (179 loc) · 3.96 KB
/
Copy pathHelium.cpp
File metadata and controls
208 lines (179 loc) · 3.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*
* Copyright 2017, Helium Systems, Inc.
* All Rights Reserved. See LICENCE.txt for license information
*/
#include "Helium.h"
#include "Arduino.h"
#include "helium-client/helium-client.h"
bool
helium_serial_readable(void * param)
{
Stream * stream = (Stream *)param;
return stream->available() > 0;
}
bool
helium_serial_getc(void * param, uint8_t * ch)
{
Stream * stream = (Stream *)param;
*ch = stream->read();
return *ch != -1;
}
bool
helium_serial_putc(void * param, uint8_t ch)
{
Stream * stream = (Stream *)param;
return stream->write(ch) == 1;
}
void
helium_wait_us(void * param, uint32_t us)
{
(void)param;
#if defined(ARDUINO_ARCH_AVR)
#define MAX_DELAY_US 16384
while (us > MAX_DELAY_US)
{
delayMicroseconds(MAX_DELAY_US);
us -= MAX_DELAY_US;
}
delayMicroseconds(us);
#else
delayMicroseconds(us);
#endif
}
Helium::Helium(USARTSerial * serial)
{
helium_init(&_ctx, (void *)serial);
}
#if defined(ARDUINO_AVR_UNO)
Helium::Helium(SoftwareSerial * serial)
{
helium_init(&_ctx, (void *)serial);
}
#endif
int
Helium::begin(enum helium_baud baud)
{
#if defined(ARDUINO_AVR_UNO)
SoftwareSerial * serial = (SoftwareSerial *)_ctx.param;
#else
USARTSerial * serial = (USARTSerial *)_ctx.param;
#endif
serial->begin(9600);
int result = helium_baud(&_ctx, baud);
uint32_t serial_baud = 9600;
switch (baud)
{
case helium_baud_b9600:
serial_baud = 9600;
break;
case helium_baud_b14400:
serial_baud = 14400;
break;
case helium_baud_b19200:
serial_baud = 19200;
break;
case helium_baud_b38400:
serial_baud = 38400;
break;
case helium_baud_b57600:
serial_baud = 57600;
break;
case helium_baud_b115200:
serial_baud = 115200;
break;
}
serial->begin(serial_baud);
return result;
}
int
Helium::info(struct helium_info * info)
{
return helium_info(&_ctx, info);
}
int
Helium::connect(struct connection * connection, uint32_t retries)
{
return helium_connect(&_ctx, connection, retries);
}
bool
Helium::connected()
{
return helium_connected(&_ctx) == helium_connected_CONNECTED;
}
int
Helium::sleep(struct connection * connection)
{
return helium_sleep(&_ctx, connection);
}
bool
Helium::needs_reset()
{
return helium_needs_reset(&_ctx);
}
int
Helium::reset()
{
return helium_reset(&_ctx);
}
//
// Channel
//
Channel::Channel(Helium * helium)
{
_helium = helium;
}
int
Channel::begin(const char * name, uint16_t * token)
{
return helium_channel_create(&_helium->_ctx, name, strlen(name), token);
}
int
Channel::begin(const char * name, int8_t * result)
{
uint16_t token;
int status = begin(name, &token);
_channel_id = -1;
if (helium_status_OK == status)
{
status = poll_result(token, &_channel_id);
}
if (result)
{
*result = status == helium_status_OK && _channel_id > 0 ? 0 : _channel_id;
}
return status;
}
int
Channel::send(void const * data, size_t len, uint16_t * token)
{
return helium_channel_send(&_helium->_ctx, _channel_id, data, len, token);
}
int
Channel::send(void const * data, size_t len, int8_t * result)
{
uint16_t token;
int status = send(data, len, &token);
if (helium_status_OK == status)
{
status = poll_result(token, result);
}
return status;
}
int
Channel::poll_result(uint16_t token, int8_t * result, uint32_t retries)
{
return helium_channel_poll_result(&_helium->_ctx, token, result, retries);
}
int
Channel::poll_data(void * data,
size_t len,
size_t * used,
uint32_t retries)
{
return helium_channel_poll_data(&_helium->_ctx,
_channel_id,
data,
len,
used,
retries);
}