22
22
#include < Servo.h>
23
23
24
24
25
- static servo_t servos[MAX_SERVOS]; // static array of servo structures
26
-
27
- uint8_t ServoCount = 0 ; // the total number of attached servos
28
-
29
-
30
-
31
- // uint32_t group_pins[3][NRF_PWM_CHANNEL_COUNT]={{NRF_PWM_PIN_NOT_CONNECTED, NRF_PWM_PIN_NOT_CONNECTED, NRF_PWM_PIN_NOT_CONNECTED, NRF_PWM_PIN_NOT_CONNECTED}, {NRF_PWM_PIN_NOT_CONNECTED, NRF_PWM_PIN_NOT_CONNECTED, NRF_PWM_PIN_NOT_CONNECTED, NRF_PWM_PIN_NOT_CONNECTED}, {NRF_PWM_PIN_NOT_CONNECTED, NRF_PWM_PIN_NOT_CONNECTED, NRF_PWM_PIN_NOT_CONNECTED, NRF_PWM_PIN_NOT_CONNECTED}};
32
- // static uint16_t seq_values[3][NRF_PWM_CHANNEL_COUNT]={{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}};
25
+ static servo_t servos[MAX_SERVOS]; // static array of servo structures
26
+ uint8_t ServoCount = 0 ; // the total number of attached servos
33
27
34
28
Servo::Servo ()
35
29
{
@@ -39,15 +33,14 @@ Servo::Servo()
39
33
this ->servoIndex = INVALID_SERVO; // too many servos
40
34
}
41
35
36
+ this ->pwm = NULL ;
42
37
}
43
38
44
39
uint8_t Servo::attach (int pin)
45
40
{
46
-
47
41
return this ->attach (pin, MIN_PULSE_WIDTH, MAX_PULSE_WIDTH);
48
42
}
49
43
50
-
51
44
uint8_t Servo::attach (int pin, int min, int max)
52
45
{
53
46
if (this ->servoIndex < MAX_SERVOS) {
@@ -56,6 +49,7 @@ uint8_t Servo::attach(int pin, int min, int max)
56
49
57
50
if (min < MIN_PULSE_WIDTH) min = MIN_PULSE_WIDTH;
58
51
if (max > MAX_PULSE_WIDTH) max = MAX_PULSE_WIDTH;
52
+
59
53
// fix min if conversion to pulse cycle value is too low
60
54
if ((min/DUTY_CYCLE_RESOLUTION)*DUTY_CYCLE_RESOLUTION<min) min+=DUTY_CYCLE_RESOLUTION;
61
55
@@ -69,21 +63,21 @@ uint8_t Servo::attach(int pin, int min, int max)
69
63
{
70
64
if ( HwPWMx[i]->addPin (pin) )
71
65
{
72
- HwPWMx[i]->setMaxValue (MAXVALUE);
73
- HwPWMx[i]->setClockDiv (CLOCKDIV);
74
-
66
+ this ->pwm = HwPWMx[i];
75
67
break ;
76
68
}
77
69
}
78
70
71
+ this ->pwm ->setMaxValue (MAXVALUE);
72
+ this ->pwm ->setClockDiv (CLOCKDIV);
73
+
79
74
}
80
75
return this ->servoIndex ;
81
76
}
82
77
83
78
void Servo::detach ()
84
79
{
85
80
servos[this ->servoIndex ].Pin .isActive = false ;
86
-
87
81
// TODO Adafruit remove pin from HW PWM
88
82
}
89
83
@@ -102,38 +96,9 @@ void Servo::write(int value)
102
96
103
97
void Servo::writeMicroseconds (int value)
104
98
{
105
- #if 0
106
- uint8_t channel, instance;
107
- uint8_t pin = servos[this->servoIndex].Pin.nbr;
108
- //instance of pwm module is MSB - look at VWariant.h
109
- instance=(g_APinDescription[pin].ulPWMChannel & 0xF0)/16;
110
- //index of pwm channel is LSB - look at VWariant.h
111
- channel=g_APinDescription[pin].ulPWMChannel & 0x0F;
112
- group_pins[instance][channel]=g_APinDescription[pin].ulPin;
113
- NRF_PWM_Type * PWMInstance = instance == 0 ? NRF_PWM0 : (instance == 1 ? NRF_PWM1 : NRF_PWM2);
114
- //configure pwm instance and enable it
115
- seq_values[instance][channel]= value | 0x8000;
116
- nrf_pwm_sequence_t const seq={
117
- seq_values[instance],
118
- NRF_PWM_VALUES_LENGTH(seq_values),
119
- 0,
120
- 0
121
- };
122
- nrf_pwm_pins_set(PWMInstance, group_pins[instance]);
123
- nrf_pwm_enable(PWMInstance);
124
- nrf_pwm_configure(PWMInstance, NRF_PWM_CLK_125kHz, NRF_PWM_MODE_UP, 2500); // 20ms - 50Hz
125
- nrf_pwm_decoder_set(PWMInstance, NRF_PWM_LOAD_INDIVIDUAL, NRF_PWM_STEP_AUTO);
126
- nrf_pwm_sequence_set(PWMInstance, 0, &seq);
127
- nrf_pwm_loop_set(PWMInstance, 0UL);
128
- nrf_pwm_task_trigger(PWMInstance, NRF_PWM_TASK_SEQSTART0);
129
- #else
130
99
uint8_t pin = servos[this ->servoIndex ].Pin .nbr ;
131
100
132
- for (int i=0 ; i<HWPWM_MODULE_NUM; i++)
133
- {
134
- if ( HwPWMx[i]->writePin (pin, value/DUTY_CYCLE_RESOLUTION) ) break ;
135
- }
136
- #endif
101
+ if ( this ->pwm ) this ->pwm ->writePin (pin, value/DUTY_CYCLE_RESOLUTION);
137
102
}
138
103
139
104
int Servo::read () // return the value as degrees
@@ -143,26 +108,11 @@ int Servo::read() // return the value as degrees
143
108
144
109
int Servo::readMicroseconds ()
145
110
{
146
- #if 0
147
- uint8_t channel, instance;
148
- uint8_t pin=servos[this->servoIndex].Pin.nbr;
149
- instance=(g_APinDescription[pin].ulPWMChannel & 0xF0)/16;
150
- channel=g_APinDescription[pin].ulPWMChannel & 0x0F;
151
- // remove the 16th bit we added before
152
- return seq_values[instance][channel] & 0x7FFF;
153
- #else
154
111
uint8_t pin=servos[this ->servoIndex ].Pin .nbr ;
155
112
156
- for (int i=0 ; i<HWPWM_MODULE_NUM; i++)
157
- {
158
- if ( HwPWMx[i]->checkPin (pin) )
159
- {
160
- return HwPWMx[i]->readPin (pin)*DUTY_CYCLE_RESOLUTION;
161
- }
162
- }
113
+ if ( this ->pwm ) return this ->pwm ->readPin (pin)*DUTY_CYCLE_RESOLUTION;
163
114
164
115
return 0 ;
165
- #endif
166
116
}
167
117
168
118
bool Servo::attached ()
0 commit comments