cayenne_lpp 0.4.0

Rust crate that implements the Cayenne Low Power Payload (LPP) format
Documentation
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
#![no_std]
#![deny(missing_docs, unsafe_code, unstable_features, unused_import_braces, unused_qualifications, trivial_casts,
trivial_numeric_casts)]

//! This crate is a port of the [Cayenne LPP] (Low Power Payload) API. It provides an easy way to send data over LPWAN
//! networks such as LoRaWAN. Cayenne LPP is compliant with payload size restrictions, which can be lowered down to
//! 11 bytes and allows the device to send multiple sensor data at one time.
//!
//! Additionally, it is also possible to send different sensor data in different frames. To do this, the channel value
//! of the data can be used.
//!
//! The original C++ version of [Cayenne LPP] can be found [here].
//!
//! [Cayenne LPP]: https://docs.mydevices.com/docs/lorawan/cayenne-lpp
//! [here]: https://github.com/myDevicesIoT/CayenneLPP

use crate::cayenne_lpp_into_iterator::CayenneLPPIntoFailableIterator;
pub use crate::cayenne_lpp_scalar::{CayenneLPPScalar, CayenneLPPValue};
pub use crate::constants::*;
use crate::error::Error;

pub(crate) mod constants;
pub(crate) mod cayenne_lpp_scalar;
mod cayenne_lpp_into_iterator;

/// Errors that may occur in the module
pub mod error;

#[cfg(test)]
mod tests;

/// This struct contains the data of the added payload objects and an index that points to the next free
/// value in the array. All newly added values will increase the index. After adding all the values, the buffer
/// contains the payloads of the different data types and has a length of ''index''.
pub struct CayenneLPP<'a> {
    buffer: &'a mut [u8],
    index: usize
}

impl<'a> CayenneLPP<'a> {

    /// Creates a new buffer for the Cayenne LPP. Since the library works without a memory allocator, it is necessary
    /// to provide it with an array that equals the length or is bigger than the data that shall be added to it.
    ///
    /// It is possible to use the size constants for the data types (e.g., LPP_TEMPERATURE_SIZE) to exactly calculate
    /// the size of the necessary array.
    pub fn new(buffer: &'a mut [u8]) -> Self {
        CayenneLPP {
            buffer,
            index: 0
        }
    }

    /// Resets the index pointing into the buffer, so it is possible to reuse the buffer and add new payloads
    /// to it.
    /// Remark: the buffer is not cleared by this operation.
    pub fn reset(&mut self) {
        self.index = 0;
    }

    /// Returns the slice of the buffer that contains the payload of the added data types.
    pub fn payload_slice(&self) -> &[u8] {
        &self.buffer[0..self.index]
    }

