Browse Source

User interface works. Have to redo the communication, step away from string compares, and get nice json.

software_develop
Elmar van Rijnswou 7 years ago
parent
commit
0f4d8cb832
  1. 40
      software/app/application.cpp
  2. 85
      software/app/web_interface.cpp
  3. 9
      software/app/web_interface.h
  4. 8
      software/files/index.html
  5. 70
      software/files/index.js
  6. 14
      software/include/application.h

40
software/app/application.cpp

@ -1,16 +1,17 @@
#include <user_config.h>
#include <SmingCore/SmingCore.h>
#include <SmingCore/HardwareSerial.h>
#include "ads101x.h"
#include <hardware.h>
#include "excitation_light.h"
#include "sensor_hub.h"
#include "sensor_settings.h"
#include "double_buffer.h"
#include "web_interface.h"
#include <stdint.h>
#include "ads_converter.h"
#include "signal_process.h"
#include <application.h>
#include <hardware.h>
#include "sensor_hub.h"
#include "ads101x.h"
using namespace rijnfel;
@ -23,11 +24,18 @@ cAdsConverter * adsConverter;
cSignalProcess signalProcess[2];
Timer procTimer;
Timer rectangleTimer;
ads::cADS101x adc(0, ADC_ADDRESS);
uint8_t channel = 0;
light::cExcitationLight mylight;
cSensorHub hub(HUB_PERIOD);
ads::cADS101x ads1015(0, ADC_ADDRESS);
void ChangeSampleChannel(int channel) {
if (channel > 0 && channel < 5) {
hub.Stop();
ads1015.SetMux(static_cast<ads::eInputMux>(ads::eInputMux::AIN_0 + channel - 1));
hub.Start();
}
}
void SettingsTest() {
channel++;
@ -35,8 +43,8 @@ void SettingsTest() {
channel = 0;
cWebInterface::GetInstance()->PrintValues();
}
adc.SetMux(static_cast<ads::eInputMux>(ads::eInputMux::AIN_0 + channel));
Serial.printf("Settings: %d\n\r", adc.GetSettings());
ads1015.SetMux(static_cast<ads::eInputMux>(ads::eInputMux::AIN_0 + channel));
Serial.printf("Settings: %d\n\r", ads1015.GetSettings());
}
void AdcTest() {
@ -44,9 +52,9 @@ void AdcTest() {
if (channel > 3) {
channel = 0;
}
adc.SetMux(static_cast<ads::eInputMux>(ads::eInputMux::AIN_0 + channel));
ads::ads_sample_t sample = adc.RawSample();
Serial.printf("raw: %d converted: %d channel: %d\n\r", sample.rawSample, adc.ConvertSample(sample), sample.mux);
ads1015.SetMux(static_cast<ads::eInputMux>(ads::eInputMux::AIN_0 + channel));
ads::ads_sample_t sample = ads1015.RawSample();
Serial.printf("raw: %d converted: %d channel: %d\n\r", sample.rawSample, ads1015.ConvertSample(sample), sample.mux);
}
void updateSensorHub() {
@ -81,12 +89,12 @@ void init() {
hub.Stop();
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, 1);
adc.SetMux(ads::eInputMux::AIN_0);
adc.SetSampleSpeed(ads::eSampleSpeed::SPS_3300);
adc.SetGain(ads::eGainAmplifier::FSR_4_096);
adc.SetOneShot(false);
hub.SetAdc(&adc);
adsConverter = new cAdsConverter(adc);
ads1015.SetMux(ads::eInputMux::AIN_0);
ads1015.SetSampleSpeed(ads::eSampleSpeed::SPS_3300);
ads1015.SetGain(ads::eGainAmplifier::FSR_4_096);
ads1015.SetOneShot(false);
hub.SetAdc(&ads1015);
adsConverter = new cAdsConverter(ads1015);
cSensorSettings<ads::ads_sample_t> * adcSettings;
adcSettings = new cSensorSettings<ads::ads_sample_t>(ADC_TIMEBASE, ADC_PERIOD);

85
software/app/web_interface.cpp

@ -7,10 +7,13 @@
#include "web_interface.h"
#include "helper_structs.h"
#include <application.h>
#include <SmingCore/SmingCore.h>
#include <SmingCore/Network/WebConstants.h>
#include <SmingCore/FileSystem.h>
#include <third-party/http-parser/http_parser.h>
#include <SmingCore/Network/Http/Websocket/WebsocketResource.h>
namespace rijnfel {
cWebInterface *cWebInterface::s_instance = 0;
@ -31,16 +34,43 @@ static void onConfiguration_json(HttpRequest &request, HttpResponse &response) {
cWebInterface::GetInstance()->OnConfiguration_json(request, response);
}
void wsConnected(WebSocketConnection& socket) {
cWebInterface::GetInstance()->m_connectedUser = &socket;
Serial.printf("Connection!");
}
void wsMessageReceived(WebSocketConnection& socket, const String& message) {
if (message.equals("start")) {
cWebInterface::GetInstance()->m_sendWebsocket = true;
} else {
cWebInterface::GetInstance()->m_sendWebsocket = false;
}
if (message.equals("ch1")) {
ChangeSampleChannel(1);
} else if (message.equals("ch2")) {
ChangeSampleChannel(2);
} else if (message.equals("ch3")) {
ChangeSampleChannel(3);
} else if (message.equals("ch4")) {
ChangeSampleChannel(4);
}
}
void wsDisconnected(WebSocketConnection& socket) {
cWebInterface::GetInstance()->m_connectedUser = NULL;
}
cWebInterface::cWebInterface() :
m_serverStarted(false), m_adc_0(this), m_adc_1(this), m_adc_2(this), m_adc_3(this) {
m_serverStarted(false), m_adc_0(this), m_adc_1(this), m_adc_2(this), m_adc_3(this), m_connectedUser(NULL), m_sendWebsocket(
false) {
for (int i = 0; i < 4; i++) {
m_adc_value_average[i] = 0;
}
// Integer requires 8 digits, and one for the comma
m_jsonBuffer = new char[1];
if (m_jsonBuffer == NULL) {
Serial.print("Not enough ram");
}
// m_jsonBuffer = new char[1];
// if (m_jsonBuffer == NULL) {
// Serial.print("Not enough ram");
//}
// TODO Auto-generated constructor stub
}
@ -59,19 +89,25 @@ void onFile(HttpRequest & i_request, HttpResponse & i_response) {
}
static void onChannel(HttpRequest & i_request, HttpResponse & i_response) {
cWebInterface::GetInstance()->OnFile(i_request, i_response);
//cWebInterface::GetInstance()->OnFile(i_request, i_response);
}
void cWebInterface::StartServer() {
if (m_serverStarted)
return;
WebsocketResource* wsResource = new WebsocketResource();
wsResource->setConnectionHandler(wsConnected);
wsResource->setMessageHandler(wsMessageReceived);
wsResource->setDisconnectionHandler(wsDisconnected);
m_server.addPath("/", onIndex);
m_server.addPath("/channel", onChannel);
m_server.addPath("/state", onRefresh);
m_server.addPath("/config", onConfiguration);
m_server.addPath("/config.json", onConfiguration_json);
m_server.addPath("/ws", wsResource);
m_server.setDefaultHandler(onFile);
m_server.listen(80);
}
void cWebInterface::StopServer() {
@ -107,8 +143,14 @@ void cWebInterface::OnRawUpdate(HttpRequest& i_request, HttpResponse& i_response
void cWebInterface::ReceiveCallback(void* i_data, cDataReceiver* i_receiver) {
int64_t average = 0;
sSizedArray * arr = static_cast<sSizedArray *>(i_data);
char buf[12];
for (int i = 0; i < arr->size; i++) {
average += arr->array[i];
if (m_connectedUser != NULL && m_sendWebsocket) {
sprintf(buf, "%d,", arr->array[i]);
const String send(buf);
m_connectedUser->sendString(send);
}
}
average /= arr->size;
if (i_receiver == &m_adc_0) {
@ -120,7 +162,6 @@ void cWebInterface::ReceiveCallback(void* i_data, cDataReceiver* i_receiver) {
} else if (i_receiver == &m_adc_3) {
m_adc_value_average[3] = static_cast<int32_t>(average);
}
PrintValues();
}
void cWebInterface::PrintValues() {
@ -132,30 +173,14 @@ cWebInterface::~cWebInterface() {
// TODO Auto-generated destructor stub
}
void cWebInterface::OnFile(HttpRequest& i_request, HttpResponse& i_response) {
const String name = "Channel1";
file_t file = fileOpen(name, eFO_CreateIfNotExist | eFO_ReadWrite);
int size = m_adc_values_raw_cnt[RAW_CHANNEL];
Serial.printf("Size: %d\n\r", size);
char buf[12];
for (int i = 0; i < 4; i++) {
int len = sprintf(buf, "%d,", m_adc_values_raw[RAW_CHANNEL][i]);
fileWrite(file, buf, len);
fileFlush(file);
}
m_adc_values_raw_cnt[RAW_CHANNEL] = 0;
fileClose(file);
i_response.setCache(86400, true); // It's important to use cache for better performance.
i_response.sendFile(name);
}
void cWebInterface::ResetRawValues() {
for (int channel = 0; channel < 4; channel++) {
for (int sample = 0; sample < 1000; sample++) {
m_adc_values_raw[channel][sample] = 0;
}
}
/*
for (int channel = 0; channel < 4; channel++) {
for (int sample = 0; sample < 1000; sample++) {
m_adc_values_raw[channel][sample] = 0;
}
}
*/
}
void cWebInterface::OnConfiguration(HttpRequest &request, HttpResponse &response) {

9
software/app/web_interface.h

@ -104,9 +104,12 @@ public:
* @param i_response
*/
void OnRawUpdate(HttpRequest& i_request, HttpResponse& i_response);
void OnFile(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
@ -120,10 +123,6 @@ private:
int32_t m_adc_value_average[4];
/// Server instance
HttpServer m_server;
char * m_jsonBuffer;
int32_t m_adc_values_raw[4][RAW_SAMPLES];
int m_adc_values_raw_cnt[4];
};
} /* namespace rijnfel */

8
software/files/index.html

@ -42,6 +42,14 @@
<textarea id="raw_samples" rows="4" cols="50" style="width:100%;">
</textarea>
<button onclick="buttonStartCallback()">Start</button>
<button onclick="buttonStopCallback()">Stop</button>
<button onclick="cleanCallback()">Clean</button>
<button onclick="ch1()">ch1</button>
<button onclick="ch2()">ch2</button>
<button onclick="ch3()">ch3</button>
<button onclick="ch4()">ch4</button>
<div id="channel_id">Channel 1</div>
</div>
</div>
</div>

70
software/files/index.js

@ -13,4 +13,74 @@ $( document ).ready(function() {
setTimeout(worker, 500);
});
})();
var wsUri = "ws://" + location.host + "/ws";
websocket = new WebSocket(wsUri);
websocket.onopen = function(evt) { onOpen(evt) };
websocket.onclose = function(evt) { onClose(evt) };
websocket.onmessage = function(evt) { onMessage(evt) };
websocket.onerror = function(evt) { onError(evt) };
});
function buttonStartCallback(){
websocket.send("start");
var myTextArea = $('#raw_samples');
myTextArea.val("");
}
function cleanCallback() {
var myTextArea = $('#raw_samples');
myTextArea.val("");
}
function ch1() {
document.getElementById("channel_id").innerHTML = "Channel 1"
var myTextArea = $('#raw_samples');
myTextArea.val("Stopped");
websocket.send("ch1");
}
function ch2() {
document.getElementById("channel_id").innerHTML = "Channel 2"
var myTextArea = $('#raw_samples');
myTextArea.val("Stopped");
websocket.send("ch2");
}
function ch3() {
document.getElementById("channel_id").innerHTML = "Channel 3"
var myTextArea = $('#raw_samples');
myTextArea.val("Stopped");
websocket.send("ch3");
}
function ch4() {
document.getElementById("channel_id").innerHTML = "Channel 4"
var myTextArea = $('#raw_samples');
myTextArea.val("Stopped");
websocket.send("ch4");
}
function buttonStopCallback(){
websocket.send("stop");
var myTextArea = $('#raw_samples');
}
function onOpen(evt) {
console.log("CONNECTED");
}
function onClose(evt) {
console.log("close");
}
function onMessage(evt) {
var myTextArea = $('#raw_samples');
myTextArea.val(myTextArea.val() + evt.data);
}
function onError(evt){
console.log("error: " + evt.data);
}

14
software/include/application.h

@ -0,0 +1,14 @@
/*
* application.h
*
* Created on: Aug 23, 2017
* Author: Elmar
*/
#ifndef INCLUDE_APPLICATION_H_
#define INCLUDE_APPLICATION_H_
//Disgusting
void ChangeSampleChannel(int channel);
#endif /* INCLUDE_APPLICATION_H_ */
Loading…
Cancel
Save