Vergil Cola
/
MQTTGateway2
Fork of my original MQTTGateway
XbeeMonitor/XBeeLib/IO/IOSample802.cpp@0:a1734fe1ec4b, 2017-04-08 (annotated)
- Committer:
- vpcola
- Date:
- Sat Apr 08 14:43:14 2017 +0000
- Revision:
- 0:a1734fe1ec4b
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
vpcola | 0:a1734fe1ec4b | 1 | /** |
vpcola | 0:a1734fe1ec4b | 2 | * Copyright (c) 2015 Digi International Inc., |
vpcola | 0:a1734fe1ec4b | 3 | * All rights not expressly granted are reserved. |
vpcola | 0:a1734fe1ec4b | 4 | * |
vpcola | 0:a1734fe1ec4b | 5 | * This Source Code Form is subject to the terms of the Mozilla Public |
vpcola | 0:a1734fe1ec4b | 6 | * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
vpcola | 0:a1734fe1ec4b | 7 | * You can obtain one at http://mozilla.org/MPL/2.0/. |
vpcola | 0:a1734fe1ec4b | 8 | * |
vpcola | 0:a1734fe1ec4b | 9 | * Digi International Inc. 11001 Bren Road East, Minnetonka, MN 55343 |
vpcola | 0:a1734fe1ec4b | 10 | * ======================================================================= |
vpcola | 0:a1734fe1ec4b | 11 | */ |
vpcola | 0:a1734fe1ec4b | 12 | |
vpcola | 0:a1734fe1ec4b | 13 | #include "XBeeLib.h" |
vpcola | 0:a1734fe1ec4b | 14 | #include "IO/IOSample802.h" |
vpcola | 0:a1734fe1ec4b | 15 | #include "Utils/Debug.h" |
vpcola | 0:a1734fe1ec4b | 16 | |
vpcola | 0:a1734fe1ec4b | 17 | #define IO_SAMPLE_802_DIGITAL_INPUTS_MASK 0x01FF |
vpcola | 0:a1734fe1ec4b | 18 | #define IO_SAMPLE_802_DIGITAL_INPUTS_COUNT 9 |
vpcola | 0:a1734fe1ec4b | 19 | #define IO_SAMPLE_802_MIN_SIZE (2 + 2) |
vpcola | 0:a1734fe1ec4b | 20 | |
vpcola | 0:a1734fe1ec4b | 21 | using namespace XBeeLib; |
vpcola | 0:a1734fe1ec4b | 22 | |
vpcola | 0:a1734fe1ec4b | 23 | IOSample802::IOSample802(const uint8_t* const raw_data, size_t size) |
vpcola | 0:a1734fe1ec4b | 24 | { |
vpcola | 0:a1734fe1ec4b | 25 | if (raw_data == NULL || size == 0) { |
vpcola | 0:a1734fe1ec4b | 26 | _channel_mask = 0; |
vpcola | 0:a1734fe1ec4b | 27 | return; |
vpcola | 0:a1734fe1ec4b | 28 | } |
vpcola | 0:a1734fe1ec4b | 29 | assert(size >= IO_SAMPLE_802_MIN_SIZE); |
vpcola | 0:a1734fe1ec4b | 30 | assert(size <= sizeof _sampled_data); |
vpcola | 0:a1734fe1ec4b | 31 | |
vpcola | 0:a1734fe1ec4b | 32 | _channel_mask = UINT16(raw_data[1], raw_data[2]); |
vpcola | 0:a1734fe1ec4b | 33 | _sampled_data_size = size - 3; |
vpcola | 0:a1734fe1ec4b | 34 | memcpy(&_sampled_data[0], &raw_data[3], _sampled_data_size); |
vpcola | 0:a1734fe1ec4b | 35 | } |
vpcola | 0:a1734fe1ec4b | 36 | |
vpcola | 0:a1734fe1ec4b | 37 | IOSample802::~IOSample802() |
vpcola | 0:a1734fe1ec4b | 38 | { |
vpcola | 0:a1734fe1ec4b | 39 | |
vpcola | 0:a1734fe1ec4b | 40 | } |
vpcola | 0:a1734fe1ec4b | 41 | |
vpcola | 0:a1734fe1ec4b | 42 | RadioStatus IOSample802::get_dio(XBee802::IoLine line, DioVal* const dio_value) const |
vpcola | 0:a1734fe1ec4b | 43 | { |
vpcola | 0:a1734fe1ec4b | 44 | if (line > XBee802::DI8) { |
vpcola | 0:a1734fe1ec4b | 45 | digi_log(LogLevelError, "get_dio: Pin %d not supported as IO\r\n", line); |
vpcola | 0:a1734fe1ec4b | 46 | return Failure; |
vpcola | 0:a1734fe1ec4b | 47 | } |
vpcola | 0:a1734fe1ec4b | 48 | |
vpcola | 0:a1734fe1ec4b | 49 | const uint16_t mask = 1 << line; |
vpcola | 0:a1734fe1ec4b | 50 | if (mask & _channel_mask) { |
vpcola | 0:a1734fe1ec4b | 51 | const uint8_t digital_channels = get_dio_channels(); |
vpcola | 0:a1734fe1ec4b | 52 | |
vpcola | 0:a1734fe1ec4b | 53 | *dio_value = digital_channels & mask ? High : Low; |
vpcola | 0:a1734fe1ec4b | 54 | return Success; |
vpcola | 0:a1734fe1ec4b | 55 | } |
vpcola | 0:a1734fe1ec4b | 56 | return Failure; |
vpcola | 0:a1734fe1ec4b | 57 | } |
vpcola | 0:a1734fe1ec4b | 58 | |
vpcola | 0:a1734fe1ec4b | 59 | RadioStatus IOSample802::get_adc(XBee802::IoLine line, uint16_t* const val) const |
vpcola | 0:a1734fe1ec4b | 60 | { |
vpcola | 0:a1734fe1ec4b | 61 | if (line > XBee802::DIO5_AD5) { |
vpcola | 0:a1734fe1ec4b | 62 | digi_log(LogLevelError, "get_adc: Pin %d not supported as ADC\r\n", line); |
vpcola | 0:a1734fe1ec4b | 63 | return Failure; |
vpcola | 0:a1734fe1ec4b | 64 | } |
vpcola | 0:a1734fe1ec4b | 65 | const uint8_t analog_mask = _channel_mask >> IO_SAMPLE_802_DIGITAL_INPUTS_COUNT; |
vpcola | 0:a1734fe1ec4b | 66 | const uint8_t line_mask = 1 << line; |
vpcola | 0:a1734fe1ec4b | 67 | const bool adc_present = line_mask & analog_mask; |
vpcola | 0:a1734fe1ec4b | 68 | if (!adc_present) { |
vpcola | 0:a1734fe1ec4b | 69 | return Failure; |
vpcola | 0:a1734fe1ec4b | 70 | } |
vpcola | 0:a1734fe1ec4b | 71 | |
vpcola | 0:a1734fe1ec4b | 72 | uint8_t analog_data_idx = dio_channels_present() == 0 ? 0 : 2; |
vpcola | 0:a1734fe1ec4b | 73 | uint8_t line_sample = 0; |
vpcola | 0:a1734fe1ec4b | 74 | |
vpcola | 0:a1734fe1ec4b | 75 | while (analog_data_idx < _sampled_data_size) { |
vpcola | 0:a1734fe1ec4b | 76 | if (analog_mask & (1 << line_sample)) { |
vpcola | 0:a1734fe1ec4b | 77 | if (line_sample == line) { |
vpcola | 0:a1734fe1ec4b | 78 | /* Write the analog value */ |
vpcola | 0:a1734fe1ec4b | 79 | *val = UINT16(_sampled_data[analog_data_idx], _sampled_data[analog_data_idx + 1]); |
vpcola | 0:a1734fe1ec4b | 80 | break; |
vpcola | 0:a1734fe1ec4b | 81 | } |
vpcola | 0:a1734fe1ec4b | 82 | analog_data_idx += 2; |
vpcola | 0:a1734fe1ec4b | 83 | } |
vpcola | 0:a1734fe1ec4b | 84 | line_sample++; |
vpcola | 0:a1734fe1ec4b | 85 | } |
vpcola | 0:a1734fe1ec4b | 86 | |
vpcola | 0:a1734fe1ec4b | 87 | return Success; |
vpcola | 0:a1734fe1ec4b | 88 | } |
vpcola | 0:a1734fe1ec4b | 89 | |
vpcola | 0:a1734fe1ec4b | 90 | inline bool IOSample802::dio_channels_present(void) const |
vpcola | 0:a1734fe1ec4b | 91 | { |
vpcola | 0:a1734fe1ec4b | 92 | return _channel_mask & IO_SAMPLE_802_DIGITAL_INPUTS_MASK; |
vpcola | 0:a1734fe1ec4b | 93 | } |
vpcola | 0:a1734fe1ec4b | 94 | |
vpcola | 0:a1734fe1ec4b | 95 | inline uint8_t IOSample802::get_dio_channels(void) const |
vpcola | 0:a1734fe1ec4b | 96 | { |
vpcola | 0:a1734fe1ec4b | 97 | if (dio_channels_present()) { |
vpcola | 0:a1734fe1ec4b | 98 | return UINT16(_sampled_data[0], _sampled_data[1]); |
vpcola | 0:a1734fe1ec4b | 99 | } else { |
vpcola | 0:a1734fe1ec4b | 100 | return 0; |
vpcola | 0:a1734fe1ec4b | 101 | } |
vpcola | 0:a1734fe1ec4b | 102 | } |