-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcmd.c
178 lines (145 loc) · 5.06 KB
/
cmd.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#include "config.h"
#include "printf.h"
#include "adc.h"
#include "flash.h"
#include "joystick.h"
#include "usb_hid.h"
void cmd_reset(BaseSequentialStream *chp, int argc, char *argv[]) {
(void)argv;
if (argc > 0) {
chprintf(chp, "Usage: reset\r\n");
return;
}
chprintf(chp, "Will reset in 200ms\r\n");
chThdSleepMilliseconds(200);
NVIC_SystemReset();
}
// void cmd_restart(BaseSequentialStream *chp, int argc, char *argv[])
// {
// (void)argv;
// if (argc > 0) {
// chprintf(chp, "Usage: cal\r\n");
// return;
// }
// chThdCreateFromHeap(NULL, 512, "lsm303c_thread", NORMALPRIO + 2, lsm303c_thread, &I2CD1);
// }
void cmd_calibrate(BaseSequentialStream *chp, int argc, char *argv[]) {
(void)argv;
if (argc > 0) {
chprintf(chp, "Usage: cal\r\n");
return;
}
js_cal_switch = 1;
chprintf(chp, "Move joystick to both end points then center it and press any key\r\n");
while (chnGetTimeout((BaseChannel *)chp, TIME_IMMEDIATE) == Q_TIMEOUT) {
chThdSleepMilliseconds(200);
}
js_cal_switch = 0;
chprintf(chp, "\r\nCalibration complete\r\n");
chThdSleepMilliseconds(200); // Wait for calibration data to be written by JS thread
chprintf(chp, "M-: %f, M+: %f, offset: %f\r\n", cal_data->m_neg, cal_data->m_pos, cal_data->offset);
}
void cmd_calprint(BaseSequentialStream *chp, int argc, char *argv[]) {
(void)argv;
if (argc > 0) {
chprintf(chp, "Usage: calwrite\r\n");
return;
}
chprintf(chp, "M-: %f, M+: %f, offset: %f\r\n", cal_data->m_neg, cal_data->m_pos, cal_data->offset);
}
void cmd_cal_save(BaseSequentialStream *chp, int argc, char *argv[]) {
(void)argv;
if (argc > 0) {
chprintf(chp, "Usage: cal_save\r\n");
return;
}
if(flash_write(cal_frame.stream, sizeof(cal_frame))) chprintf(chp, "Flash written\r\n");
}
void cmd_cal_load(BaseSequentialStream *chp, int argc, char *argv[]) {
(void)argv;
if (argc > 0) {
chprintf(chp, "Usage: cal_load\r\n");
return;
}
js_cal_load();
}
void cmd_calread(BaseSequentialStream *chp, int argc, char *argv[]) {
(void)argv;
char buffer[128];
uint8_t i;
if (argc > 0) {
chprintf(chp, "Usage: calread\r\n");
return;
}
if (!flash_read(buffer, sizeof(buffer))) return;
for(i=0; i<sizeof(buffer); i++) {
chprintf(chp, "%c", buffer[i]);
}
chprintf(chp, "\r\n");
}
void cmd_flashwrite(BaseSequentialStream *chp, int argc, char *argv[]) {
(void)argv;
char pattern[] = "Hello Flash!";
if (argc > 0) {
chprintf(chp, "Usage: flashwrite\r\n");
return;
}
if(flash_write(pattern, sizeof(pattern))) chprintf(chp, "Flash written\r\n");
}
void cmd_flashread(BaseSequentialStream *chp, int argc, char *argv[]) {
(void)argv;
char buffer[128];
uint8_t i;
if (argc > 0) {
chprintf(chp, "Usage: flashread\r\n");
return;
}
if (!flash_read(buffer, sizeof(buffer))) return;
for(i=0; i<sizeof(buffer); i++) {
chprintf(chp, "%c", buffer[i]);
}
chprintf(chp, "\r\n");
}
void cmd_flashinfo(BaseSequentialStream *chp, int argc, char *argv[]) {
(void)argv;
const flash_descriptor_t *fd = flashGetDescriptor(&FD1);
if (argc > 0) {
chprintf(chp, "Usage: flashinfo\r\n");
return;
}
chprintf(chp, "Flash address: %x, sector count: %d, sector size: %x\r\n", fd->address, fd->sectors_count, fd->sectors_size);
chprintf(chp, "Flash offset: %x, sector size: %x\r\n", flashGetSectorOffset((BaseFlash *) &FD1, 0), flashGetSectorSize((BaseFlash *) &FD1, 0));
flash_offset_t last_sector = flashGetSectorOffset((BaseFlash *) &FD1, fd->sectors_count - 1);
flash_offset_t flash_end = flashGetSectorOffset((BaseFlash *) &FD1, fd->sectors_count);
chprintf(chp, "Cal data sector: %x, cal data address: %x\r\n", last_sector / fd->sectors_size, last_sector);
chprintf(chp, "Sector limit: %x, address limit: %x\r\n", flash_end / fd->sectors_size, flash_end);
}
void cmd_status(BaseSequentialStream *chp, int argc, char *argv[]) {
(void)argv;
if (argc > 0) {
chprintf(chp, "Usage: status\r\n");
return;
}
while (chnGetTimeout((BaseChannel *)chp, TIME_IMMEDIATE) == Q_TIMEOUT) {
chprintf(chp, "Stream: ");
for (uint8_t i=0; i<sizeof(hid_in_data.stream); i++) {
chprintf(chp, "%x ", hid_in_data.stream[i]);
}
chprintf(chp, "\r\n");
#if CONFIG_JS_NUM_AXES > 0
chprintf(chp, "Axes:\r\n");
for(int i=0; i<CONFIG_JS_NUM_AXES; i++) {
chprintf(chp, "%d: %d\r\n", i, hid_in_data.fields.axes[i]);
}
#endif
#if CONFIG_JS_NUM_BUTTONS > 0
chprintf(chp, "Buttons: %b\r\n", hid_in_data.fields.buttons);
#endif
#if CONFIG_ADC_NUM_INPUTS > 0
chprintf(chp, "ADC: %d %d %d %d\r\nADC raw: %d %d %d %d\r\n",
adc_read(ADC_VAL_0), adc_read(ADC_VAL_1), adc_read(ADC_VAL_2), adc_read(ADC_VAL_3),
adc_val[0], adc_val[1], adc_val[2], adc_val[3]);
#endif
chThdSleepMilliseconds(500);
}
}