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/IOSample802.h"
vpcola 0:f1d3878b8dd9 15 #include "Utils/Debug.h"
vpcola 0:f1d3878b8dd9 16
vpcola 0:f1d3878b8dd9 17 #define IO_SAMPLE_802_DIGITAL_INPUTS_MASK 0x01FF
vpcola 0:f1d3878b8dd9 18 #define IO_SAMPLE_802_DIGITAL_INPUTS_COUNT 9
vpcola 0:f1d3878b8dd9 19 #define IO_SAMPLE_802_MIN_SIZE (2 + 2)
vpcola 0:f1d3878b8dd9 20
vpcola 0:f1d3878b8dd9 21 using namespace XBeeLib;
vpcola 0:f1d3878b8dd9 22
vpcola 0:f1d3878b8dd9 23 IOSample802::IOSample802(const uint8_t* const raw_data, size_t size)
vpcola 0:f1d3878b8dd9 24 {
vpcola 0:f1d3878b8dd9 25 if (raw_data == NULL || size == 0) {
vpcola 0:f1d3878b8dd9 26 _channel_mask = 0;
vpcola 0:f1d3878b8dd9 27 return;
vpcola 0:f1d3878b8dd9 28 }
vpcola 0:f1d3878b8dd9 29 assert(size >= IO_SAMPLE_802_MIN_SIZE);
vpcola 0:f1d3878b8dd9 30 assert(size <= sizeof _sampled_data);
vpcola 0:f1d3878b8dd9 31
vpcola 0:f1d3878b8dd9 32 _channel_mask = UINT16(raw_data[1], raw_data[2]);
vpcola 0:f1d3878b8dd9 33 _sampled_data_size = size - 3;
vpcola 0:f1d3878b8dd9 34 memcpy(&_sampled_data[0], &raw_data[3], _sampled_data_size);
vpcola 0:f1d3878b8dd9 35 }
vpcola 0:f1d3878b8dd9 36
vpcola 0:f1d3878b8dd9 37 IOSample802::~IOSample802()
vpcola 0:f1d3878b8dd9 38 {
vpcola 0:f1d3878b8dd9 39
vpcola 0:f1d3878b8dd9 40 }
vpcola 0:f1d3878b8dd9 41
vpcola 0:f1d3878b8dd9 42 RadioStatus IOSample802::get_dio(XBee802::IoLine line, DioVal* const dio_value) const
vpcola 0:f1d3878b8dd9 43 {
vpcola 0:f1d3878b8dd9 44 if (line > XBee802::DI8) {
vpcola 0:f1d3878b8dd9 45 digi_log(LogLevelError, "get_dio: Pin %d not supported as IO\r\n", line);
vpcola 0:f1d3878b8dd9 46 return Failure;
vpcola 0:f1d3878b8dd9 47 }
vpcola 0:f1d3878b8dd9 48
vpcola 0:f1d3878b8dd9 49 const uint16_t mask = 1 << line;
vpcola 0:f1d3878b8dd9 50 if (mask & _channel_mask) {
vpcola 0:f1d3878b8dd9 51 const uint8_t digital_channels = get_dio_channels();
vpcola 0:f1d3878b8dd9 52
vpcola 0:f1d3878b8dd9 53 *dio_value = digital_channels & mask ? High : Low;
vpcola 0:f1d3878b8dd9 54 return Success;
vpcola 0:f1d3878b8dd9 55 }
vpcola 0:f1d3878b8dd9 56 return Failure;
vpcola 0:f1d3878b8dd9 57 }
vpcola 0:f1d3878b8dd9 58
vpcola 0:f1d3878b8dd9 59 RadioStatus IOSample802::get_adc(XBee802::IoLine line, uint16_t* const val) const
vpcola 0:f1d3878b8dd9 60 {
vpcola 0:f1d3878b8dd9 61 if (line > XBee802::DIO5_AD5) {
vpcola 0:f1d3878b8dd9 62 digi_log(LogLevelError, "get_adc: Pin %d not supported as ADC\r\n", line);
vpcola 0:f1d3878b8dd9 63 return Failure;
vpcola 0:f1d3878b8dd9 64 }
vpcola 0:f1d3878b8dd9 65 const uint8_t analog_mask = _channel_mask >> IO_SAMPLE_802_DIGITAL_INPUTS_COUNT;
vpcola 0:f1d3878b8dd9 66 const uint8_t line_mask = 1 << line;
vpcola 0:f1d3878b8dd9 67 const bool adc_present = line_mask & analog_mask;
vpcola 0:f1d3878b8dd9 68 if (!adc_present) {
vpcola 0:f1d3878b8dd9 69 return Failure;
vpcola 0:f1d3878b8dd9 70 }
vpcola 0:f1d3878b8dd9 71
vpcola 0:f1d3878b8dd9 72 uint8_t analog_data_idx = dio_channels_present() == 0 ? 0 : 2;
vpcola 0:f1d3878b8dd9 73 uint8_t line_sample = 0;
vpcola 0:f1d3878b8dd9 74
vpcola 0:f1d3878b8dd9 75 while (analog_data_idx < _sampled_data_size) {
vpcola 0:f1d3878b8dd9 76 if (analog_mask & (1 << line_sample)) {
vpcola 0:f1d3878b8dd9 77 if (line_sample == line) {
vpcola 0:f1d3878b8dd9 78 /* Write the analog value */
vpcola 0:f1d3878b8dd9 79 *val = UINT16(_sampled_data[analog_data_idx], _sampled_data[analog_data_idx + 1]);
vpcola 0:f1d3878b8dd9 80 break;
vpcola 0:f1d3878b8dd9 81 }
vpcola 0:f1d3878b8dd9 82 analog_data_idx += 2;
vpcola 0:f1d3878b8dd9 83 }
vpcola 0:f1d3878b8dd9 84 line_sample++;
vpcola 0:f1d3878b8dd9 85 }
vpcola 0:f1d3878b8dd9 86
vpcola 0:f1d3878b8dd9 87 return Success;
vpcola 0:f1d3878b8dd9 88 }
vpcola 0:f1d3878b8dd9 89
vpcola 0:f1d3878b8dd9 90 inline bool IOSample802::dio_channels_present(void) const
vpcola 0:f1d3878b8dd9 91 {
vpcola 0:f1d3878b8dd9 92 return _channel_mask & IO_SAMPLE_802_DIGITAL_INPUTS_MASK;
vpcola 0:f1d3878b8dd9 93 }
vpcola 0:f1d3878b8dd9 94
vpcola 0:f1d3878b8dd9 95 inline uint8_t IOSample802::get_dio_channels(void) const
vpcola 0:f1d3878b8dd9 96 {
vpcola 0:f1d3878b8dd9 97 if (dio_channels_present()) {
vpcola 0:f1d3878b8dd9 98 return UINT16(_sampled_data[0], _sampled_data[1]);
vpcola 0:f1d3878b8dd9 99 } else {
vpcola 0:f1d3878b8dd9 100 return 0;
vpcola 0:f1d3878b8dd9 101 }
vpcola 0:f1d3878b8dd9 102 }