Library to easily communicate with XBee modules.
Fork of XBeeLib by
IOSampleZB.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/IOSampleZB.h" 00015 00016 #define IO_SAMPLE_ZB_MIN_SIZE (2 + 1 + 2) 00017 00018 using namespace XBeeLib; 00019 00020 IOSampleZB::IOSampleZB(const uint8_t* const raw_data, size_t size) 00021 { 00022 if (raw_data == NULL || size == 0) { 00023 _digital_mask = 0; 00024 _analog_mask = 0; 00025 return; 00026 } 00027 assert(size >= IO_SAMPLE_ZB_MIN_SIZE); 00028 assert(size <= sizeof _sampled_data); 00029 00030 _digital_mask = UINT16(raw_data[1], raw_data[2]); 00031 _analog_mask = raw_data[3]; 00032 _sampled_data_size = size - 4; 00033 memcpy(&_sampled_data[0], &raw_data[4], _sampled_data_size); 00034 } 00035 00036 IOSampleZB::~IOSampleZB() 00037 { 00038 00039 } 00040 00041 RadioStatus IOSampleZB::get_dio(XBeeZB::IoLine line, DioVal* const dio_value) const 00042 { 00043 const uint16_t mask = 1 << line; 00044 if (mask & _digital_mask) { 00045 const uint16_t digital_channels = get_digital_channels(); 00046 00047 *dio_value = digital_channels & mask ? High : Low; 00048 return Success; 00049 } 00050 return Failure; 00051 } 00052 00053 RadioStatus IOSampleZB::get_adc(XBeeZB::IoLine line, uint16_t* const val) const 00054 { 00055 const uint8_t line_mask = 1 << line; 00056 const bool adc_present = line_mask & _analog_mask; 00057 if (!adc_present) { 00058 return Failure; 00059 } 00060 00061 uint8_t analog_data_idx = _digital_mask == 0 ? 0 : 2; 00062 uint8_t line_sample = 0; 00063 00064 while (analog_data_idx < _sampled_data_size) { 00065 if (_analog_mask & (1 << line_sample)) { 00066 if (line_sample == line) { 00067 /* Write the analog value */ 00068 *val = UINT16(_sampled_data[analog_data_idx], _sampled_data[analog_data_idx + 1]); 00069 break; 00070 } 00071 analog_data_idx += 2; 00072 } 00073 line_sample++; 00074 } 00075 00076 return Success; 00077 } 00078 00079 inline uint16_t IOSampleZB::get_digital_channels(void) const 00080 { 00081 if (_digital_mask == 0) { 00082 return 0; 00083 } 00084 return UINT16(_sampled_data[0], _sampled_data[1]); 00085 } 00086
Generated on Tue Jul 12 2022 18:10:50 by 1.7.2