-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmenu.c
131 lines (109 loc) · 3.25 KB
/
menu.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
//
// Created by pstadler on 06.11.19.
//
#include <stdio.h>
#include <stdint.h>
#include <pigpio.h>
#include <printf.h>
#include "menu.h"
#include "configuration.h"
#include "main.h"
#include "screens.h"
#include <unistd.h>
#include <stdlib.h>
extern struct FbDevState framebuf_state;
extern bool mainIsRunning;
extern bool usbDriveInserted;
extern bool usbDriveCopied;
static int last_state_a = 0;
static unsigned int last_poll_time = ROT_DEBOUNCE_TIME;
static int homeSwitchPressedTime = 0;
static void usb_copy_files(void);
void
menu_switch_pressed(void) {
/* */
homeSwitchPressedTime = 0;
framebuf_state.mode = FBMODE_HOME;
framebuf_state.state = FBSTATE_IDLE;
framebuf_state.homesw_mode = !framebuf_state.homesw_mode;
}
void
calib_switch_pressed(void) {
/* */
framebuf_state.mode = FBMODE_CALIB;
framebuf_state.state = FBSTATE_INITIALIZE;
}
void
start_switch_pressed(void) {
/* */
if(!framebuf_state.isCalibrated) {
return;
}
framebuf_state.mode = FBMODE_TEST;
framebuf_state.state = FBSTATE_INITIALIZE;
}
void
shutdown_switch_pressed(void) {
/* Perform device shutdown */
system("shutdown now");
}
void*
menu_poll(void* vargp) {
/* Threaded function to be polled */
while(mainIsRunning) {
if(gpioRead(GPIO_INT_ROT_SW)) {
if(homeSwitchPressedTime++ >= SHUTDOWN_PRESS_TIME) {
shutdown_switch_pressed();
}
}
if(last_poll_time > 0) {
last_poll_time--;
}
int a_state = gpioRead(GPIO_INT_ROT_A);
int b_state = gpioRead(GPIO_INT_ROT_B);
if(last_poll_time == 0 && a_state != last_state_a) {
if(a_state != b_state) {
menu_rot_changed(ROTSTATE_COUNTERCLOCKWISE);
} else {
menu_rot_changed(ROTSTATE_CLOCKWISE);
}
last_state_a = a_state;
last_poll_time = ROT_DEBOUNCE_TIME;
}
}
pthread_exit(NULL);
}
void*
usbdrive_poll(void* vargp) {
/* Check whether usb drive is inserted and therefor mounted every 5 seconds */
while(mainIsRunning) {
if(framebuf_state.mode == FBMODE_HOME) {
FILE *f_mount = popen("mount /dev/sda1 /media/usb/ 2>&1", "r");
pclose(f_mount);
FILE *f = popen("mount | grep /dev/sda1 2>&1", "r");
if (f != NULL) {
if (fgetc(f) != EOF) {
/* As soon as the drive is inserted and mounted, copy
all result files from the result dir onto the drive
(in home screen only!) */
usbDriveInserted = true;
usb_copy_files();
/* umount drive again */
system("umount /media/usb/");
sleep(10); /* Give some time to remove the drive again */
usbDriveInserted = false;
}
pclose(f);
}
sleep(5);
}
}
pthread_exit(NULL);
}
static void
usb_copy_files(void) {
/* Copies all .csv files from the results dir onto the mounted usb drive */
usbDriveCopied = false;
system("cp /home/pi/Software/latencytest_fbutils/results/*.csv /media/usb/results/ 2>&1");
usbDriveCopied = true;
}