-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeviceControl.cpp
134 lines (110 loc) · 3.64 KB
/
deviceControl.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
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
#include "pico/stdlib.h"
#include "deviceControl.h"
#include "serialCommunication.h"
#include "cstdio"
DeviceControl::DeviceControl()
{
DeviceControl::pinConfig = {
{ 0, NONE },
{ 1, NONE },
{ 2, DIGITAL_INPUT },
{ 3, DIGITAL_INPUT },
{ 4, DIGITAL_INPUT },
{ 5, DIGITAL_INPUT },
{ 6, DIGITAL_INPUT },
{ 7, DIGITAL_INPUT },
{ 8, DIGITAL_INPUT },
{ 9, DIGITAL_INPUT },
{ 10, DIGITAL_INPUT },
{ 11, DIGITAL_INPUT },
{ 12, DIGITAL_INPUT },
{ 13, DIGITAL_PWM_INPUT },
{ 14, DIGITAL_OUTPUT },
{ 15, DIGITAL_OUTPUT },
{ 16, DIGITAL_OUTPUT },
{ 17, DIGITAL_OUTPUT },
{ 18, DIGITAL_OUTPUT },
{ 19, DIGITAL_OUTPUT },
{ 20, DIGITAL_OUTPUT },
{ 21, DIGITAL_OUTPUT },
{ 22, NONE },
{ 23, NONE },
{ 24, NONE },
{ 25, DIGITAL_OUTPUT },
{ 26, DIGITAL_OUTPUT },
{ 27, DIGITAL_OUTPUT },
{ 28, DIGITAL_OUTPUT },
};
for ( auto &[pinNumber, pinMode]: DeviceControl::pinConfig ) {
switch ( pinMode ) {
case DIGITAL_OUTPUT:
gpio_init( pinNumber );
gpio_set_dir( pinNumber, GPIO_OUT );
break;
case DIGITAL_INPUT:
case DIGITAL_PWM_INPUT:
gpio_init( pinNumber );
gpio_set_dir( pinNumber, GPIO_IN );
gpio_set_irq_enabled_with_callback(
pinNumber, 0b1100, true, gpio_callback );
break;
// All interrupts fire on all pins no matter what you do... Switching based on port config instead
// case DIGITAL_PWM_INPUT:
// gpio_init( pinNumber );
// gpio_set_dir( pinNumber, GPIO_IN );
//
// gpio_set_irq_enabled_with_callback(
// pinNumber, 0b1100, true, gpio_phaseDuration_callback );
//
// break;
case PWM_OUTPUT:
gpio_init( pinNumber );
gpio_set_function( pinNumber, GPIO_FUNC_PWM );
break;
case NONE:
break;
}
}
}
void DeviceControl::gpio_callback( uint gpio, uint32_t event )
{
if ( DeviceControl::pinConfig[gpio] == DIGITAL_PWM_INPUT ) {
DeviceControl::gpio_phaseDuration_callback( gpio, event );
return;
}
bool isHighSignal = event == 8;
notifyPinValueChange( gpio, isHighSignal );
}
void DeviceControl::gpio_phaseDuration_callback( uint gpio, uint32_t event )
{
bool isHighSignal = event == 8;
// // pin filtering does not work correctly in the current SDK
// if ( DeviceControl::pinConfig[gpio] != DIGITAL_PWM_INPUT ) return;
if ( isHighSignal ) {
DeviceControl::lastPhaseRisingAt[gpio] = time_us_64();
} else {
auto pulseDuration = (time_us_64() - DeviceControl::lastPhaseRisingAt[gpio]) / 1e3;
// maybe should avoid sending relatively unchanged phase durations
char message[20];
sprintf( message, "%02i^%0.3f", gpio, pulseDuration );
SerialCommunication::addMessageToWrite( message );
}
}
void DeviceControl::setGpioPinValue( uint pinNumber, bool value, bool notify )
{
gpio_put( pinNumber, value );
if ( notify ) notifyPinValueChange( pinNumber, value );
}
bool DeviceControl::getGpioPinValue( uint pinNumber )
{
return gpio_get( pinNumber );
}
void DeviceControl::notifyPinValueChange( uint pinNumber, bool value )
{
char message[20];
sprintf( message, "%02i|%i",
pinNumber,
value ? 1 : 0
);
SerialCommunication::addMessageToWrite( message );
}