XBee modules

Dependencies:   DigiLogger

Fork of XBeeLib by Digi International Inc.

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers IOSample802.cpp Source File

IOSample802.cpp

00001 /**
00002  * Copyright (c) 2015 Digi International Inc.,
00003  * All rights not expressly granted are reserved.
00004  *
00005  * This Source Code Form is subject to the terms of the Mozilla Public
00006  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
00007  * You can obtain one at http://mozilla.org/MPL/2.0/.
00008  *
00009  * Digi International Inc. 11001 Bren Road East, Minnetonka, MN 55343
00010  * =======================================================================
00011  */
00012 
00013 #include "XBeeLib.h"
00014 #include "IO/IOSample802.h"
00015 #include "Utils/Debug.h"
00016 
00017 #define IO_SAMPLE_802_DIGITAL_INPUTS_MASK   0x01FF
00018 #define IO_SAMPLE_802_DIGITAL_INPUTS_COUNT  9
00019 #define IO_SAMPLE_802_MIN_SIZE              (2 + 2)
00020 
00021 using namespace XBeeLib;
00022 
00023 IOSample802::IOSample802(const uint8_t* const raw_data, size_t size)
00024 {
00025     if (raw_data == NULL || size == 0) {
00026         _channel_mask = 0;
00027         return;
00028     }
00029     assert(size >= IO_SAMPLE_802_MIN_SIZE);
00030     assert(size <= sizeof _sampled_data);
00031 
00032     _channel_mask = UINT16(raw_data[1], raw_data[2]);
00033     _sampled_data_size = size - 3;
00034     memcpy(&_sampled_data[0], &raw_data[3], _sampled_data_size);
00035 }
00036 
00037 IOSample802::~IOSample802()
00038 {
00039 
00040 }
00041 
00042 RadioStatus IOSample802::get_dio(XBee802::IoLine line, DioVal* const dio_value) const
00043 {
00044     if (line > XBee802::DI8) {
00045         digi_log(LogLevelError, "get_dio: Pin %d not supported as IO\r\n", line);
00046         return Failure;
00047     }
00048 
00049     const uint16_t mask = 1 << line;
00050     if (mask & _channel_mask) {
00051         const uint8_t digital_channels = get_dio_channels();
00052 
00053         *dio_value = digital_channels & mask ? High : Low;
00054         return Success;
00055     }
00056     return Failure;
00057 }
00058 
00059 RadioStatus IOSample802::get_adc(XBee802::IoLine line, uint16_t* const val) const
00060 {
00061     if (line > XBee802::DIO5_AD5) {
00062         digi_log(LogLevelError, "get_adc: Pin %d not supported as ADC\r\n", line);
00063         return Failure;
00064     }
00065     const uint8_t analog_mask = _channel_mask >> IO_SAMPLE_802_DIGITAL_INPUTS_COUNT;
00066     const uint8_t line_mask = 1 << line;
00067     const bool adc_present = line_mask & analog_mask;
00068     if (!adc_present) {
00069         return Failure;
00070     }
00071 
00072     uint8_t analog_data_idx = dio_channels_present() == 0 ? 0 : 2;
00073     uint8_t line_sample = 0;
00074 
00075     while (analog_data_idx < _sampled_data_size) {
00076         if (analog_mask & (1 << line_sample)) {
00077             if (line_sample == line) {
00078                 /* Write the analog value */
00079                 *val = UINT16(_sampled_data[analog_data_idx], _sampled_data[analog_data_idx + 1]);
00080                 break;
00081             }
00082             analog_data_idx += 2;
00083         }
00084         line_sample++;
00085     }
00086 
00087     return Success;
00088 }
00089 
00090 inline bool IOSample802::dio_channels_present(void) const
00091 {
00092     return _channel_mask & IO_SAMPLE_802_DIGITAL_INPUTS_MASK;
00093 }
00094 
00095 inline uint8_t IOSample802::get_dio_channels(void) const
00096 {
00097     if (dio_channels_present()) {
00098         return UINT16(_sampled_data[0], _sampled_data[1]);
00099     } else {
00100         return 0;
00101     }
00102 }