-
Notifications
You must be signed in to change notification settings - Fork 0
/
pedal.cpp
80 lines (61 loc) · 1.44 KB
/
pedal.cpp
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
/**
*
*
*/
#include "Arduino.h"
#include "EEPROM.h"
#include "pedal.h"
#define MAX_VAL 1023
Pedal::Pedal (uint16_t pin, uint16_t index) {
_pin = pin;
_addr = index * sizeof(pedal_calibration_struct);
load();
}
uint16_t Pedal::value(void) {
uint16_t val = analogRead(_pin);
val = constrain(val, _cal.vmin, _cal.vmax);
val -= _cal.vmin;
// manipulate the value based on min/max
uint16_t range = _cal.vmax - _cal.vmin;
// sanity check
if(range == 0) range = 1;
uint16_t mul = (MAX_VAL << 6) / range;
return (val * mul) >> 6;
}
uint16_t Pedal::raw(void) {
uint16_t val = analogRead(_pin);
return val;
}
void Pedal::reset(void) {
_cal.vmin = MAX_VAL;
_cal.vmax = 0;
calibrate();
}
void Pedal::calibrate(void) {
// update the min/max
uint16_t val = analogRead(_pin);
if(val < _cal.vmin) _cal.vmin = val;
if(val > _cal.vmax) _cal.vmax = val;
}
void Pedal::load(void) {
// load up the current min/max
EEPROM.get(_addr, _cal);
// do some sanity checking
if(_cal.vmax > MAX_VAL || _cal.vmin > _cal.vmax) {
reset();
}
}
void Pedal::save(void) {
pedal_calibration_struct t;
EEPROM.get(_addr, t);
// check if we really need to update
if(t.vmax != _cal.vmax || t.vmin != _cal.vmin) {
EEPROM.put(_addr, _cal);
}
}
uint16_t Pedal::vmin(void) {
return _cal.vmin;
}
uint16_t Pedal::vmax(void) {
return _cal.vmax;
}