forked from DangerousPrototypes/BusPirate5-firmware
-
Notifications
You must be signed in to change notification settings - Fork 0
/
system_monitor.c
167 lines (142 loc) · 4.4 KB
/
system_monitor.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
#include <stdio.h>
#include "pico/stdlib.h"
#include "pirate.h"
#include "system_config.h"
static char voltages_value[HW_PINS-1][4];
static uint32_t voltages_update_mask[3];
static uint32_t current_sense;
char current_value[6];
static uint8_t current_update_mask; //bbb.b which digits changed?
//check if each digit of the current value (in ASCII) changed. digits numbered as 432.0
bool monitor_get_current_char(uint8_t digit, char *c)
{
*c=current_value[digit]; //copy no matter what so we can use it...
if(current_update_mask & (1u<<digit))
{
return true;
}
return false;
}
//if changed, return pointer to value string
bool monitor_get_current_ptr(char **c)
{
*c=current_value;
if(current_update_mask)
{
return true;
}
return false;
}
//check if each digit of the voltage value (in ASCII) changed. digits numbered as 2.0
bool monitor_get_voltage_char(uint8_t pin, uint8_t digit, char *c)
{
*c=voltages_value[pin][digit];
if(voltages_update_mask[digit] & (1u<<pin))
{
return true;
}
return false;
}
bool monitor_get_voltage_ptr(uint8_t pin, char **c)
{
*c=voltages_value[pin];
if(voltages_update_mask[0]||voltages_update_mask[2])
{
return true;
}
return false;
}
void monitor_clear_voltage(void)
{
for(uint8_t i=0; i<count_of(voltages_value); i++)
{
voltages_value[i][0]='0';
voltages_value[i][1]='.';
voltages_value[i][2]='0';
voltages_value[i][3]=0x00;
}
}
void monitor_clear_current(void)
{
current_value[0]='0';
current_value[1]='0';
current_value[2]='0';
current_value[3]='.';
current_value[4]='0';
current_value[5]=0x00;
}
void monitor_init(void)
{
voltages_update_mask[0]=0;
voltages_update_mask[1]=0;
voltages_update_mask[2]=0;
monitor_clear_voltage();
monitor_clear_current();
}
void monitor_reset(void)
{
voltages_update_mask[0]=0;
voltages_update_mask[1]=0;
voltages_update_mask[2]=0;
current_update_mask=0;
system_config.pin_changed=0;
system_config.info_bar_changed=false;
}
void monitor_force_update(void)
{
voltages_update_mask[0]=0xffffffff;
voltages_update_mask[1]=0xffffffff;
voltages_update_mask[2]=0xffffffff;
current_update_mask=0xff;
system_config.pin_changed=0xffffffff;
system_config.info_bar_changed=true;
}
bool monitor_voltage_changed(void)
{
return (bool)(voltages_update_mask[0]|voltages_update_mask[2]);
}
bool monitor_current_changed(void)
{
return (bool)current_update_mask;
}
bool monitor(bool current_sense)
{
char c;
//TODO hw_adc helper functions - do conversion on request, and cache it????
hw_adc_sweep();
for(uint8_t i=0; i<count_of(voltages_value); i++)
{
c=((*hw_pin_voltage_ordered[i])/1000)+0x30; //TODO: really do the +0x30 here????
if(c!=voltages_value[i][0])
{
voltages_value[i][0]=c;
voltages_update_mask[0]|=(1U<<i);
}
c=(((*hw_pin_voltage_ordered[i])%1000)/100)+0x30;
if(c!=voltages_value[i][2])
{
voltages_value[i][2]=c;
voltages_update_mask[2]|=(1u<<i);
}
}
if(current_sense)
{
uint32_t current_sense_temp=((hw_adc_raw[HW_ADC_CURRENT_SENSE]>>1) * ((500 * 1000)/2048));
if(current_sense_temp!=current_sense) //value changed, convert to ascii, see which digits changed
{
current_sense=current_sense_temp; //TODO: maybe there is more variation here than we want and this hits needlessly
char current_value_temp[6];
sprintf(current_value_temp, "%03u.%01u", (current_sense_temp/1000), ((current_sense_temp%1000)/100));
for(uint8_t i=0; i<5; i++)
{
if(current_value_temp[i]!=current_value[i]) //if the value of this digit changed, set the mask bit and copy the new value
{
current_update_mask|=(1u<<i);
current_value[i]=current_value_temp[i];
}
}
}
}
//TODO: return individually?! global struct? system config?
return (voltages_update_mask[0]|voltages_update_mask[2]|current_update_mask); //was there an update?
}