    /// Adds a scalar value to the data structure.
    pub fn add_scalar(&mut self, scalar: &CayenneLPPScalar) -> Result<(), Error> {
        let channel = scalar.channel;
        match scalar.value {
            CayenneLPPValue::DigitalInput(s) => self.add_digital_input(channel, s),
            CayenneLPPValue::DigitalOutput(s) => self.add_digital_output(channel, s),
            CayenneLPPValue::AnalogInput(s) => self.add_analog_input(channel, s),
            CayenneLPPValue::AnalogOutput(s) => self.add_analog_output(channel, s),
            CayenneLPPValue::GenericSensor(s) => self.add_generic_sensor(channel, s),
            CayenneLPPValue::Luminosity(s) => self.add_luminosity(channel, s),
            CayenneLPPValue::Presence(s) => self.add_presence(channel, s),
            CayenneLPPValue::Temperature(s) => self.add_temperature(channel, s),
            CayenneLPPValue::RelativeHumidity(s) => self.add_relative_humidity(channel, s),
            CayenneLPPValue::Accelerometer(x, y, z) => self.add_accelerometer(channel, x, y, z),
            CayenneLPPValue::BarometricPressure(s) => self.add_barometric_pressure(channel, s),
            CayenneLPPValue::Voltage(s) => self.add_voltage(channel, s),
            CayenneLPPValue::Current(s) => self.add_current(channel, s),
            CayenneLPPValue::Frequency(s) => self.add_frequency(channel, s),
            CayenneLPPValue::Percentage(s) => self.add_percentage(channel, s),
            CayenneLPPValue::Altitude(s) => self.add_altitude(channel, s),
            CayenneLPPValue::Concentration(s) => self.add_concentration(channel, s),
            CayenneLPPValue::Power(s) => self.add_power(channel, s),
            CayenneLPPValue::Distance(s) => self.add_distance(channel, s),
            CayenneLPPValue::Energy(s) => self.add_energy(channel, s),
            CayenneLPPValue::Direction(s) => self.add_direction(channel, s),
            CayenneLPPValue::UnixTime(s) => self.add_unixtime(channel, s),
            CayenneLPPValue::Gyrometer(x, y, z) => self.add_gyrometer(channel, x, y, z),
            CayenneLPPValue::Color(r, g, b)        => self.add_color(channel, r, g, b),
            CayenneLPPValue::GPS(lat, lon, alt) => self.add_gps(channel, lat, lon, alt),
            CayenneLPPValue::Switch(s) => self.add_switch(channel, s),
        }
    }

    /// Adds the payload for a digital input to the Cayenne LPP data structure.
    pub fn add_digital_input(&mut self, channel: u8, value: u8) -> Result<(), Error> {
        if self.index + LPP_DIGITAL_INPUT_SIZE > self.buffer.len() {
            return Err(Error::InsufficientMemory);
        }

        self.buffer[self.index] = channel;
        self.buffer[{ self.index += 1; self.index }] = LPP_DIGITAL_INPUT;
        self.buffer[{ self.index += 1; self.index }] = value;
        self.index += 1;

        Ok(())
    }

    /// Adds the payload for a digital output to the Cayenne LPP data structure.
    pub fn add_digital_output(&mut self, channel: u8, value: u8) -> Result<(), Error> {
        if self.index + LPP_DIGITAL_OUTPUT_SIZE > self.buffer.len() {
            return Err(Error::InsufficientMemory);
        }

        self.buffer[self.index] = channel;
        self.buffer[{ self.index += 1; self.index }] = LPP_DIGITAL_OUTPUT;
        self.buffer[{ self.index += 1; self.index }] = value;
        self.index += 1;

        Ok(())
    }

    /// Adds the payload for an analog input to the Cayenne LPP data structure.
    pub fn add_analog_input(&mut self, channel: u8, value: f32) -> Result<(), Error> {
        if self.index + LPP_ANALOG_INPUT_SIZE > self.buffer.len() {
            return Err(Error::InsufficientMemory);
        }

        let analog_input: i16 = (value * 100.0) as i16;
        let analog_input_bytes = analog_input.to_be_bytes();

        self.buffer[self.index] = channel;
        self.buffer[{ self.index += 1; self.index }] = LPP_ANALOG_INPUT;
        self.buffer[{ self.index += 1; self.index }] = analog_input_bytes[0];
        self.buffer[{ self.index += 1; self.index }] = analog_input_bytes[1];
        self.index += 1;

        Ok(())
    }

    /// Adds the payload for an analog output to the Cayenne LPP data structure.
    pub fn add_analog_output(&mut self, channel: u8, value: f32) -> Result<(), Error> {
        if self.index + LPP_ANALOG_OUTPUT_SIZE > self.buffer.len() {
            return Err(Error::InsufficientMemory);
        }

        let analog_output: i16 = (value * 100.0) as i16;
        let analog_output_bytes = analog_output.to_be_bytes();

        self.buffer[self.index] = channel;
        self.buffer[{ self.index += 1; self.index }] = LPP_ANALOG_OUTPUT;
        self.buffer[{ self.index += 1; self.index }] = analog_output_bytes[0];
        self.buffer[{ self.index += 1; self.index }] = analog_output_bytes[1];
        self.index += 1;

        Ok(())
    }

