From 6ae7b44df70d408f8f249745639f1b05371fddf4 Mon Sep 17 00:00:00 2001 From: Elmar van Rijnswou Date: Wed, 23 Aug 2017 11:05:36 +0200 Subject: [PATCH] refactoring continues --- software/app/ads101x.h | 76 +++++++++++++++++++++ software/app/ads_converter.h | 30 +++++++++ software/app/dac101c085.h | 72 ++++++++++++++++++++ software/app/temperature.h | 28 ++++++++ software/app/web_interface.h | 127 +++++++++++++++++++++++++++++++++++ 5 files changed, 333 insertions(+) create mode 100644 software/app/ads101x.h create mode 100644 software/app/ads_converter.h create mode 100644 software/app/dac101c085.h create mode 100644 software/app/temperature.h create mode 100644 software/app/web_interface.h diff --git a/software/app/ads101x.h b/software/app/ads101x.h new file mode 100644 index 0000000..e236ac8 --- /dev/null +++ b/software/app/ads101x.h @@ -0,0 +1,76 @@ +/* + * ADS101x.h + * + * Created on: Jul 20, 2017 + * Author: Elmar + */ + +#ifndef APP_ADS101X_H_ +#define APP_ADS101X_H_ + +#include +#include +#include "../include/rijnfel_core/adc.h" + +namespace rijnfel { + +namespace ads { + +//Voltage in uV +typedef int32_t ads_voltage_t; + +enum eInputMux { + AIN_0_1, AIN_0_3, AIN_1_3, AIN_2_3, AIN_0, AIN_1, AIN_2, AIN_3 +}; +enum eGainAmplifier { + FSR_6_144, FSR_4_096, FSR_2_048, FSR_1_024, FSR_0_512, FSR_0_256 +}; +enum eSampleSpeed { + SPS_128, SPS_250, SPS_490, SPS_920, SPS_1600, SPS_2400, SPS_3300 +}; + +struct ads_sample_t { + uint16_t rawSample; + enum eInputMux mux; + enum eGainAmplifier gain; +}; + +class cADS101x: public cADC { +public: + + cADS101x(uint8_t i_address = 0x48); + cADS101x(uint8_t i_readyPin, uint8_t i_address = 0x48); + + void SetOneShot(bool i_oneShot = true); + void SetGain(enum eGainAmplifier i_gain); + void SetMux(enum eInputMux i_mux); + void SetSampleSpeed(enum eSampleSpeed i_speed); + virtual void WaitSample(void); + virtual bool IsReady(void); + uint16_t GetSettings(void); + + virtual ads_sample_t RawSample(void); + virtual ads_voltage_t ConvertSample(ads_sample_t & sample); + virtual ~cADS101x() { + + } +private: + uint16_t CreateSettings(uint8_t i_startSample); + uint16_t ReadRegister(uint8_t i_register); + void WriteSettings(uint16_t i_settings); + void SetDefaults(void); + void OneShot(void); +private: + uint8_t m_address, m_readyPin; + bool m_oneShot; + enum eInputMux m_mux; + enum eGainAmplifier m_gain; + enum eSampleSpeed m_sampleSpeed; +private: +#ifdef DUMMY_ADC + uint16_t m_counter; +#endif +}; +} +} +#endif /* APP_ADS101X_H_ */ diff --git a/software/app/ads_converter.h b/software/app/ads_converter.h new file mode 100644 index 0000000..29a52bd --- /dev/null +++ b/software/app/ads_converter.h @@ -0,0 +1,30 @@ +/* + * ads_converter.h + * + * Created on: Aug 22, 2017 + * Author: Elmar + */ + +#ifndef APP_ADS_CONVERTER_H_ +#define APP_ADS_CONVERTER_H_ + +#include "../include/rijnfel_core/rijnfel_core.h" +#include "ads101x.h" + +namespace rijnfel { + +class cAdsConverter: public cDataSink { +public: + cAdsConverter(cADC & i_adc); + virtual void ReceiveCallback(void * i_data, cDataReceiver * i_provider); + // Every channel has its own provider, after this we can not distinguish anymore + cDataProvider m_convertedSamples[4]; + cDataReceiver m_rawSamples; + virtual ~cAdsConverter(); +private: + cADC & m_adc; +}; + +} /* namespace rijnfel */ + +#endif /* APP_ADS_CONVERTER_H_ */ diff --git a/software/app/dac101c085.h b/software/app/dac101c085.h new file mode 100644 index 0000000..dd0b509 --- /dev/null +++ b/software/app/dac101c085.h @@ -0,0 +1,72 @@ +//---------------------------------------------------------------------------------------------------------------------------- +// Project: Uppsense +// Name: dac101c085.h +// Author: Maximilian Stiefel +// Date: 08.08.2017 +// +// Description: Small simple driver for the TI DAC101C085. +// +//---------------------------------------------------------------------------------------------------------------------------- + +#ifndef APP_DAC101C085_H_ +#define APP_DAC101C085_H_ + +//-------------------------------------Libraries------------------------------------------------------------------------------ +#include +#include + +//-------------------------------------Defines-------------------------------------------------------------------------------- +#define PD_BITS_POS 13 // Position of power down bits +#define D_BITS_POS 2 // Position of data bits + +//-------------------------------------Namespaces----------------------------------------------------------------------------- +namespace rijnfel { +namespace dac { + +//-------------------------------------Enums---------------------------------------------------------------------------------- +/** Represents the operational mode of the DAC. + */ +enum eOpMode {NORMAL, PULL_DOWN_2K5, PULL_DOWN_100K, HIGH_IMPEDANCE}; + +//-------------------------------------cDAC101085----------------------------------------------------------------------------- +/** Small simple driver for the TI DAC101C085. + */ +class cDAC101C085 +{ +public: + /** @brief Instantiate DAC, assign a number and an address. + * @param i_dac_number Number of the DAC. + * @param i_address I2C address of this DAC. + */ + cDAC101C085(uint8_t i_dac_number, uint8_t i_address); // Constructor + + /** @brief Free DAC, is currently doing nothing. + */ + ~cDAC101C085(); // Destructor + + /** @brief Check if the device is answering on the bus. + * @retval uint8_t Returns 1 on success, 0 when failing. + */ + uint8_t CheckDev(); // Check if device is available and alive + + /** @brief Read out 16 bit register of the DAC. + * @retval uint16_t 16 bit read from the DAC. + */ + uint16_t ReadSettings(void); + + /** @brief Write new setting to DAC register. + * @retval uint8_t Returns 1 on success, 0 when failing. + */ + uint8_t ChangeSettings(enum eOpMode mode, uint16_t val); + +private: + uint8_t m_dac_number; // Which of the dacs is it? + uint8_t m_address; + uint8_t WriteSettings(uint16_t settings); + uint8_t I2CError(uint8_t error); +}; + +} +} + +#endif /* APP_DAC101C085_H_ */ diff --git a/software/app/temperature.h b/software/app/temperature.h new file mode 100644 index 0000000..f104cfe --- /dev/null +++ b/software/app/temperature.h @@ -0,0 +1,28 @@ +/* + * temperature.h + * + * Created on: Aug 1, 2017 + * Author: Elmar + */ + +#ifndef APP_TEMPERATURE_H_ +#define APP_TEMPERATURE_H_ + +#include "../include/rijnfel_core/rijnfel_core.h" + +namespace rijnfel { +template +class cTemperatureSensor: cSensor { +public: + cTemperatureSensor() { + + } + virtual Temperature ConvertSample(Sample sample) = 0; + virtual ~cTemperatureSensor() { + + } +}; + +} + +#endif /* APP_TEMPERATURE_H_ */ diff --git a/software/app/web_interface.h b/software/app/web_interface.h new file mode 100644 index 0000000..bfdcd4a --- /dev/null +++ b/software/app/web_interface.h @@ -0,0 +1,127 @@ +/* + * web_interface.h + * + * Created on: Aug 6, 2017 + * Author: Elmar + */ + +#ifndef APP_WEB_INTERFACE_H_ +#define APP_WEB_INTERFACE_H_ + +//TODO remove this +#include "ads101x.h" + +#include +#include + +namespace rijnfel { +#define RAW_CHANNEL 1 +#define RAW_SAMPLES 1000 +class cWebInterface: public cDataSink { +public: + /** + * Returns the global cWebInterface instance. + * Only one webserver should ever be running + * @return global cWebInterface instance + */ + static cWebInterface *GetInstance() { + if (cWebInterface::s_instance == NULL) + cWebInterface::s_instance = new cWebInterface(); + return cWebInterface::s_instance; + } + + /// Singleton instance + static cWebInterface *s_instance; + +public: + /** + * Should not be used because it's a singleton + * @param delete + */ + cWebInterface(cWebInterface const&) = delete; + + /** + * Should not be used because it's a singleton + * @param delete + * @return delete + */ + cWebInterface& operator=(cWebInterface const&) = delete; + + /** + * Registers the paths and starts the server + */ + void StartServer(); + + /** + * Stops the server + */ + void StopServer(); + + /** + * This is a debug function that prints out the average values + * @todo remove this + */ + void PrintValues(); + + /** + * Destructor + */ + virtual ~cWebInterface(); + + // This is what the future will look like + // cDataReceiver m_sensorVal[2]; + // cDataReceiver m_battery; + // cDataReceiver m_ledLife; + // cDataReceiver m_temp; + // But for now... + cDataReceiver m_adc_0; + cDataReceiver m_adc_1; + cDataReceiver m_adc_2; + cDataReceiver m_adc_3; + + virtual void ReceiveCallback(void * i_data, cDataReceiver * i_receiver); +public: + /** + * + * @param i_request + * @param i_response Response to the client, index.html + */ + void OnIndex(HttpRequest & i_request, HttpResponse & i_response); + + /** + * Function gets called from the mobile to refresh the average values + * @param i_request Request from the client + * @param i_response Response to the client, contains JSON with averages + */ + void OnRefresh(HttpRequest & i_request, HttpResponse & i_response); + + /** + * + * @param i_request + * @param i_response + */ + void OnRawUpdate(HttpRequest& i_request, HttpResponse& i_response); + void OnConfiguration_json(HttpRequest &request, HttpResponse &response); + void OnConfiguration(HttpRequest &request, HttpResponse &response); + + //TODO migrate this + WebSocketConnection * m_connectedUser; + bool m_sendWebsocket; +protected: + /** + * Initializes the variables + */ + cWebInterface(); +private: + void ResetRawValues(); + /// Whether the server has started or not + bool m_serverStarted; + /// Holds the average for all the channels + int32_t m_adc_value_average[4]; + /// Server instance + HttpServer m_server; +}; + +} /* namespace rijnfel */ + +#endif /* APP_WEB_INTERFACE_H_ */