DigiMEsh DIOs, ADCs and PWMs example for mbed XBeeLib By Digi

Dependencies:   XBeeLib mbed

Fork of XBeeZB_dio_adc by Digi International Inc.

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /**
00002  * Copyright (c) 2015 Digi International Inc.,
00003  * All rights not expressly granted are reserved.
00004  *
00005  * This Source Code Form is subject to the terms of the Mozilla Public
00006  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
00007  * You can obtain one at http://mozilla.org/MPL/2.0/.
00008  *
00009  * Digi International Inc. 11001 Bren Road East, Minnetonka, MN 55343
00010  * =======================================================================
00011  */
00012 
00013 #include "mbed.h"
00014 #include "XBeeLib.h"
00015 #if defined(ENABLE_LOGGING)
00016 #include "DigiLoggerMbedSerial.h"
00017 using namespace DigiLog;
00018 #endif
00019 
00020 #define REMOTE_NODE_ADDR64_MSB  ((uint32_t)0x0013A200)
00021 
00022 #error "Replace next define with the LSB of the remote module's 64-bit address (SL parameter)"
00023 #define REMOTE_NODE_ADDR64_LSB  ((uint32_t)0x01234567)
00024 
00025 #define REMOTE_NODE_ADDR64      UINT64(REMOTE_NODE_ADDR64_MSB, REMOTE_NODE_ADDR64_LSB)
00026 
00027 using namespace XBeeLib;
00028 
00029 Serial *log_serial;
00030 
00031 int main()
00032 {
00033     log_serial = new Serial(DEBUG_TX, DEBUG_RX);
00034     log_serial->baud(9600);
00035     log_serial->printf("Sample application to demo how to handle remote XBeeDM devices DIOs and ADCs\r\n\r\n");
00036     log_serial->printf(XB_LIB_BANNER);
00037 
00038 #if defined(ENABLE_LOGGING)
00039     new DigiLoggerMbedSerial(log_serial, LogLevelInfo);
00040 #endif
00041 
00042     XBeeDM xbee = XBeeDM(RADIO_TX, RADIO_RX, RADIO_RESET, NC, NC, 9600);
00043 
00044     RadioStatus radioStatus = xbee.init();
00045     MBED_ASSERT(radioStatus == Success);
00046 
00047     const RemoteXBeeDM remoteDevice = RemoteXBeeDM(REMOTE_NODE_ADDR64);
00048 
00049     radioStatus = xbee.set_pin_config(remoteDevice, XBeeDM::DIO3_AD3, DigitalInput);
00050     MBED_ASSERT(radioStatus == Success);
00051 
00052     radioStatus = xbee.set_pin_pull_up(remoteDevice, XBeeDM::DIO3_AD3, true);
00053     MBED_ASSERT(radioStatus == Success);
00054 
00055     radioStatus = xbee.set_pin_config(remoteDevice, XBeeDM::DIO4, DigitalOutHigh);
00056     MBED_ASSERT(radioStatus == Success);
00057 
00058     radioStatus = xbee.set_pin_config(remoteDevice, XBeeDM::DIO2_AD2, Adc);
00059     MBED_ASSERT(radioStatus == Success);
00060 
00061     radioStatus = xbee.set_pin_config(remoteDevice, XBeeDM::DIO10_PWM0, Pwm);
00062     MBED_ASSERT(radioStatus == Success);
00063 
00064     while(true) {
00065         IOSampleDM ioSample = xbee.get_iosample(remoteDevice);
00066 
00067         if (!ioSample.is_valid()) {
00068             log_serial->printf("get_iosample failed, ADC and Digital Input reads will be invalid\r\n");
00069         }
00070         /* Read DIO3_AD3 digital value */
00071         DioVal dio3_val;
00072 
00073         radioStatus = ioSample.get_dio(XBeeDM::DIO3_AD3, &dio3_val);
00074         MBED_ASSERT(radioStatus == Success);
00075         log_serial->printf("DIO3 value = %d\r\n", dio3_val);
00076 
00077         /* Toggle LED associated to DIO4 */
00078         static bool led_on = false;
00079         if (!led_on)
00080             radioStatus = xbee.set_dio(remoteDevice, XBeeDM::DIO4, Low);
00081         else
00082             radioStatus = xbee.set_dio(remoteDevice, XBeeDM::DIO4, High);
00083         MBED_ASSERT(radioStatus == Success);
00084         led_on = !led_on;
00085 
00086         /* Read DIO2_AD2 analog value */
00087         uint16_t adc2_val;
00088         radioStatus = ioSample.get_adc(XBeeDM::DIO2_AD2, &adc2_val);
00089         MBED_ASSERT(radioStatus == Success);
00090         log_serial->printf("ADC2 value = 0x%04x\r\n", adc2_val);
00091 
00092         /* Set the led associated to PWM0 to different intensity levels */
00093         static float pwm_val_list[] = { 0.0, 50.0, 70.0, 100.0 };
00094         static uint8_t pwm_val_idx = 0;
00095         log_serial->printf("Setting PWM0 to = %f\r\n", pwm_val_list[pwm_val_idx]);
00096         radioStatus = xbee.set_pwm(remoteDevice, XBeeDM::DIO10_PWM0, pwm_val_list[pwm_val_idx]);
00097         MBED_ASSERT(radioStatus == Success);
00098         pwm_val_idx++;
00099         if (pwm_val_idx == sizeof(pwm_val_list)/sizeof(pwm_val_list[0])) {
00100             pwm_val_idx = 0;
00101         }
00102 
00103         log_serial->printf("\r\n");
00104 
00105         wait(5);
00106     }
00107 
00108     delete(log_serial);
00109 }