    /// Adds the payload for a generic sensor to the Cayenne LPP data structure. The units are not specified.
    pub fn add_generic_sensor(&mut self, channel: u8, value: u32) -> Result<(), Error> {
        if self.index + LPP_GENERIC_SENSOR_SIZE > self.buffer.len() {
            return Err(Error::InsufficientMemory);
        }

        let value_bytes = value.to_be_bytes();

        self.buffer[self.index] = channel;
        self.buffer[{ self.index += 1; self.index }] = LPP_GENERIC_SENSOR;
        self.buffer[{ self.index += 1; self.index }] = value_bytes[0];
        self.buffer[{ self.index += 1; self.index }] = value_bytes[1];
        self.buffer[{ self.index += 1; self.index }] = value_bytes[2];
        self.buffer[{ self.index += 1; self.index }] = value_bytes[3];
        self.index += 1;

        Ok(())
    }

    /// Adds the payload for luminosity to the Cayenne LPP data structure. The value should be provided in lux.
    pub fn add_luminosity(&mut self, channel: u8, lux: u16) -> Result<(), Error> {
        if self.index + LPP_LUMINOSITY_SIZE > self.buffer.len() {
            return Err(Error::InsufficientMemory);
        }

        self.buffer[self.index] = channel;
        self.buffer[{ self.index += 1; self.index }] = LPP_LUMINOSITY;
        self.buffer[{ self.index += 1; self.index }] = (lux >> 8) as u8;
        self.buffer[{ self.index += 1; self.index }] = lux as u8;
        self.index += 1;

        Ok(())
    }

    /// Adds the payload for a presence sensor to the Cayenne LPP data structure.
    pub fn add_presence(&mut self, channel: u8, value: u8) -> Result<(), Error> {
        if self.index + LPP_PRESENCE_SIZE > self.buffer.len() {
            return Err(Error::InsufficientMemory);
        }

        self.buffer[self.index] = channel;
        self.buffer[{ self.index += 1; self.index }] = LPP_PRESENCE;
        self.buffer[{ self.index += 1; self.index }] = value;
        self.index += 1;

        Ok(())
    }

    /// Adds the payload for temperature to the Cayenne LPP data structure.
    pub fn add_temperature(&mut self, channel: u8, celsius: f32) -> Result<(), Error> {
        if self.index + LPP_TEMPERATURE_SIZE > self.buffer.len() {
            return Err(Error::InsufficientMemory);
        }

        let temperature: i16 = (celsius * 10.0) as i16;
        let temperature_bytes = temperature.to_be_bytes();

        self.buffer[self.index] = channel;
        self.buffer[{ self.index += 1; self.index }] = LPP_TEMPERATURE;
        self.buffer[{ self.index += 1; self.index }] = temperature_bytes[0];
        self.buffer[{ self.index += 1; self.index }] = temperature_bytes[1];
        self.index += 1;

        Ok(())
    }

    /// Adds the payload for relative humidity to the Cayenne LPP data structure.
    pub fn add_relative_humidity(&mut self, channel: u8, relative_humidity: f32) -> Result<(), Error> {
        if self.index + LPP_RELATIVE_HUMIDITY_SIZE > self.buffer.len() {
            return Err(Error::InsufficientMemory);
        }

        let scaled_value = relative_humidity * 2.0;
        let fraction = scaled_value % 1.0;
        let whole_number = scaled_value - fraction;
        let rounded_value = match (whole_number, fraction) {
            // Greater than zero, Round toward zero
            (s, f) if s > 0.0 && f < 0.5 => {
                s
            },
            // Greater than zero, round away from zero
            (s, f) if s > 0.0 && f >= 0.5 => {
                s + 1.0
            }
            // Less than zero, round toward zero
            (s, f) if s < 0.0 && f > -0.5 => {
                s
            },
            // Less than zero, round away from zero
            (s, f) if s < 0.0 && f <= -0.5 => {
                s - 1.0
            },
            // Should never happen, just clamp if it does...
            (s, _) => {
                s
            }
        };

        self.buffer[self.index] = channel;
        self.buffer[{ self.index += 1; self.index }] = LPP_RELATIVE_HUMIDITY;
        self.buffer[{ self.index += 1; self.index }] = rounded_value as u8;
        self.index += 1;

        Ok(())
    }

