Fork of my MQTTGateway

Dependencies:   mbed-http

Committer:
vpcola
Date:
Sat Apr 08 14:45:51 2017 +0000
Revision:
0:f1d3878b8dd9
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
vpcola 0:f1d3878b8dd9 1 /**
vpcola 0:f1d3878b8dd9 2 * Copyright (c) 2015 Digi International Inc.,
vpcola 0:f1d3878b8dd9 3 * All rights not expressly granted are reserved.
vpcola 0:f1d3878b8dd9 4 *
vpcola 0:f1d3878b8dd9 5 * This Source Code Form is subject to the terms of the Mozilla Public
vpcola 0:f1d3878b8dd9 6 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
vpcola 0:f1d3878b8dd9 7 * You can obtain one at http://mozilla.org/MPL/2.0/.
vpcola 0:f1d3878b8dd9 8 *
vpcola 0:f1d3878b8dd9 9 * Digi International Inc. 11001 Bren Road East, Minnetonka, MN 55343
vpcola 0:f1d3878b8dd9 10 * =======================================================================
vpcola 0:f1d3878b8dd9 11 */
vpcola 0:f1d3878b8dd9 12
vpcola 0:f1d3878b8dd9 13 #include "XBeeLib.h"
vpcola 0:f1d3878b8dd9 14 #include "IO/IOSampleDM.h"
vpcola 0:f1d3878b8dd9 15
vpcola 0:f1d3878b8dd9 16 #define IO_SAMPLE_DM_MIN_SIZE (2 + 1 + 2)
vpcola 0:f1d3878b8dd9 17
vpcola 0:f1d3878b8dd9 18 using namespace XBeeLib;
vpcola 0:f1d3878b8dd9 19
vpcola 0:f1d3878b8dd9 20 IOSampleDM::IOSampleDM(const uint8_t* const raw_data, size_t size)
vpcola 0:f1d3878b8dd9 21 {
vpcola 0:f1d3878b8dd9 22 if (raw_data == NULL || size == 0) {
vpcola 0:f1d3878b8dd9 23 _digital_mask = 0;
vpcola 0:f1d3878b8dd9 24 _analog_mask = 0;
vpcola 0:f1d3878b8dd9 25 return;
vpcola 0:f1d3878b8dd9 26 }
vpcola 0:f1d3878b8dd9 27 assert(size >= IO_SAMPLE_DM_MIN_SIZE);
vpcola 0:f1d3878b8dd9 28 assert(size <= sizeof _sampled_data);
vpcola 0:f1d3878b8dd9 29
vpcola 0:f1d3878b8dd9 30 _digital_mask = UINT16(raw_data[1], raw_data[2]);
vpcola 0:f1d3878b8dd9 31 _analog_mask = raw_data[3];
vpcola 0:f1d3878b8dd9 32 _sampled_data_size = size - 4;
vpcola 0:f1d3878b8dd9 33 memcpy(&_sampled_data[0], &raw_data[4], _sampled_data_size);
vpcola 0:f1d3878b8dd9 34 }
vpcola 0:f1d3878b8dd9 35
vpcola 0:f1d3878b8dd9 36 IOSampleDM::~IOSampleDM()
vpcola 0:f1d3878b8dd9 37 {
vpcola 0:f1d3878b8dd9 38
vpcola 0:f1d3878b8dd9 39 }
vpcola 0:f1d3878b8dd9 40
vpcola 0:f1d3878b8dd9 41 RadioStatus IOSampleDM::get_dio(XBeeDM::IoLine line, DioVal* const dio_value) const
vpcola 0:f1d3878b8dd9 42 {
vpcola 0:f1d3878b8dd9 43 const uint16_t mask = 1 << line;
vpcola 0:f1d3878b8dd9 44 if (mask & _digital_mask) {
vpcola 0:f1d3878b8dd9 45 const uint16_t digital_channels = get_digital_channels();
vpcola 0:f1d3878b8dd9 46
vpcola 0:f1d3878b8dd9 47 *dio_value = digital_channels & mask ? High : Low;
vpcola 0:f1d3878b8dd9 48 return Success;
vpcola 0:f1d3878b8dd9 49 }
vpcola 0:f1d3878b8dd9 50 return Failure;
vpcola 0:f1d3878b8dd9 51 }
vpcola 0:f1d3878b8dd9 52
vpcola 0:f1d3878b8dd9 53 RadioStatus IOSampleDM::get_adc(XBeeDM::IoLine line, uint16_t* const val) const
vpcola 0:f1d3878b8dd9 54 {
vpcola 0:f1d3878b8dd9 55 const uint8_t line_mask = 1 << line;
vpcola 0:f1d3878b8dd9 56 const bool adc_present = line_mask & _analog_mask;
vpcola 0:f1d3878b8dd9 57 if (!adc_present) {
vpcola 0:f1d3878b8dd9 58 return Failure;
vpcola 0:f1d3878b8dd9 59 }
vpcola 0:f1d3878b8dd9 60
vpcola 0:f1d3878b8dd9 61 uint8_t analog_data_idx = _digital_mask == 0 ? 0 : 2;
vpcola 0:f1d3878b8dd9 62 uint8_t line_sample = 0;
vpcola 0:f1d3878b8dd9 63
vpcola 0:f1d3878b8dd9 64 while (analog_data_idx < _sampled_data_size) {
vpcola 0:f1d3878b8dd9 65 if (_analog_mask & (1 << line_sample)) {
vpcola 0:f1d3878b8dd9 66 if (line_sample == line) {
vpcola 0:f1d3878b8dd9 67 /* Write the analog value */
vpcola 0:f1d3878b8dd9 68 *val = UINT16(_sampled_data[analog_data_idx], _sampled_data[analog_data_idx + 1]);
vpcola 0:f1d3878b8dd9 69 break;
vpcola 0:f1d3878b8dd9 70 }
vpcola 0:f1d3878b8dd9 71 analog_data_idx += 2;
vpcola 0:f1d3878b8dd9 72 }
vpcola 0:f1d3878b8dd9 73 line_sample++;
vpcola 0:f1d3878b8dd9 74 }
vpcola 0:f1d3878b8dd9 75
vpcola 0:f1d3878b8dd9 76 return Success;
vpcola 0:f1d3878b8dd9 77 }
vpcola 0:f1d3878b8dd9 78
vpcola 0:f1d3878b8dd9 79 inline uint16_t IOSampleDM::get_digital_channels(void) const
vpcola 0:f1d3878b8dd9 80 {
vpcola 0:f1d3878b8dd9 81 if (_digital_mask == 0) {
vpcola 0:f1d3878b8dd9 82 return 0;
vpcola 0:f1d3878b8dd9 83 }
vpcola 0:f1d3878b8dd9 84 return UINT16(_sampled_data[0], _sampled_data[1]);
vpcola 0:f1d3878b8dd9 85 }
vpcola 0:f1d3878b8dd9 86