From b5d39ad75daa52a1e0d97b7f8403ca44b26bda13 Mon Sep 17 00:00:00 2001 From: Elmar van Rijnswou Date: Mon, 28 Aug 2017 02:03:13 +0200 Subject: [PATCH] Refactored. Removed stupid debugging. Pressing start will now sample for one second --- software/app/access_point.cpp | 36 +++++++++++++ software/app/access_point.h | 14 +++++ software/app/ads101x.cpp | 5 +- software/app/application.cpp | 93 ++++++++++++++++++++-------------- software/app/sensor_hub.cpp | 1 - software/app/web_interface.cpp | 2 + software/include/application.h | 2 + 7 files changed, 111 insertions(+), 42 deletions(-) create mode 100644 software/app/access_point.cpp create mode 100644 software/app/access_point.h diff --git a/software/app/access_point.cpp b/software/app/access_point.cpp new file mode 100644 index 0000000..d9a5bfe --- /dev/null +++ b/software/app/access_point.cpp @@ -0,0 +1,36 @@ +/* + * access_point.cpp + * + * Created on: Aug 28, 2017 + * Author: Elmar + */ + +#include +#include +#include "access_point.h" + +static void STADisconnect(String ssid, uint8_t ssid_len, uint8_t bssid[6], uint8_t reason) { + if (!WifiAccessPoint.isEnabled()) { + WifiStation.disconnect(); + WifiAccessPoint.enable(true); + WifiStation.connect(); + } +} + +static void STAGotIP(IPAddress ip, IPAddress mask, IPAddress gateway) { + if (WifiAccessPoint.isEnabled()) { + WifiAccessPoint.enable(false); + } +} + +void access_point_start() { + wifi_set_sleep_type(NONE_SLEEP_T); + WifiAccessPoint.setIP(IPAddress(10, 0, 0, 1)); + WifiAccessPoint.enable(true); + WifiEvents.onStationDisconnect(STADisconnect); + WifiEvents.onStationGotIP(STAGotIP); +} + +void access_point_config() { + WifiAccessPoint.config("Sensus", "", AUTH_OPEN, false, 3); +} diff --git a/software/app/access_point.h b/software/app/access_point.h new file mode 100644 index 0000000..b2f3d17 --- /dev/null +++ b/software/app/access_point.h @@ -0,0 +1,14 @@ +/* + * access_point.h + * + * Created on: Aug 28, 2017 + * Author: Elmar + */ + +#ifndef APP_ACCESS_POINT_H_ +#define APP_ACCESS_POINT_H_ + +void access_point_start(); +void access_point_config(); + +#endif /* APP_ACCESS_POINT_H_ */ diff --git a/software/app/ads101x.cpp b/software/app/ads101x.cpp index 0af9778..d86fc9d 100644 --- a/software/app/ads101x.cpp +++ b/software/app/ads101x.cpp @@ -120,7 +120,8 @@ uint16_t cADS101x::GetSettings(void) { } ads_voltage_t cADS101x::ConvertSample(ads_sample_t & sample) { //Raw sample is in (parts of) millivolts, go to micro to remove fractions - ads_voltage_t returnType = (sample.rawSample >> 4); //Multiply for extra precision + ads_voltage_t returnType = (sample.rawSample >> 4); + //Multiply for extra precision switch (sample.gain) { case eGainAmplifier::FSR_0_256: //one lsb is 0.125 mv = 125 uv @@ -156,7 +157,7 @@ ads_sample_t cADS101x::RawSample(void) { sample.gain = m_gain; sample.mux = m_mux; sample.rawSample = m_counter; - m_counter += 16; + m_counter += 8; return sample; #else OneShot(); diff --git a/software/app/application.cpp b/software/app/application.cpp index 9b490ea..11837a1 100644 --- a/software/app/application.cpp +++ b/software/app/application.cpp @@ -12,64 +12,95 @@ #include "web_interface.h" #include "signal_process.h" #include "sensor_hub.h" +#include "access_point.h" #include "tests.h" using namespace rijnfel; -void STADisconnect(String ssid, uint8_t ssid_len, uint8_t bssid[6], uint8_t reason); -void STAGotIP(IPAddress ip, IPAddress mask, IPAddress gateway); - cAdsConverter * adsConverter; //We want different signal processing for the channels cSignalProcess signalProcess[2]; Timer procTimer; Timer rectangleTimer; -uint8_t channel = 0; +Timer _stopTimer; light::cExcitationLight mylight; cSensorHub hub(HUB_PERIOD); ads::cADS101x ads1015(0, ADC_ADDRESS); +static bool _sensorStationRunning = false; +static bool _keepRunning = false; + +static void UpdateSensorHub() { + WDT.alive(); + hub.Update(); +} + +static void StopRunning() { + StopSensorStation(); +} + +void StartSensorStation(int seconds) { + if (_sensorStationRunning) { + return; + } + if (seconds == 0) { + _keepRunning = true; + } else { + _stopTimer.initializeMs(seconds * 1000, StopRunning).start(false); + _keepRunning = false; + } + procTimer.initializeUs(HUB_PERIOD, UpdateSensorHub).start(); + _sensorStationRunning = true; +} + +void StopSensorStation(void) { + if (!_sensorStationRunning) + return; + if (!_keepRunning) { + _stopTimer.stop(); + } + procTimer.initializeUs(HUB_PERIOD, UpdateSensorHub).stop(); + _sensorStationRunning = false; +} + void ChangeSampleChannel(int channel) { if (channel > 0 && channel < 5) { - hub.Stop(); + if (_sensorStationRunning) { + hub.Stop(); + } ads1015.SetMux(static_cast(ads::eInputMux::AIN_0 + channel - 1)); - hub.Start(); + if (_sensorStationRunning) { + hub.Start(); + } } } -void updateSensorHub() { - WDT.alive(); - hub.Update(); -} - void ready() { - WifiAccessPoint.config("Sensus", "", AUTH_OPEN, false, 3); hub.Start(); + access_point_config(); } void init() { spiffs_mount(); Serial.begin(460800); system_update_cpu_freq(SYS_CPU_160MHZ); - wifi_set_sleep_type(NONE_SLEEP_T); - + access_point_start(); System.onReady(ready); - WifiAccessPoint.setIP(IPAddress(10, 0, 0, 1)); - WifiAccessPoint.enable(true); - Wire.pins(4, 5); Wire.begin(); - //SET higher CPU freq & disable wifi sleep + Serial.systemDebugOutput(false); + delay(10); + system_set_os_print(0); // Turn off LED for measurements hub.Stop(); pinMode(LED_PIN, OUTPUT); digitalWrite(LED_PIN, 1); ads1015.SetMux(ads::eInputMux::AIN_0); - ads1015.SetSampleSpeed(ads::eSampleSpeed::SPS_2400); + ads1015.SetSampleSpeed(ads::eSampleSpeed::SPS_3300); ads1015.SetGain(ads::eGainAmplifier::FSR_4_096); ads1015.SetOneShot(false); hub.SetAdc(&ads1015); @@ -83,31 +114,15 @@ void init() { // Channel one and two are getting processed adsConverter->m_convertedSamples[0].Connect(&signalProcess[0].m_incommingData); adsConverter->m_convertedSamples[1].Connect(&signalProcess[1].m_incommingData); - //signalProcess[0].m_processedData.Connect(&cWebInterface::GetInstance()->m_adc_0); - //signalProcess[1].m_processedData.Connect(&cWebInterface::GetInstance()->m_adc_1); + signalProcess[0].m_processedData.Connect(&cWebInterface::GetInstance()->m_adc_0); + signalProcess[1].m_processedData.Connect(&cWebInterface::GetInstance()->m_adc_1); // Channel three and four are not - //adsConverter->m_convertedSamples[2].Connect(&cWebInterface::GetInstance()->m_adc_2); - //adsConverter->m_convertedSamples[3].Connect(&cWebInterface::GetInstance()->m_adc_3); + adsConverter->m_convertedSamples[2].Connect(&cWebInterface::GetInstance()->m_adc_2); + adsConverter->m_convertedSamples[3].Connect(&cWebInterface::GetInstance()->m_adc_3); - WifiEvents.onStationDisconnect(STADisconnect); - WifiEvents.onStationGotIP(STAGotIP); cWebInterface::GetInstance()->StartServer(); - procTimer.initializeUs(HUB_PERIOD, updateSensorHub).start(); mylight.SetCurrent(500); mylight.RectangleUpdate(); } -void STADisconnect(String ssid, uint8_t ssid_len, uint8_t bssid[6], uint8_t reason) { - if (!WifiAccessPoint.isEnabled()) { - WifiStation.disconnect(); - WifiAccessPoint.enable(true); - WifiStation.connect(); - } -} - -void STAGotIP(IPAddress ip, IPAddress mask, IPAddress gateway) { - if (WifiAccessPoint.isEnabled()) { - WifiAccessPoint.enable(false); - } -} diff --git a/software/app/sensor_hub.cpp b/software/app/sensor_hub.cpp index f39d331..40568b1 100644 --- a/software/app/sensor_hub.cpp +++ b/software/app/sensor_hub.cpp @@ -24,7 +24,6 @@ void cSensorHub::Update() { if (m_adcSettings) { if (m_adcSettings->ShouldSample(m_updatePeriod)) { if (m_adcSettings->m_buffer.AddValue(m_adc->RawSample())) { - //m_adcSettings->m_callback(m_adcSettings->m_buffer); m_adcSettings->m_samplesProvider.Push(static_cast(&m_adcSettings->m_buffer)); } } diff --git a/software/app/web_interface.cpp b/software/app/web_interface.cpp index 902cc3d..85f4101 100644 --- a/software/app/web_interface.cpp +++ b/software/app/web_interface.cpp @@ -40,8 +40,10 @@ void wsConnected(WebSocketConnection& socket) { void wsMessageReceived(WebSocketConnection& socket, const String& message) { if (message.equals("start")) { + StartSensorStation(1); cWebInterface::GetInstance()->m_sendWebsocket = true; } else { + StopSensorStation(); cWebInterface::GetInstance()->m_sendWebsocket = false; } if (message.equals("ch1")) { diff --git a/software/include/application.h b/software/include/application.h index 8b53c46..bfdac16 100644 --- a/software/include/application.h +++ b/software/include/application.h @@ -9,6 +9,8 @@ #define INCLUDE_APPLICATION_H_ //Disgusting +void StartSensorStation(int seconds); +void StopSensorStation(void); void ChangeSampleChannel(int channel); #endif /* INCLUDE_APPLICATION_H_ */