    /// Adds the payload of an accelerometer to the Cayenne LPP data structure.
    pub fn add_accelerometer(&mut self, channel: u8, x: f32, y: f32, z: f32) -> Result<(), Error> {
        if self.index + LPP_ACCELEROMETER_SIZE > self.buffer.len() {
            return Err(Error::InsufficientMemory);
        }

        // prepare axis values
        let vx: i16 = (x * 1000.0) as i16;
        let vy: i16 = (y * 1000.0) as i16;
        let vz: i16 = (z * 1000.0) as i16;

        self.buffer[self.index] = channel;
        self.buffer[{ self.index += 1; self.index }] = LPP_ACCELEROMETER;
        self.buffer[{ self.index += 1; self.index }] = (vx >> 8) as u8;
        self.buffer[{ self.index += 1; self.index }] = vx as u8;
        self.buffer[{ self.index += 1; self.index }] = (vy >> 8) as u8;
        self.buffer[{ self.index += 1; self.index }] = vy as u8;
        self.buffer[{ self.index += 1; self.index }] = (vz >> 8) as u8;
        self.buffer[{ self.index += 1; self.index }] = vz as u8;
        self.index += 1;

        Ok(())
    }

    /// Adds the payload for barometric pressure to the Cayenne LPP data structure.
    pub fn add_barometric_pressure(&mut self, channel: u8, hpa: f32) -> Result<(), Error> {
        if self.index + LPP_BAROMETRIC_PRESSURE_SIZE > self.buffer.len() {
            return Err(Error::InsufficientMemory);
        }

        let pressure = (hpa * 10.0) as u16;

        self.buffer[self.index] = channel;
        self.buffer[{ self.index += 1; self.index }] = LPP_BAROMETRIC_PRESSURE;
        self.buffer[{ self.index += 1; self.index }] = (pressure >> 8) as u8;
        self.buffer[{ self.index += 1; self.index }] = pressure as u8;
        self.index += 1;

        Ok(())
    }

    /// Adds the payload for a voltage to the Cayenne LPP data structure (in volts)
    pub fn add_voltage(&mut self, channel: u8, voltage: f32) -> Result<(), Error> {
        if self.index + LPP_VOLTAGE_SIZE > self.buffer.len() {
            return Err(Error::InsufficientMemory);
        }

        if voltage * 100.0 > u16::MAX as f32 {
            return Err(Error::OutOfRange);
        }
    
        let voltage: u16 = (voltage * 100.0) as u16;

        self.buffer[self.index] = channel;
        self.buffer[{ self.index += 1; self.index }] = LPP_VOLTAGE;
        self.buffer[{ self.index += 1; self.index }] = (voltage >> 8) as u8;
        self.buffer[{ self.index += 1; self.index }] = voltage as u8;
        self.index += 1;
        
        Ok(())
    }

    /// Adds the payload for a current to the Cayenne LPP data structure (in amps)
    pub fn add_current(&mut self, channel: u8, amperage: f32) -> Result<(), Error> {
        if self.index + LPP_CURRENT_SIZE > self.buffer.len() {
            return Err(Error::InsufficientMemory);
        }

        if amperage * 1000.0 > u16::MAX as f32 {
            return Err(Error::OutOfRange);
        }
    
        let amperage: u16 = (amperage * 1000.0) as u16;

        self.buffer[self.index] = channel;
        self.buffer[{ self.index += 1; self.index }] = LPP_CURRENT;
        self.buffer[{ self.index += 1; self.index }] = (amperage >> 8) as u8;
        self.buffer[{ self.index += 1; self.index }] = amperage as u8;
        self.index += 1;
        
        Ok(())
    }

