Browse Source

Merge branch 'software_develop' of github.com:s3xm3x/UppSense into software_develop

software_develop
Maximilian Stiefel 7 years ago
parent
commit
f0a07ac1f5
  1. 3
      software/Basic_Blink/Makefile-user.mk
  2. 2
      software/Basic_Blink/app/ads101x.cpp
  3. 2
      software/Basic_Blink/app/ads101x.h
  4. 88
      software/Basic_Blink/app/application.cpp
  5. 21
      software/Basic_Blink/app/double_buffer.h
  6. 8
      software/Basic_Blink/app/sensor_hub.cpp
  7. 1
      software/Basic_Blink/app/sensor_hub.h
  8. 14
      software/Basic_Blink/app/sensor_settings.h
  9. 114
      software/Basic_Blink/app/web_interface.cpp
  10. 51
      software/Basic_Blink/app/web_interface.h
  11. BIN
      software/Basic_Blink/files/bootstrap.min.css.gz
  12. 69
      software/Basic_Blink/files/config.html
  13. 38
      software/Basic_Blink/files/config.js
  14. 88
      software/Basic_Blink/files/index.html
  15. 13
      software/Basic_Blink/files/index.js
  16. BIN
      software/Basic_Blink/files/jquery-2.1.4.min.js.gz

3
software/Basic_Blink/Makefile-user.mk

@ -34,6 +34,7 @@ COM_SPEED = 460800
# SPI_MODE = dio
## SPIFFS options
DISABLE_SPIFFS = 1
# DISABLE_SPIFFS = 1
# SPIFF_FILES = files
SPIFF_SIZE = 196608

2
software/Basic_Blink/app/ads101x.cpp

@ -8,6 +8,8 @@
#include "ads101x.h"
#include <SmingCore/Wire.h>
#include <SmingCore/SmingCore.h>
namespace rijnfel {
namespace ads {

2
software/Basic_Blink/app/ads101x.h

@ -34,7 +34,7 @@ struct ads_sample_t {
enum eGainAmplifier gain;
};
class cADS101x: cADC<ads_sample_t, ads_voltage_t> {
class cADS101x: public cADC<ads_sample_t, ads_voltage_t> {
public:
cADS101x(uint8_t i_address = 0x48);

88
software/Basic_Blink/app/application.cpp

@ -2,65 +2,77 @@
#include <SmingCore/SmingCore.h>
#include <SmingCore/HardwareSerial.h>
#include "ads101x.h"
#include "sensor_hub.h"
#include "sensor_settings.h"
#include "double_buffer.h"
#include "web_interface.h"
#define LED_PIN 2 // GPIO2
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);
Timer procTimer;
ads::cADS101x adc;
bool state = true;
uint16_t sampless = 1;
uint8_t channel = 0;
unsigned int average[4];
void readAdc() {
//while (adc.IsReady()) {
cSensorHub hub(5);
int test = 0;
void updateSensorHub() {
hub.Update();
}
void adcCallback(cDoubleBuffer<ads::ads_sample_t> & buffer) {
cWebInterface::GetInstance()->UpdateAdc(adc, buffer);
adc.SetMux(static_cast<ads::eInputMux>(ads::eInputMux::AIN_0 + channel));
channel++;
if (channel > 3) {
channel = 0;
}
uint16_t settings = adc.GetSettings();
Serial.printf("Settings = %d;\n\r", settings);
if (false) {
adc.SetMux(
static_cast<ads::eInputMux>(ads::eInputMux::AIN_0 + channel));
ads::ads_sample_t sample = adc.RawSample();
average[channel] += adc.ConvertSample(sample) / 1000; //want it in mv
channel++;
if (channel > 3) {
channel = 0;
sampless++;
if (sampless > 3300) {
for (uint8_t i = 0; i < 4; i++) {
Serial.printf("C[%d] = %d; ", i, average[i] / sampless);
average[i] = 0;
}
Serial.printf("\n\r");
sampless = 0;
}
}
}
WDT.alive();
//}
//Serial.printf("Not ready anymore\n\r");
}
void blink() {
readAdc();
}
void init() {
spiffs_mount();
Serial.begin(460800);
Wire.pins(5, 4);
Wire.pins(4, 5);
Wire.begin();
//SET higher CPU freq & disable wifi sleep
system_update_cpu_freq(SYS_CPU_160MHZ);
WDT.enable(false);
wifi_set_sleep_type(NONE_SLEEP_T);
//WDT.enable(false);
pinMode(LED_PIN, OUTPUT);
adc.SetMux(ads::eInputMux::AIN_0);
adc.SetSampleSpeed(ads::eSampleSpeed::SPS_3300);
procTimer.initializeMs(500, readAdc).start();
hub.SetAdc(&adc);
cSensorSettings<ads::ads_sample_t> * adcSettings;
adcSettings = new cSensorSettings<ads::ads_sample_t>(&adcCallback, 250, 5);
hub.SetAdcSettings(adcSettings);
WifiEvents.onStationDisconnect(STADisconnect);
WifiEvents.onStationGotIP(STAGotIP);
/* WifiAccessPoint.setIP(IPAddress(10, 0, 0, 1)); //TODO
WifiAccessPoint.config("Sensus", "", AUTH_OPEN, false, 3);*/
cWebInterface::GetInstance()->Start();
procTimer.initializeMs(100, updateSensorHub).start();
}
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);
}
// Add commands to be executed after successfully connecting to AP and got IP from it
}

21
software/Basic_Blink/app/double_buffer.h

@ -9,6 +9,7 @@
#define APP_DOUBLE_BUFFER_H_
#include <stdint.h>
#include <SmingCore/SmingCore.h>
namespace rijnfel {
@ -25,7 +26,11 @@ public:
}
void Resize(int i_newSize) {
m_size = i_newSize;
delete m_buf[0];
delete m_buf[1];
m_buf[0] = new BufferType[m_size];
m_buf[1] = new BufferType[m_size];
}
bool AddValue(BufferType i_val) {
@ -39,14 +44,22 @@ public:
return false;
}
BufferType * GetReadyBuffer() {
return m_buf[(m_bufferIndex == 0) ? 1 : 0];
}
BufferType * GetBuffer(int i_index) {
return m_buf[i_index];
}
int GetSize() {
return m_size;
}
virtual ~cDoubleBuffer() {
delete m_buf[0];
delete m_buf[1];
delete m_buf;
delete[] m_buf[0];
delete[] m_buf[1];
delete[] m_buf;
}
private:
int m_size;

8
software/Basic_Blink/app/sensor_hub.cpp

@ -27,6 +27,13 @@ void cSensorHub::Update() {
}
}
void cSensorHub::SetAdc(ads::cADS101x* i_adc) {
if (m_adc != NULL) {
delete m_adc;
}
m_adc = i_adc;
}
void cSensorHub::SetAdcSettings(
cSensorSettings<ads::ads_sample_t> * i_adcSettings) {
m_adcSettings = i_adcSettings;
@ -45,3 +52,4 @@ cSensorHub::~cSensorHub() {
}
} /* namespace rijnfel */

