Maxim Integrated / Mbed OS MAXREFDES101_SOURCE

Dependencies:   max32630fthr Adafruit_FeatherOLED USBDevice

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers DSInterface.h Source File

DSInterface.h

00001 /*******************************************************************************
00002  * Copyright (C) 2017 Maxim Integrated Products, Inc., All Rights Reserved.
00003  *
00004  * Permission is hereby granted, free of charge, to any person obtaining a
00005  * copy of this software and associated documentation files (the "Software"),
00006  * to deal in the Software without restriction, including without limitation
00007  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
00008  * and/or sell copies of the Software, and to permit persons to whom the
00009  * Software is furnished to do so, subject to the following conditions:
00010  *
00011  * The above copyright notice and this permission notice shall be included
00012  * in all copies or substantial portions of the Software.
00013  *
00014  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00015  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00016  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
00017  * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
00018  * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
00019  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
00020  * OTHER DEALINGS IN THE SOFTWARE.
00021  *
00022  * Except as contained in this notice, the name of Maxim Integrated
00023  * Products, Inc. shall not be used except as stated in the Maxim Integrated
00024  * Products, Inc. Branding Policy.
00025  *
00026  * The mere transfer of this software does not imply any licenses
00027  * of trade secrets, proprietary technology, copyrights, patents,
00028  * trademarks, maskwork rights, or any other form of intellectual
00029  * property whatsoever. Maxim Integrated Products, Inc. retains all
00030  * ownership rights.
00031  *******************************************************************************
00032  */
00033 
00034 #ifndef _DSINTERFACE_H_
00035 #define _DSINTERFACE_H_
00036 
00037 #include "mbed.h"
00038 #include <stdint.h>
00039 #include "SensorComm.h"
00040 #include "MaximSensor.h"
00041 #include "USBSerial.h"
00042 #include "../../Utilities/queue.h"
00043 #include "FATFileSystem.h"
00044 
00045 
00046 #define COMM_SUCCESS        0
00047 #define COMM_GENERAL_ERROR  -1
00048 #define COMM_INVALID_PARAM  -254
00049 #define COMM_NOT_RECOGNIZED -255
00050 
00051 #define FLASH_ERR_GENERAL   -1
00052 #define FLASH_ERR_CHECKSUM  -2
00053 #define FLASH_ERR_AUTH      -3
00054 
00055 #define DS_MAX_NUM_SENSORCOMMS  8
00056 
00057 #define DS_BINARY_PACKET_START_BYTE 0xAA
00058 
00059 #define FLASH_MESSAGE_SIZE     32
00060 #define CIRCULAR_QUEUE_COUNT  256
00061 #define FLASH_MESSAGE_OFFSET    1
00062 
00063 
00064 /**
00065  * @brief   DSInterface is Maxim's DeviceStudio Interface class
00066  */
00067 class DSInterface
00068 {
00069 public:
00070 
00071     /* PUBLIC FUNCTION DECLARATIONS */
00072     /**
00073     * @brief    DSInterface constructor.
00074     *
00075     * @param[in]    fw_version Firmware version number.
00076     * @param[in]    fw_platform Firmware platform name.
00077     * @param[in]    USB Pointer to USBSerial device
00078     */
00079     DSInterface(USBSerial* USB);
00080 
00081     /**
00082     * @brief    DSInterface destructor.
00083     *
00084     */
00085     virtual ~DSInterface();
00086 
00087     /**
00088     * @brief    Add SensorComm for DSInterface to pass commands to
00089     *
00090     * @param[in]    s SensorComm instance to handle sensor commands.
00091     */
00092     void add_sensor_comm(SensorComm *s);
00093 
00094     /**
00095     * @brief    Command builder from i/o device.
00096     * @details  Reads character and builds command from DeviceStudio commands.
00097     *
00098     * @param[in]    ch Input character from i/o device.
00099     */
00100     void build_command(char ch);
00101 
00102     /**
00103     * @brief    Prints latest sensor data to USB stream
00104     * @details  Should be called on every pass thorugh the main loop in main.cpp
00105     *
00106     */
00107     void data_report_execute();
00108 
00109     void handle_file_close();
00110 
00111     void force_file_close();
00112 
00113     void swap_queues();
00114 
00115     /**
00116     * @brief    Set the fw version which DSInterface will replay with for "get_device_info" command
00117     *
00118     * @param[in]    fw_version Firmware version number.
00119     */
00120     void set_fw_version(const char *fw_version);
00121 
00122     /**
00123      * @brief    get the algorithms define within sesnor hub fw version
00124      *
00125      * @param[out]    algorihm description string
00126      */
00127     int get_fw_algorithms( void );
00128     /**
00129     * @brief    Set the fw platform which DSInterface will replay with for "get_device_info" command
00130     *
00131     * @param[in]    fw_platform Firmware platform name.
00132     */
00133     void set_fw_platform(const char *fw_platform);
00134 
00135     /**
00136      * @brief   Enable data output on the console interface (USB)
00137      */
00138     void enable_console_interface();
00139 
00140     void ds_set_ble_status(bool en);
00141 
00142     void ds_set_flash_rec_status(bool en);
00143 
00144     void stopcommand();
00145     void parse_command_str(const char* cmd );
00146     uint8_t  algo_report_mode; // sets the mode for report 1/2
00147 
00148 
00149  //   FATFileSystem *logfilesystem;
00150     FILE *flashFileHandle;
00151 
00152 
00153     bool recordingStarted;
00154 
00155 
00156 protected:
00157 
00158     /* PROTECTED FUNCTION DECLARATIONS */
00159     /**
00160     * @brief    Parses DeviceStudio commands.
00161     * @details  Parses and executes commands. Prints return code to i/o device.
00162     */
00163     void parse_command();
00164 
00165     void write_to_flash_thread_funct();
00166 
00167 
00168     /* PROTECTED VARIABLES */
00169     USBSerial *m_USB;
00170     Mutex comm_mutex;
00171 
00172     int num_sensors;
00173     SensorComm* sensor_list[DS_MAX_NUM_SENSORCOMMS];
00174 
00175     volatile uint8_t ds_console_interface_exists_;
00176     volatile uint8_t ds_ble_interface_exists_;
00177 
00178     const char* platform_name;
00179     const char* firmware_version;
00180 
00181     char algo_desc_string[128];
00182     int algo_desc_strsz;
00183 
00184     static const uint32_t CONSOLE_STR_BUF_SZ = 1024;
00185 
00186     char cmd_str[CONSOLE_STR_BUF_SZ];
00187     int cmd_idx;
00188     bool silent_mode;
00189     bool pause_mode;
00190     char lcd_time_str[30];
00191     int lcd_time_count;
00192     int lcd_time_val;
00193 
00194     //buffer to hold string that will be written into the log file
00195     char logbuf[1024];
00196 
00197     //Flag to check which type of data streaming will occur
00198     char stream_type;
00199 
00200     // flash logging variables
00201     queue_t ds_queue_one_;
00202     queue_t ds_queue_two_;
00203 
00204     queue_t *ds_queue_flash_;
00205     queue_t *ds_queue_fill_;
00206 
00207     unsigned char buffer_one_[CIRCULAR_QUEUE_COUNT * FLASH_MESSAGE_SIZE];
00208     unsigned char buffer_two_[CIRCULAR_QUEUE_COUNT * FLASH_MESSAGE_SIZE];
00209 
00210     EventQueue event_queue_;
00211     Thread flash_thread_;
00212 };
00213 
00214 #endif /* _DSINTERFACE_H_ */