    /// Adds the payload for a frequency to the Cayenne LPP data structure. The units are in hertz
    pub fn add_frequency(&mut self, channel: u8, frequency: u32) -> Result<(), Error> {
        if self.index + LPP_FREQUENCY_SIZE > self.buffer.len() {
            return Err(Error::InsufficientMemory);
        }

        let frequency_bytes = frequency.to_be_bytes();

        self.buffer[self.index] = channel;
        self.buffer[{ self.index += 1; self.index }] = LPP_FREQUENCY;
        self.buffer[{ self.index += 1; self.index }] = frequency_bytes[0];
        self.buffer[{ self.index += 1; self.index }] = frequency_bytes[1];
        self.buffer[{ self.index += 1; self.index }] = frequency_bytes[2];
        self.buffer[{ self.index += 1; self.index }] = frequency_bytes[3];
        self.index += 1;

        Ok(())
    }

    /// Adds the payload for a percentage to the CayenneLPP data structure.  The units are single percent (0-100)%
    pub fn add_percentage(&mut self, channel: u8, percentage: u8) -> Result<(), Error> {
        if self.index + LPP_PERCENTAGE_SIZE > self.buffer.len() {
            return Err(Error::InsufficientMemory);
        }

        self.buffer[self.index] = channel;
        self.buffer[{ self.index += 1; self.index }] = LPP_PERCENTAGE;
        self.buffer[{ self.index += 1; self.index }] = percentage;
        self.index += 1;

        Ok(())
    }

    /// Adds the payload for an altitude to the Cayenne LPP data structure (in meters)
    pub fn add_altitude(&mut self, channel: u8, altitude: i16) -> Result<(), Error> {
        if self.index + LPP_ALTITUDE_SIZE > self.buffer.len() {
            return Err(Error::InsufficientMemory);
        }

        self.buffer[self.index] = channel;
        self.buffer[{ self.index += 1; self.index }] = LPP_ALTITUDE;
        self.buffer[{ self.index += 1; self.index }] = (altitude >> 8) as u8;
        self.buffer[{ self.index += 1; self.index }] = altitude as u8;
        self.index += 1;
        
        Ok(())
    }

    /// Adds the payload for a power to the Cayenne LPP data structure (in watts)
    pub fn add_power(&mut self, channel: u8, power: u16) -> Result<(), Error> {
        if self.index + LPP_POWER_SIZE > self.buffer.len() {
            return Err(Error::InsufficientMemory);
        }

        self.buffer[self.index] = channel;
        self.buffer[{ self.index += 1; self.index }] = LPP_POWER;
        self.buffer[{ self.index += 1; self.index }] = (power >> 8) as u8;
        self.buffer[{ self.index += 1; self.index }] = power as u8;
        self.index += 1;
        
        Ok(())
    }

    /// Adds the payload for a frequency to the Cayenne LPP data structure. The units are in millimeters
    pub fn add_distance(&mut self, channel: u8, distance: u32) -> Result<(), Error> {
        if self.index + LPP_DISTANCE_SIZE > self.buffer.len() {
            return Err(Error::InsufficientMemory);
        }

        let distance_bytes = distance.to_be_bytes();

        self.buffer[self.index] = channel;
        self.buffer[{ self.index += 1; self.index }] = LPP_DISTANCE;
        self.buffer[{ self.index += 1; self.index }] = distance_bytes[0];
        self.buffer[{ self.index += 1; self.index }] = distance_bytes[1];
        self.buffer[{ self.index += 1; self.index }] = distance_bytes[2];
        self.buffer[{ self.index += 1; self.index }] = distance_bytes[3];
        self.index += 1;

        Ok(())
    }