1
software/Basic_Blink/app/sensor_hub.h

@ -17,6 +17,7 @@ class cSensorHub {
public:
//in milliseconds
cSensorHub(uint32_t i_updatePeriod);
void SetAdc(ads::cADS101x * i_adc);
void SetAdcSettings(cSensorSettings<ads::ads_sample_t> * i_adcSettings);
void SetTempSettings(cSensorSettings<uint32_t> * i_tempSettings);
void Update();

14
software/Basic_Blink/app/sensor_settings.h

@ -11,16 +11,19 @@
#include "double_buffer.h"
namespace rijnfel {
//timebase, in miliseconds.
//Example, period = 10 (ms) timebase = 250(ms)
//Callback will be called after 250/10 = 25 samples
template<typename BufferType>
class cSensorSettings {
public:
cSensorSettings(void (*i_callback)(cDoubleBuffer<BufferType> & buffer),
uint32_t i_periodTimer, uint32_t i_period) :
m_callback(i_callback), m_periodTimer(i_periodTimer), m_period(
i_period) {
m_buffer(1000 / m_period);
uint32_t i_timeBase, uint32_t i_period) :
m_callback(i_callback), m_periodTimer(0), m_period(i_period), m_buffer(
0) {
m_buffer.Resize((int) (i_timeBase / m_period));
}
bool ShouldSample(uint32_t i_updatePeriod) {
m_periodTimer += i_updatePeriod;
if (m_periodTimer >= m_period) {
@ -29,6 +32,7 @@ public:
}
return false;
}
cDoubleBuffer<BufferType> m_buffer;
void (*m_callback)(cDoubleBuffer<BufferType> & buffer);
private:

114
software/Basic_Blink/app/web_interface.cpp

@ -0,0 +1,114 @@
/*
* web_interface.cpp
*
* Created on: Aug 6, 2017
* Author: Elmar
*/
#include "web_interface.h"
#include <SmingCore/SmingCore.h>
//TODO remove this
#include "ads101x.h"
namespace rijnfel {
cWebInterface *cWebInterface::s_instance = 0;
static void onIndex(HttpRequest &request, HttpResponse &response) {
cWebInterface::GetInstance()->OnIndex(request, response);
}
static void onRefresh(HttpRequest &request, HttpResponse &response) {
cWebInterface::GetInstance()->OnRefresh(request, response);
}
cWebInterface::cWebInterface() :
m_serverStarted(false) {
for (int i = 0; i < 4; i++) {
m_adc_value[i] = 0;
}
// TODO Auto-generated constructor stub
}
void onFile(HttpRequest &request, HttpResponse &response) {
String file = request.getPath();
if (file[0] == '/')
file = file.substring(1);
if (file[0] == '.')
response.forbidden();
else {
response.setCache(86400, true); // It's important to use cache for better performance.
response.sendFile(file);
}
}
void cWebInterface::Start() {
if (m_serverStarted)
return;
server.addPath("/", onIndex);
server.addPath("/state", onRefresh);
server.setDefaultHandler(onFile);
server.listen(80);
}
void cWebInterface::Stop() {
if (!m_serverStarted)
return;
m_serverStarted = false;
}
void cWebInterface::OnIndex(HttpRequest &request, HttpResponse &response) {
response.setCache(86400, true); // It's important to use cache for better performance.
response.sendFile("index.html");
}
void cWebInterface::OnRefresh(HttpRequest &request, HttpResponse &response) {
JsonObjectStream* stream = new JsonObjectStream();
JsonObject& json = stream->getRoot();
json["adc_0"] = m_adc_value[0];
json["adc_1"] = m_adc_value[1];
json["adc_2"] = m_adc_value[2];
json["adc_3"] = m_adc_value[3];
response.sendJsonObject(stream);
}
void cWebInterface::UpdateAdc(cADC<ads::ads_sample_t, int32_t> & adc,
cDoubleBuffer<ads::ads_sample_t>& adcBuffer) {
ads::ads_sample_t * buf = &adcBuffer.GetReadyBuffer()[0];
if (buf != NULL) {
ads::ads_sample_t averageSample;
averageSample.gain = buf->gain;
averageSample.mux = buf->mux;
int64_t average = 0;
int size = adcBuffer.GetSize();
for (int i = 0; i < size; i++) {
average += buf[i].rawSample;
}
if (size != 0) {
average /= size;
}
averageSample.rawSample = static_cast<int16_t>(average);
int pos = averageSample.mux - ads::eInputMux::AIN_0;
m_adc_value[pos] = adc.ConvertSample(averageSample);
Serial.printf("Current mux: %d pos: %d \n\r", averageSample.mux, pos);
Serial.printf("%d %d %d %d\n\r", m_adc_value[0], m_adc_value[1],
m_adc_value[2], m_adc_value[3]);
}
}
void cWebInterface::UpdateTemp(cDoubleBuffer<uint32_t>& adcBuffer) {
}
cWebInterface::~cWebInterface() {
// TODO Auto-generated destructor stub
}
} /* namespace rijnfel */

51
software/Basic_Blink/app/web_interface.h

@ -0,0 +1,51 @@
/*
* web_interface.h
*
* Created on: Aug 6, 2017
* Author: Elmar
*/
#ifndef APP_WEB_INTERFACE_H_
#define APP_WEB_INTERFACE_H_
#include "adc.h"
#include "ads101x.h"
#include "double_buffer.h"
#include <SmingCore/SmingCore.h>
namespace rijnfel {
class cWebInterface {
public:
static cWebInterface *s_instance;
static cWebInterface *GetInstance() {
if (cWebInterface::s_instance == NULL)
cWebInterface::s_instance = new cWebInterface();
return cWebInterface::s_instance;
}
public:
cWebInterface();
cWebInterface(cWebInterface const&) {
m_serverStarted = false;
}
cWebInterface& operator=(cWebInterface const&) {
}
void Start();
void Stop();
void UpdateAdc(cADC<ads::ads_sample_t, int32_t> & adc,
cDoubleBuffer<ads::ads_sample_t> & adcBuffer);
void UpdateTemp(cDoubleBuffer<uint32_t> & adcBuffer);
virtual ~cWebInterface();
public:
void OnIndex(HttpRequest &request, HttpResponse &response);
void OnRefresh(HttpRequest &request, HttpResponse &response);
private:
bool m_serverStarted;
int16_t m_adc_value[4];
HttpServer server;
};
} /* namespace rijnfel */
#endif /* APP_WEB_INTERFACE_H_ */

BIN
software/Basic_Blink/files/bootstrap.min.css.gz

Binary file not shown.

69
software/Basic_Blink/files/config.html

@ -0,0 +1,69 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>TyTherm configuration</title>
<!-- Bootstrap core -->
<link href="bootstrap.min.css" rel="stylesheet">
<script src="jquery-2.1.4.min.js"></script>
<style>
.main { font-size: 63px; }
</style>
</head>
<script src="config.js"></script>
<body>
<div class="container">
<div class="header">
<nav>
<ul class="nav nav-pills pull-right">
<li role="presentation"><a href="/">Status</a></li>
<li role="presentation" class="active"><a href="/config">Configuration</a></li>
</ul>
</nav>
<h3 class="text-muted">TyTherm</h3>
</div>
<div class="row">
<h2>&nbsp;</h2>
<div class="col-xs-offset-2 col-xs-8">
<!-- Network config -->
<div>
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">Network</h3>
</div>
<div class="panel-body">
<form id="form_netcfg" method="POST">
<div class="form-group">
<label for="StaSSID">Name</label>
<input type="text" id="StaSSID" name="StaSSID" class="form-control" placeholder="Enter SSID" value="{StaSSID}">
</div>
<div class="form-group">
<label for="Password">Password</label>
<input type="password" id="StaPassword" name="StaPassword" class="form-control" placeholder="Password">
</div>
<div class="form-group">
<label for="StaEnable">Enable WIFI Client</label>
<input type="checkbox" id="StaEnable" name="StaEnable" class="form-control">
</div>
<button type="submit" class="btn btn-success">Save network</button>
<button type="button" id="netcfg_cancel" class="btn btn-link">Cancel</button>
</form>
</div>
</div>
</div>
<!-- TyTherm adjustment config -->
</div> <!-- /col -->
</div> <!-- /row -->
</div> <!-- /container -->
</body>
</html>

38
software/Basic_Blink/files/config.js

@ -0,0 +1,38 @@
function get_config() {
$.getJSON('/config.json',
function(data) {
$.each(data, function(key, value){
document.getElementById(key).value = value;
if (data.StaEnable == 1) {
document.getElementById('StaEnable').checked = true;
}
else
document.getElementById('StaEnable').checked = false;
});
});
}
function post_netcfg(event) {
event.preventDefault();
var formData = {
'StaSSID' : document.getElementById('StaSSID').value,
'StaPassword' : document.getElementById('StaPassword').value,
'StaEnable' : (document.getElementById('StaEnable').checked ? 1 : 0)
};
$.ajax({
type : 'POST',
url : '/config',
contentType : 'application/json; charset=utf-8',
data : JSON.stringify(formData),
dataType : 'json'
})
}
$( document ).ready(function() {
get_config();
document.getElementById('form_netcfg').addEventListener('submit', post_netcfg);
document.getElementById('netcfg_cancel').addEventListener('click', get_config);
});

88
software/Basic_Blink/files/index.html

@ -0,0 +1,88 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>TyTherm Status</title>
<!-- Bootstrap core -->
<link href="bootstrap.min.css" rel="stylesheet">
<script src="jquery-2.1.4.min.js"></script>
<style>
.main { font-size: 63px; }
</style>
</head>
<script src="index.js"></script>
<body>
<div class="container">
<div class="header">
<nav>
<ul class="nav nav-pills pull-right">
<li role="presentation" class="active"><a href="/">Status</a></li>
<li role="presentation"><a href="/config">Configuration</a></li>
</ul>
</nav>
<h3 class="text-muted">TyTherm</h3>
</div>
<div class="row">
<h2>&nbsp;</h2>
<div class="col-xs-offset-2">
<div class="col-xs-10 col-md-5">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">Channel 0</h3>
</div>
<div class="panel-body">
<h1 id="adc_0" class="text-center main">{adc_0}</h1>
</div>
</div>
</div>
</div> <!-- /col -->
<div class="col-xs-offset-2">
<div class="col-xs-10 col-md-5">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">Channel 1</h3>
</div>
<div class="panel-body">
<h1 id="adc_1" class="text-center main">{adc_1}</h1>
</div>
</div>
</div>
</div> <!-- /col -->
</div> <!-- /row -->
<div class="row">
<div class="col-xs-offset-2">
<div class="col-xs-10 col-md-5">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">Channel 2</h3>
</div>
<div class="panel-body">
<h1 id="adc_2" class="text-center main">{adc_2}</h1>
</div>
</div>
</div>
</div> <!-- /col -->
<div class="col-xs-offset-2">
<div class="col-xs-10 col-md-5">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">Channel 3</h3>
</div>
<div class="panel-body">
<h1 id="adc_3" class="text-center main">{adc_3}</h1>
</div>
</div>
</div>
</div> <!-- /col -->
</div> <!-- /row -->
</div> <!-- /container -->
</body>
</html>

13
software/Basic_Blink/files/index.js

@ -0,0 +1,13 @@
$( document ).ready(function() {
(function worker() {
$.getJSON('/state', function(data) {
document.getElementById('adc_0').textContent = data.adc_0;
document.getElementById('adc_1').textContent = data.adc_1;
document.getElementById('adc_2').textContent = data.adc_2;
document.getElementById('adc_3').textContent = data.adc_3;
setTimeout(worker, 5000);
});
})();
});

BIN
software/Basic_Blink/files/jquery-2.1.4.min.js.gz

Binary file not shown.
Loading…
Cancel
Save