Vergil Cola
/
MQTTGateway2
Fork of my original MQTTGateway
XbeeMonitor/XBeeLib/IO/IOSampleDM.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/IOSampleDM.h" |
vpcola | 0:a1734fe1ec4b | 15 | |
vpcola | 0:a1734fe1ec4b | 16 | #define IO_SAMPLE_DM_MIN_SIZE (2 + 1 + 2) |
vpcola | 0:a1734fe1ec4b | 17 | |
vpcola | 0:a1734fe1ec4b | 18 | using namespace XBeeLib; |
vpcola | 0:a1734fe1ec4b | 19 | |
vpcola | 0:a1734fe1ec4b | 20 | IOSampleDM::IOSampleDM(const uint8_t* const raw_data, size_t size) |
vpcola | 0:a1734fe1ec4b | 21 | { |
vpcola | 0:a1734fe1ec4b | 22 | if (raw_data == NULL || size == 0) { |
vpcola | 0:a1734fe1ec4b | 23 | _digital_mask = 0; |
vpcola | 0:a1734fe1ec4b | 24 | _analog_mask = 0; |
vpcola | 0:a1734fe1ec4b | 25 | return; |
vpcola | 0:a1734fe1ec4b | 26 | } |
vpcola | 0:a1734fe1ec4b | 27 | assert(size >= IO_SAMPLE_DM_MIN_SIZE); |
vpcola | 0:a1734fe1ec4b | 28 | assert(size <= sizeof _sampled_data); |
vpcola | 0:a1734fe1ec4b | 29 | |
vpcola | 0:a1734fe1ec4b | 30 | _digital_mask = UINT16(raw_data[1], raw_data[2]); |
vpcola | 0:a1734fe1ec4b | 31 | _analog_mask = raw_data[3]; |
vpcola | 0:a1734fe1ec4b | 32 | _sampled_data_size = size - 4; |
vpcola | 0:a1734fe1ec4b | 33 | memcpy(&_sampled_data[0], &raw_data[4], _sampled_data_size); |
vpcola | 0:a1734fe1ec4b | 34 | } |
vpcola | 0:a1734fe1ec4b | 35 | |
vpcola | 0:a1734fe1ec4b | 36 | IOSampleDM::~IOSampleDM() |
vpcola | 0:a1734fe1ec4b | 37 | { |
vpcola | 0:a1734fe1ec4b | 38 | |
vpcola | 0:a1734fe1ec4b | 39 | } |
vpcola | 0:a1734fe1ec4b | 40 | |
vpcola | 0:a1734fe1ec4b | 41 | RadioStatus IOSampleDM::get_dio(XBeeDM::IoLine line, DioVal* const dio_value) const |
vpcola | 0:a1734fe1ec4b | 42 | { |
vpcola | 0:a1734fe1ec4b | 43 | const uint16_t mask = 1 << line; |
vpcola | 0:a1734fe1ec4b | 44 | if (mask & _digital_mask) { |
vpcola | 0:a1734fe1ec4b | 45 | const uint16_t digital_channels = get_digital_channels(); |
vpcola | 0:a1734fe1ec4b | 46 | |
vpcola | 0:a1734fe1ec4b | 47 | *dio_value = digital_channels & mask ? High : Low; |
vpcola | 0:a1734fe1ec4b | 48 | return Success; |
vpcola | 0:a1734fe1ec4b | 49 | } |
vpcola | 0:a1734fe1ec4b | 50 | return Failure; |
vpcola | 0:a1734fe1ec4b | 51 | } |
vpcola | 0:a1734fe1ec4b | 52 | |
vpcola | 0:a1734fe1ec4b | 53 | RadioStatus IOSampleDM::get_adc(XBeeDM::IoLine line, uint16_t* const val) const |
vpcola | 0:a1734fe1ec4b | 54 | { |
vpcola | 0:a1734fe1ec4b | 55 | const uint8_t line_mask = 1 << line; |
vpcola | 0:a1734fe1ec4b | 56 | const bool adc_present = line_mask & _analog_mask; |
vpcola | 0:a1734fe1ec4b | 57 | if (!adc_present) { |
vpcola | 0:a1734fe1ec4b | 58 | return Failure; |
vpcola | 0:a1734fe1ec4b | 59 | } |
vpcola | 0:a1734fe1ec4b | 60 | |
vpcola | 0:a1734fe1ec4b | 61 | uint8_t analog_data_idx = _digital_mask == 0 ? 0 : 2; |
vpcola | 0:a1734fe1ec4b | 62 | uint8_t line_sample = 0; |
vpcola | 0:a1734fe1ec4b | 63 | |
vpcola | 0:a1734fe1ec4b | 64 | while (analog_data_idx < _sampled_data_size) { |
vpcola | 0:a1734fe1ec4b | 65 | if (_analog_mask & (1 << line_sample)) { |
vpcola | 0:a1734fe1ec4b | 66 | if (line_sample == line) { |
vpcola | 0:a1734fe1ec4b | 67 | /* Write the analog value */ |
vpcola | 0:a1734fe1ec4b | 68 | *val = UINT16(_sampled_data[analog_data_idx], _sampled_data[analog_data_idx + 1]); |
vpcola | 0:a1734fe1ec4b | 69 | break; |
vpcola | 0:a1734fe1ec4b | 70 | } |
vpcola | 0:a1734fe1ec4b | 71 | analog_data_idx += 2; |
vpcola | 0:a1734fe1ec4b | 72 | } |
vpcola | 0:a1734fe1ec4b | 73 | line_sample++; |
vpcola | 0:a1734fe1ec4b | 74 | } |
vpcola | 0:a1734fe1ec4b | 75 | |
vpcola | 0:a1734fe1ec4b | 76 | return Success; |
vpcola | 0:a1734fe1ec4b | 77 | } |
vpcola | 0:a1734fe1ec4b | 78 | |
vpcola | 0:a1734fe1ec4b | 79 | inline uint16_t IOSampleDM::get_digital_channels(void) const |
vpcola | 0:a1734fe1ec4b | 80 | { |
vpcola | 0:a1734fe1ec4b | 81 | if (_digital_mask == 0) { |
vpcola | 0:a1734fe1ec4b | 82 | return 0; |
vpcola | 0:a1734fe1ec4b | 83 | } |
vpcola | 0:a1734fe1ec4b | 84 | return UINT16(_sampled_data[0], _sampled_data[1]); |
vpcola | 0:a1734fe1ec4b | 85 | } |
vpcola | 0:a1734fe1ec4b | 86 |