    /// Adds the payload for energy to the Cayenne LPP data structure. The units are in single Wh
    pub fn add_energy(&mut self, channel: u8, energy: u32) -> Result<(), Error> {
        if self.index + LPP_ENERGY_SIZE > self.buffer.len() {
            return Err(Error::InsufficientMemory);
        }

        let energy_bytes = energy.to_be_bytes();

        self.buffer[self.index] = channel;
        self.buffer[{ self.index += 1; self.index }] = LPP_ENERGY;
        self.buffer[{ self.index += 1; self.index }] = energy_bytes[0];
        self.buffer[{ self.index += 1; self.index }] = energy_bytes[1];
        self.buffer[{ self.index += 1; self.index }] = energy_bytes[2];
        self.buffer[{ self.index += 1; self.index }] = energy_bytes[3];
        self.index += 1;

        Ok(())
    }

    /// Adds the payload for a direction to the Cayenne LPP data structure (in degrees)
    pub fn add_direction(&mut self, channel: u8, direction: u16) -> Result<(), Error> {
        if self.index + LPP_DIRECTION_SIZE > self.buffer.len() {
            return Err(Error::InsufficientMemory);
        }

        self.buffer[self.index] = channel;
        self.buffer[{ self.index += 1; self.index }] = LPP_DIRECTION;
        self.buffer[{ self.index += 1; self.index }] = (direction >> 8) as u8;
        self.buffer[{ self.index += 1; self.index }] = direction as u8;
        self.index += 1;
        
        Ok(())
    }

    /// Adds the payload for a unixtime to the Cayenne LPP data structure.
    /// The units are in seconds, and it's relative to unix epoch
    pub fn add_unixtime(&mut self, channel: u8, unixtime: u32) -> Result<(), Error> {
        if self.index + LPP_UNIXTIME_SIZE > self.buffer.len() {
            return Err(Error::InsufficientMemory);
        }

        let unixtime_bytes = unixtime.to_be_bytes();

        self.buffer[self.index] = channel;
        self.buffer[{ self.index += 1; self.index }] = LPP_UNIXTIME;
        self.buffer[{ self.index += 1; self.index }] = unixtime_bytes[0];
        self.buffer[{ self.index += 1; self.index }] = unixtime_bytes[1];
        self.buffer[{ self.index += 1; self.index }] = unixtime_bytes[2];
        self.buffer[{ self.index += 1; self.index }] = unixtime_bytes[3];
        self.index += 1;

        Ok(())
    }

    /// Adds the payload for a gyrometer to the Cayenne LPP data structure.
    pub fn add_gyrometer(&mut self, channel: u8, x: f32, y: f32, z: f32) -> Result<(), Error> {
        if self.index + LPP_GYROMETER_SIZE > self.buffer.len() {
            return Err(Error::InsufficientMemory);
        }

        // prepare axis values
        let vx: u16 = (x * 100.0) as u16;
        let vy: u16 = (y * 100.0) as u16;
        let vz: u16 = (z * 100.0) as u16;

        self.buffer[self.index] = channel;
        self.buffer[{ self.index += 1; self.index }] = LPP_GYROMETER;
        self.buffer[{ self.index += 1; self.index }] = (vx >> 8) as u8;
        self.buffer[{ self.index += 1; self.index }] = vx as u8;
        self.buffer[{ self.index += 1; self.index }] = (vy >> 8) as u8;
        self.buffer[{ self.index += 1; self.index }] = vy as u8;
        self.buffer[{ self.index += 1; self.index }] = (vz >> 8) as u8;
        self.buffer[{ self.index += 1; self.index }] = vz as u8;
        self.index += 1;

        Ok(())
    }

