|
| 1 | +// sample struct that keeps 3 samples in memory |
| 2 | +struct sample_t { |
| 3 | + int values[3]; |
| 4 | + int min; |
| 5 | + int max; |
| 6 | +} sample_t_default = {{0, 0, 0}, 0, 0}; |
| 7 | + |
1 | 8 | void safety_rx_hook(CAN_FIFOMailBox_TypeDef *to_push);
|
2 | 9 | int safety_tx_hook(CAN_FIFOMailBox_TypeDef *to_send);
|
3 | 10 | int safety_tx_lin_hook(int lin_num, uint8_t *data, int len);
|
4 | 11 | int safety_ignition_hook();
|
5 | 12 | uint32_t get_ts_elapsed(uint32_t ts, uint32_t ts_last);
|
6 | 13 | int to_signed(int d, int bits);
|
| 14 | +void update_sample(struct sample_t *sample, int sample_new); |
7 | 15 |
|
8 | 16 | typedef void (*safety_hook_init)(int16_t param);
|
9 | 17 | typedef void (*rx_hook)(CAN_FIFOMailBox_TypeDef *to_push);
|
@@ -118,3 +126,18 @@ int to_signed(int d, int bits) {
|
118 | 126 | }
|
119 | 127 | return d;
|
120 | 128 | }
|
| 129 | + |
| 130 | +// given a new sample, update the smaple_t struct |
| 131 | +void update_sample(struct sample_t *sample, int sample_new) { |
| 132 | + for (int i = sizeof(sample->values)/sizeof(sample->values[0]) - 1; i > 0; i--) { |
| 133 | + sample->values[i] = sample->values[i-1]; |
| 134 | + } |
| 135 | + sample->values[0] = sample_new; |
| 136 | + |
| 137 | + // get the minimum and maximum measured samples |
| 138 | + sample->min = sample->max = sample->values[0]; |
| 139 | + for (int i = 1; i < sizeof(sample->values)/sizeof(sample->values[0]); i++) { |
| 140 | + if (sample->values[i] < sample->min) sample->min = sample->values[i]; |
| 141 | + if (sample->values[i] > sample->max) sample->max = sample->values[i]; |
| 142 | + } |
| 143 | +} |
0 commit comments