Add live measurement plot

This commit is contained in:
jaseg 2024-11-19 17:46:23 +01:00
parent 0bd266da0c
commit 1589ece3a0
8 changed files with 157 additions and 2 deletions

View file

@ -18,3 +18,10 @@ end
source upstream/PyCortexMDebug/cmdebug/svd_gdb.py
svd_load upstream/stm32square/svd/STM32G474.svd
break debug_plot_hook
commands
dump binary memory /tmp/adc1_readings.bin adc1_readings
dump binary memory /tmp/adc2_readings.bin adc2_readings
cont
end

42
fw/adc_serve.py Normal file
View file

@ -0,0 +1,42 @@
#!/usr/bin/env python3
import logging
from pathlib import Path
import base64
import threading
import numpy as np
from flask import Flask, render_template, url_for
from flask_socketio import SocketIO
import inotify.adapters, inotify.constants
app = Flask(__name__)
sock = SocketIO(app)
app.logger.setLevel(logging.INFO)
@app.route('/')
def index():
return render_template('index.html')
def watch_adc_graphs(sock):
def handler():
Path('/tmp/adc1_readings.bin').touch()
Path('/tmp/adc2_readings.bin').touch()
in_handle = inotify.adapters.Inotify()
in_handle.add_watch('/tmp/adc2_readings.bin', mask=inotify.constants.IN_CLOSE_WRITE)
for event in in_handle.event_gen(yield_nones=False):
app.logger.info('graph update')
adc1 = np.frombuffer(Path('/tmp/adc1_readings.bin').read_bytes(), dtype=np.uint16)
adc2 = np.frombuffer(Path('/tmp/adc2_readings.bin').read_bytes(), dtype=np.uint16)
sock.emit('graph_update', {'adc1': adc1.tolist(),
'adc2': adc2.tolist()})
adc_thr = threading.Thread(target=handler, daemon=True)
adc_thr.start()
if __name__ == '__main__':
watch_adc_graphs(sock)
sock.run(app)

View file

@ -10,6 +10,9 @@ void update_leds(void);
uint16_t adc1_readings[512];
uint16_t adc2_readings[512];
void debug_plot_hook(void){
}
int main(void) {
/* Enable HSE w/ 8 MHz crystal */
RCC->CR |= RCC_CR_HSEON;
@ -295,7 +298,6 @@ int main(void) {
| DMA_CCR_MINC
| DMA_CCR_CIRC
| DMA_CCR_TEIE
| DMA_CCR_HTIE
| DMA_CCR_TCIE
| DMA_CCR_EN;
@ -307,10 +309,12 @@ int main(void) {
| DMA_CCR_MINC
| DMA_CCR_CIRC
| DMA_CCR_TEIE
| DMA_CCR_HTIE
| DMA_CCR_TCIE
| DMA_CCR_EN;
NVIC_SetPriority(DMA1_Channel1_IRQn, 2);
NVIC_EnableIRQ(DMA1_Channel1_IRQn);
int n = 0;
int d = 1;
int dir = 1;
@ -342,6 +346,18 @@ int main(void) {
}
}
void DMA1_Channel1_IRQHandler(void) {
static int n = 0;
DMA1->IFCR = DMA_IFCR_CTCIF1;
n++;
if (n == 3) {
n = 0;
debug_plot_hook();
}
}
void update_leds() {
int blink_flag = (sys_time_us & (512*1024-1)) < (384*1024);

8
fw/static/plotly-2.35.2.min.js vendored Normal file

File diff suppressed because one or more lines are too long

11
fw/static/pure-min.css vendored Normal file

File diff suppressed because one or more lines are too long

7
fw/static/socket.io.min.js vendored Normal file

File diff suppressed because one or more lines are too long

47
fw/templates/index.html Normal file
View file

@ -0,0 +1,47 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>Board monitor</title>
<link rel="stylesheet" type="text/css" href="{{url_for('static', filename='pure-min.css')}}">
<!--link rel="icon" type="image/png" href="{{url_for('static', filename='favicon-512.png')}}"-->
<!--link rel="apple-touch-icon" href="{{url_for('static', filename='favicon-512.png')}}"-->
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="{{url_for('static', filename='plotly-2.35.2.min.js')}}" charset="utf-8"></script>
<script src="{{url_for('static', filename='socket.io.min.js')}}" charset="utf-8"></script>
<style>
#plot_adc1, #plot_adc2 {
width: 600px;
height: 250px;
}
</style>
</head>
<body>
<h4>ADC 1</h4>
<div id="plot_adc1"></div>
<h4>ADC 2</h4>
<div id="plot_adc2"></div>
<script>
div_adc1 = document.querySelector('#plot_adc1');
div_adc2 = document.querySelector('#plot_adc2');
const layout_settings = {margin: {t: 20, l: 20, r: 20, b: 20}};
plot_adc1 = Plotly.newPlot(div_adc1, [{'x': [], 'y': [], 'type': 'scatter'}], layout_settings);
plot_adc2 = Plotly.newPlot(div_adc2, [{'x': [], 'y': [], 'type': 'scatter'}], layout_settings);
const socket = io();
socket.on('graph_update', (arg, callback) => {
console.log('graph update');
xs1 = Array(arg['adc1'].length)
for (var i=0; i<xs1.length; i++) {
xs1[i] = i;
}
xs2 = Array(arg['adc2'].length)
for (var i=0; i<xs2.length; i++) {
xs2[i] = i;
}
plot_adc1 = Plotly.react(div_adc1, [{'x': xs1, 'y': arg['adc1'], 'type': 'scatter'}], layout_settings);
plot_adc2 = Plotly.react(div_adc2, [{'x': xs2, 'y': arg['adc2'], 'type': 'scatter'}], layout_settings);
});
</script>
</body>
</html>

17
fw/test.py Normal file
View file

@ -0,0 +1,17 @@
#!/usr/bin/env python3
import time
import numpy as np
from pathlib import Path
if __name__ == '__main__':
i = 0
while True:
i += 1
arr1 = np.linspace(0, 2*np.pi, 512)
arr2 = np.linspace(0, 2*np.pi, 512)
arr1 = np.sin(arr1 + 2*np.pi*i/20) * 32767 + 32767
arr2 = np.cos(arr2 + 2*np.pi*i/20) * 32767 + 32767
Path('/tmp/adc1_readings.bin').write_bytes(arr1.astype(np.uint16).tobytes())
Path('/tmp/adc2_readings.bin').write_bytes(arr2.astype(np.uint16).tobytes())
time.sleep(0.1)