    /// Adds the payload for GPS to the Cayenne LPP data structure.
    pub fn add_gps(&mut self, channel: u8, latitude: f32, longitude: f32, meters: f32) -> Result<(), Error> {
        if self.index + LPP_GPS_SIZE > self.buffer.len() {
            return Err(Error::InsufficientMemory);
        }

        // Do bounds-checking on the GPS values
        if !(-90.0..=90.0).contains(&latitude) {
            return Err(Error::OutOfRange);
        }

        if !(-180.0..= 180.0).contains(&longitude) {
            return Err(Error::OutOfRange);
        }

        // prepare GPS values (3 bytes each)
        let vx: i32 = (latitude * 10000.0) as i32;
        let vy: i32 = (longitude * 10000.0) as i32;
        let vz: i32 = (meters * 100.0) as i32;

        self.buffer[self.index] = channel;
        self.buffer[{ self.index += 1; self.index }] = LPP_GPS;
        self.buffer[{ self.index += 1; self.index }] = (vx >> 16) as u8;
        self.buffer[{ self.index += 1; self.index }] = (vx >> 8) as u8;
        self.buffer[{ self.index += 1; self.index }] = vx as u8;
        self.buffer[{ self.index += 1; self.index }] = (vy >> 16) as u8;
        self.buffer[{ self.index += 1; self.index }] = (vy >> 8) as u8;
        self.buffer[{ self.index += 1; self.index }] = vy as u8;
        self.buffer[{ self.index += 1; self.index }] = (vz >> 16) as u8;
        self.buffer[{ self.index += 1; self.index }] = (vz >> 8) as u8;
        self.buffer[{ self.index += 1; self.index }] = vz as u8;
        self.index += 1;

        Ok(())
    }

    /// Adds the payload for switch to the Cayenne LPP data structure. It's a byte that's just 0/1
    pub fn add_switch(&mut self, channel: u8, value: bool) -> Result<(), Error> {
        if self.index + LPP_SWITCH_SIZE > self.buffer.len() {
            return Err(Error::InsufficientMemory);
        }

        self.buffer[self.index] = channel;
        self.buffer[{ self.index += 1; self.index }] = LPP_SWITCH;
        self.buffer[{ self.index += 1; self.index }] = if value { 1 } else { 0 };
        self.index += 1;

        Ok(())
    }

    /// Adds the payload for a concentration to the Cayenne LPP data structure (in ppm)
    pub fn add_concentration(&mut self, channel: u8, concentration: u16) -> Result<(), Error> {
        if self.index + LPP_CONCENTRATION_SIZE > self.buffer.len() {
            return Err(Error::InsufficientMemory);
        }

        self.buffer[self.index] = channel;
        self.buffer[{ self.index += 1; self.index }] = LPP_CONCENTRATION;
        self.buffer[{ self.index += 1; self.index }] = (concentration >> 8) as u8;
        self.buffer[{ self.index += 1; self.index }] = concentration as u8;
        self.index += 1;
        
        Ok(())
    }

    /// Adds the payload for color to the Cayenne LPP data structure. It's a byte per-color channel
    pub fn add_color(&mut self, channel: u8, red: u8, green: u8, blue: u8) -> Result<(), Error> {
        if self.index + LPP_COLOR_SIZE > self.buffer.len() {
            return Err(Error::InsufficientMemory);
        }

        self.buffer[self.index] = channel;
        self.buffer[{ self.index += 1; self.index }] = LPP_COLOR;
        self.buffer[{ self.index += 1; self.index }] = red;
        self.buffer[{ self.index += 1; self.index }] = green;
        self.buffer[{ self.index += 1; self.index }] = blue;
        self.index += 1;

        Ok(())
    }
}

impl<'a> IntoIterator for CayenneLPP<'a> {
    type Item = Result<CayenneLPPScalar, Error>;
    type IntoIter = CayenneLPPIntoFailableIterator<'a>;

    fn into_iter(self) -> Self::IntoIter {
        CayenneLPPIntoFailableIterator {
            lpp: self,
            index: 0
        }
    }
}