Xbee s2b for lpc11u24
Dependencies: DigiLogger
Fork of XBeeLib by
Diff: IO/IOSample802.cpp
- Revision:
- 0:fcaad0dfa051
- Child:
- 3:8662ebe83570
diff -r 000000000000 -r fcaad0dfa051 IO/IOSample802.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/IO/IOSample802.cpp Fri May 08 11:50:56 2015 +0200 @@ -0,0 +1,87 @@ +#include "XBeeLib.h" +#include "IO/IOSample802.h" +#include "Utils/Debug.h" + +#define IO_SAMPLE_802_DIGITAL_INPUTS_MASK 0x01FF +#define IO_SAMPLE_802_DIGITAL_INPUTS_COUNT 9 +#define IO_SAMPLE_802_MIN_SIZE (2 + 2) + +using namespace XBeeLib; + +IOSample802::IOSample802(const uint8_t* const raw_data, size_t size) +{ + assert(size >= IO_SAMPLE_802_MIN_SIZE); + assert(size <= sizeof _sampled_data); + + _channel_mask = UINT16(raw_data[1], raw_data[2]); + _sampled_data_size = size - 3; + memcpy(&_sampled_data[0], &raw_data[3], _sampled_data_size); +} + +IOSample802::~IOSample802() +{ + +} + +RadioStatus IOSample802::get_dio(XBee802::IoLine line, DioVal* const dio_value) const +{ + if (line > XBee802::DI8) { + digi_log(LogLevelError, "get_dio: Pin %d not supported as IO\r\n", line); + return Failure; + } + + const uint16_t mask = 1 << line; + if (mask & _channel_mask) { + const uint8_t digital_channels = get_dio_channels(); + + *dio_value = digital_channels & mask ? High : Low; + return Success; + } + return Failure; +} + +RadioStatus IOSample802::get_adc(XBee802::IoLine line, uint16_t* const val) const +{ + if (line > XBee802::DIO5_AD5) { + digi_log(LogLevelError, "get_adc: Pin %d not supported as ADC\r\n", line); + return Failure; + } + const uint8_t analog_mask = _channel_mask >> IO_SAMPLE_802_DIGITAL_INPUTS_COUNT; + const uint8_t line_mask = 1 << line; + const bool adc_present = line_mask & analog_mask; + if (!adc_present) { + return Failure; + } + + uint8_t analog_data_idx = dio_channels_present() == 0 ? 0 : 2; + uint8_t line_sample = 0; + + while (analog_data_idx < _sampled_data_size) { + if (analog_mask & (1 << line_sample)) { + if (line_sample == line) { + /* Write the analog value */ + *val = UINT16(_sampled_data[analog_data_idx], _sampled_data[analog_data_idx + 1]); + break; + } + analog_data_idx += 2; + } + line_sample++; + } + + return Success; +} + +inline bool IOSample802::dio_channels_present(void) const +{ + return _channel_mask & IO_SAMPLE_802_DIGITAL_INPUTS_MASK; +} + +inline uint8_t IOSample802::get_dio_channels(void) const +{ + if (dio_channels_present()) { + return UINT16(_sampled_data[0], _sampled_data[1]); + } + else { + return 0; + } +}