-
Notifications
You must be signed in to change notification settings - Fork 25
/
console.c
161 lines (153 loc) · 5.35 KB
/
console.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
#include "console.h"
#include "packet.h"
#include "ch.h"
#include "hal.h"
#include "comm_usb.h"
#include "datatypes.h"
#include <string.h>
#include <stdio.h>
#include <chprintf.h>
#include "memstreams.h"
#include "config.h"
#include "ltc6803.h"
#include "rtcc.h"
#include "current_monitor.h"
#include "analog.h"
#include "power.h"
#include "comm_can.h"
void console_process_command(char *command)
{
enum { kMaxArgs = 64 };
int argc = 0;
char *argv[kMaxArgs];
char *p2 = strtok(command, " ");
while (p2 && argc < kMaxArgs) {
argv[argc++] = p2;
p2 = strtok(0, " ");
}
if (argc == 0) {
console_printf("No command received\n");
console_printf("\r\n");
return;
}
if (strcmp(argv[0], "ping") == 0) {
console_printf("pong\n");
console_printf("\r\n");
}
else if (strcmp(argv[0], "mem") == 0) {
size_t n, size;
n = chHeapStatus(NULL, &size);
console_printf("core free memory : %u bytes\n", chCoreGetStatusX());
console_printf("heap fragments : %u\n", n);
console_printf("heap free total : %u bytes\n", size);
console_printf("\r\n");
}
else if (strcmp(argv[0], "threads") == 0) {
thread_t *tp;
static const char *states[] = {CH_STATE_NAMES};
console_printf(" addr stack prio refs state name time \n");
console_printf("-------------------------------------------------------------\n");
tp = chRegFirstThread();
do {
console_printf("%.8lx %.8lx %4lu %4lu %9s %14s %lu\n",
(uint32_t)tp, (uint32_t)tp->p_ctx.r13,
(uint32_t)tp->p_prio, (uint32_t)(tp->p_refs - 1),
states[tp->p_state], tp->p_name, (uint32_t)tp->p_time);
tp = chRegNextThread(tp);
} while (tp != NULL);
console_printf("\r\n");
}
else if (strcmp(argv[0], "uptime") == 0) {
console_printf("System uptime: %d seconds\n", ST2S(chVTGetSystemTime()));
console_printf("\r\n");
}
else if (strcmp(argv[0], "cell_voltages") == 0) {
Config *config = config_get_configuration();
float *cells = ltc6803_get_cell_voltages();
for (uint8_t i = 0; i < config->numCells; i++)
{
console_printf("Cell %d: %.4fV\n", i + 1, cells[i]);
}
console_printf("\r\n");
}
else if (strcmp(argv[0], "current") == 0) {
console_printf("Battery current: %.2fA\n", current_monitor_get_current());
console_printf("\r\n");
}
else if (strcmp(argv[0], "voltage") == 0) {
console_printf("Bus voltage: %.2fV\n", current_monitor_get_bus_voltage());
console_printf("\r\n");
}
else if (strcmp(argv[0], "temp") == 0) {
float temp = analog_temperature();
console_printf("Board temperature: %.2f degrees C\n", (double)temp);
console_printf("\r\n");
}
else if (strcmp(argv[0], "charger_voltage") == 0) {
console_printf("Charger input voltage: %.2fV\n", analog_charger_input_voltage());
console_printf("\r\n");
}
else if (strcmp(argv[0], "power_on_event") == 0) {
console_printf("Power on event: %d\n", power_get_power_on_event());
console_printf("\r\n");
}
else if (strcmp(argv[0], "rtcc") == 0) {
Time time = rtcc_get_time();
console_printf("RTCC time: %d/%d/%04d %02d:%02d:%02d\n", time.month, time.day, time.year, time.hour, time.minute, time.second);
console_printf("\r\n");
}
else if (strcmp(argv[0], "enable_drain") == 0) {
Config *config = config_get_configuration();
console_printf("Enabling all balance resistors...\n");
for (uint8_t i = 0; i < config->numCells; i++)
{
ltc6803_enable_balance(i + 1);
}
ltc6803_lock();
console_printf("\r\n");
}
else if (strcmp(argv[0], "disable_drain") == 0) {
console_printf("Disabling all balance resistors...\n");
ltc6803_unlock();
ltc6803_disable_balance_all();
console_printf("\r\n");
}
else if (strcmp(argv[0], "infinity_current") == 0) {
console_printf("Current: %f\n", comm_can_get_infinity_current());
console_printf("\r\n");
}
else
{
console_printf("%s: command not found\n", argv[0]);
// console_printf("type help for a list of available commands\n");
console_printf("\r\n");
}
}
void console_printf(char* format, ...) {
va_list arg;
va_start (arg, format);
unsigned int len;
static char print_buffer[255];
print_buffer[0] = PACKET_CONSOLE;
len = vsnprintf(print_buffer+1, 254, format, arg);
/*MemoryStream ms;*/
/*BaseSequentialStream *chp;*/
/*size_t size = 254;*/
/*size_t size_wo_nul;*/
/*if (size > 0)*/
/*size_wo_nul = size - 1;*/
/*else*/
/*size_wo_nul = 0;*/
/* Memory stream object to be used as a string writer, reserving one
byte for the final zero.*/
/*msObjectInit(&ms, (uint8_t *)(print_buffer + 1), size_wo_nul, 0);*/
/*[> Performing the print operation using the common code.<]*/
/*chp = (BaseSequentialStream *)(void *)&ms;*/
/*len = chvprintf(chp, format, arg);*/
va_end (arg);
/*if (ms.eos < size)*/
/*print_buffer[ms.eos + 1] = 0;*/
if(len > 0) {
packet_send_packet((unsigned char*)print_buffer, (len<254) ? len+1: 255);
}
}