gadget-tester/ui_tester.c
2019-11-22 11:12:54 +01:00

345 lines
8.3 KiB
C

#include <stddef.h>
#include "ui.h"
#include <string.h>
#include "adc.h"
#include "button.h"
#include "system.h"
struct display_menu *CurrentMenu = &MainMenu;
static int pin_selected = 0;
static int front_selected = 0;
static void ui_sel_pin(uint8_t press, int hold);
static int pin_t1, pin_t2, front_t1, front_t2;
static uint32_t time_t1_s, time_t1_us;
static void ui_return(uint8_t press, int hold)
{
if (press == '*') {
display_clear(NULL);
clear_input_callback();
ui_display_menu(&MainMenu);
}
}
static void ui_action_interrupt(uint8_t press, int hold)
{
if (press == '*') {
clear_keepalive();
display_clear(NULL);
display_text(5, "Interrupted!");
set_input_callback(ui_return);
}
}
static void display_time(int line, uint32_t s, uint32_t us)
{
char time_txt[] = " .000000 s";
printf("in display_time (%u.%06u)\r\n", s, us);
if (s >= 10000)
time_txt[0] = '0' + s / 10000;
if (s >= 1000)
time_txt[1] = '0' + (s % 10000) / 1000;
if (s >= 100)
time_txt[2] = '0' + (s % 1000) / 100;
if (s >= 10)
time_txt[3] = '0' + (s % 100) / 10;
time_txt[4] = '0' + s % 10;
/* . */
time_txt[6] += us / 100000;
time_txt[7] += (us % 100000) / 10000;
time_txt[8] += (us % 10000) / 1000;
time_txt[9] += (us % 1000) / 100;
time_txt[10] += (us % 100) / 10;
time_txt[11] += (us % 10);
printf("out display_time (%s)\r\n", time_txt);
display_text(line, time_txt);
}
static void trigger_poll(void)
{
exti_poll();
}
static void trigger_t1(uint32_t pin, uint8_t front, uint32_t s, uint32_t us)
{
if ((front_t1 == 2) || (front_t1 == front)) {
exti_clear_callback(pin_t1);
time_t1_s = s;
time_t1_us = us;
display_text(5, "T1");
}
}
static void ui_restart_trigger(uint8_t press, int hold);
static void trigger_t2(uint32_t pin, uint8_t front, uint32_t s, uint32_t us)
{
uint32_t delta_s, delta_us;
if ((front_t2 == 2) || (front_t2 == front)) {
exti_clear_callback(pin);
delta_s = s - time_t1_s;
if (us < time_t1_us) {
delta_s--;
us += 1000000;
}
delta_us = us - time_t1_us;
display_clear(NULL);
display_text(5, "Time Delta: ");
if (delta_s > 99999) {
display_text(6, "Error!");
set_input_callback(ui_return);
return;
}
clear_keepalive();
display_time(6, delta_s, delta_us);
display_text(7, "Press to restart");
set_input_callback(ui_restart_trigger);
}
}
static void ui_restart_trigger(uint8_t press, int hold)
{
if (press == '*') {
set_input_callback(ui_action_interrupt);
display_clear(NULL);
display_text(7, "Capturing...");
exti_set_callback(Channel_Pin[pin_t1], trigger_t1);
exti_set_callback(Channel_Pin[pin_t2], trigger_t2);
pin_exti_init();
pin_exti_start_read();
set_keepalive(trigger_poll);
}
}
static void ui_sel_pin(uint8_t press, int hold)
{
char TXT[] = "PIN X ";
if ((press == '-') && (front_selected == 0) && (pin_selected > 0)) {
pin_selected--;
front_selected = 2;
}else if ((press == '+') && (front_selected == 2) && (pin_selected < 4)) {
pin_selected++;
front_selected = 0;
} else if ((press == '+') && (front_selected < 2))
front_selected++;
else if ((press == '-') && (front_selected > 0))
front_selected--;
switch (front_selected) {
case 0:
memcpy(TXT + 6, "FALL", 4);
break;
case 1:
memcpy(TXT + 6, "RISE", 4);
break;
case 2:
memcpy(TXT + 6, "BOTH", 4);
break;
}
TXT[4] = '0' + pin_selected;
display_text(6, TXT);
display_text(7, Channel_name[pin_selected]);
if (press == '*') {
if (pin_t1 < 0) {
pin_t1 = pin_selected;
front_t1 = front_selected;
pin_selected = 0;
front_selected = 0;
display_text(5, "Select T2 pin:");
} else {
pin_t2 = pin_selected;
front_t2 = front_selected;
set_input_callback(ui_action_interrupt);
display_clear(NULL);
display_text(7, "Capturing...");
exti_set_callback(Channel_Pin[pin_t1], trigger_t1);
exti_set_callback(Channel_Pin[pin_t2], trigger_t2);
pin_exti_init();
pin_exti_start_read();
set_keepalive(trigger_poll);
}
}
}
static void ui_measure_interval(const void *arg)
{
int i;
pin_t1 = -1;
pin_t2 = -1;
set_input_callback(ui_sel_pin);
for (i = 0; i < 5; i++)
GPIOA_MODE &= ~(0x03 << (Channel_Pin[i] * 2));
display_text(5, "Select T1 pin:");
ui_sel_pin(0,0);
}
static void display_voltage(int line, uint16_t val)
{
uint32_t mV = (val * 3300) / 4096;
char txt[] = "0.000";
if (val > 1000)
txt[0] = '0' + mV / 1000;
mV %= 1000;
if (mV != 0)
txt[4] = '0' + mV % 10;
if (mV > 9)
txt[3] = '0' + ((mV / 10) % 10);
if (mV > 99)
txt[2] = '0' + (mV / 100);
display_text(6, txt);
}
void ui_voltage_poll(void)
{
uint16_t val = 0;
char TXT[]="PIN X ADC";
if (pin_selected < 2)
pin_selected = 2;
TXT[4] = '0' + pin_selected;
GPIOA_MODE |= (0x03 << (Channel[pin_selected] * 2));
adc_pin_val(Channel[pin_selected], &val);
display_text(5, TXT);
display_voltage(6, val);
display_text(7, Channel_name[pin_selected]);
}
static void ui_voltage_input(uint8_t press, int hold)
{
if (press == '*') {
display_clear(NULL);
display_text(5, "Interrupted!");
clear_keepalive();
set_input_callback(ui_return);
return;
}
if (press == '+') {
pin_selected++;
if (pin_selected > 4)
pin_selected = 2;
}
if (press == '-') {
pin_selected--;
if (pin_selected < 2)
pin_selected = 4;
}
ui_voltage_poll();
}
static void ui_measure_voltage(const void *arg)
{
set_input_callback(ui_voltage_input);
set_keepalive(ui_voltage_poll);
display_clear(NULL);
ui_voltage_poll();
}
static void ui_input_poll(void)
{
uint32_t val = 0;
char Line5[] = "P0: 0 P1: 0";
char Line6[] = "P2: 0 P3: 0";
char Line7[] = "P4: 0 ";
val = gpio_get(GPIOA, GPIO8|GPIO9|GPIO7|GPIO6|GPIO5);
if (val & GPIO8)
Line5[4] = '1';
if (val & GPIO9)
Line5[12] = '1';
if (val & GPIO7)
Line6[4] = '1';
if (val & GPIO6)
Line6[12] = '1';
if (val & GPIO5)
Line7[4] = '1';
display_text(5, Line5);
display_text(6, Line6);
display_text(7, Line7);
}
static void ui_measure_input(const void *arg)
{
int i;
for (i = 0; i < 5; i++)
GPIOA_MODE &= ~(0x03 << (Channel_Pin[i] * 2));
set_input_callback(ui_action_interrupt);
set_keepalive(ui_input_poll);
ui_input_poll();
}
static void ui_measure_frequency(const void *arg)
{
}
static void ui_generate_dc(const void *arg)
{
}
static void ui_generate_pwm(const void *arg)
{
}
static void ui_submenu(const void *arg)
{
ui_display_menu(arg);
}
const struct display_menu SignalGenMenu = {
.entry_n = 3,
.entry = {
{ "Generate DC ", ui_generate_dc, NULL },
{ "Generate PWM ", ui_generate_pwm, NULL },
{ "Back to Main ", ui_submenu, &MainMenu },
{ "", NULL, NULL}
}
};
const struct display_menu ExtraMenu = {
.entry_n = 1,
.entry = {
{ "TODO! ", ui_submenu, &MainMenu},
{ "", NULL, NULL}
}
};
const struct display_menu MeasureMenu = {
.entry_n = 5,
.entry = {
{ "Digital input", ui_measure_input, NULL },
{ "Time Delta", ui_measure_interval, NULL },
{ "Voltage ", ui_measure_voltage, NULL },
{ "Frequency", ui_measure_frequency, NULL },
{ "Back to Main ", ui_submenu, &MainMenu },
{ "", NULL, NULL}
}
};
const struct display_menu MainMenu = {
.entry_n = 3,
.entry = {
{ "Measure ", ui_submenu, &MeasureMenu },
{ "Signal Gener. ", ui_submenu, &SignalGenMenu },
{ "Extra ", ui_submenu, &ExtraMenu },
{ "", NULL, NULL}
}
};