Skip to content

Commit 02ab329

Browse files
authored
Merge pull request #11 from maidnl/get_PWM_functions_used_wrong_index
Opta Controller: Get pwm functions used wrong index
2 parents c6abc0a + 477bb77 commit 02ab329

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

examples/Analog/PWM/PWM.ino

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ void optaAnalogTask() {
101101
rising = 1;
102102
}
103103
}
104+
104105
for(int i = 0; i < OptaController.getExpansionNum(); i++) {
105106

106107
AnalogExpansion aexp = OptaController.getExpansion(i);
@@ -122,6 +123,25 @@ void optaAnalogTask() {
122123
}
123124
}
124125
}
126+
for(int i = 0; i < OptaController.getExpansionNum(); i++) {
127+
128+
AnalogExpansion aexp = OptaController.getExpansion(i);
129+
130+
if(aexp) {
131+
Serial.println("PWM ch 0 period " +
132+
String(aexp.getPwmPeriod(OA_FIRST_PWM_CH))+
133+
" pulse " + aexp.getPwmPulse(OA_FIRST_PWM_CH));
134+
Serial.println("PWM ch 1 period " +
135+
String(aexp.getPwmPeriod(OA_FIRST_PWM_CH + 1)) +
136+
" pulse " + aexp.getPwmPulse(OA_FIRST_PWM_CH + 1));
137+
Serial.println("PWM ch 2 period " +
138+
String(aexp.getPwmPeriod(OA_FIRST_PWM_CH + 2)) +
139+
" pulse " + aexp.getPwmPulse(OA_FIRST_PWM_CH + 2));
140+
Serial.println("PWM ch 3 period " +
141+
String(aexp.getPwmPeriod(OA_FIRST_PWM_CH + 3))+
142+
" pulse " + aexp.getPwmPulse(OA_FIRST_PWM_CH + 2));
143+
}
144+
}
125145
}
126146
}
127147

src/AnalogExpansion.cpp

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -502,8 +502,17 @@ void AnalogExpansion::setPwm(uint8_t ch, uint32_t period, uint32_t pulse) {
502502
* the value is already up to date*/
503503
}
504504

505+
/* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
506+
505507
/* get Pwm period in micro seconds */
506508
uint32_t AnalogExpansion::getPwmPeriod(uint8_t ch) {
509+
if (ch >= OA_FIRST_PWM_CH && ch <= OA_LAST_PWM_CH) {
510+
ch = ch - OA_FIRST_PWM_CH;
511+
}
512+
else if(ch >= OA_PWM_CHANNELS_NUM) {
513+
return 0;
514+
}
515+
507516
uint32_t per_add = BASE_OA_PWM_ADDRESS + ch;
508517
if (!addressExist(per_add)) {
509518
return 0;
@@ -512,18 +521,29 @@ void AnalogExpansion::setPwm(uint8_t ch, uint32_t period, uint32_t pulse) {
512521
return iregs[per_add];
513522
}
514523
}
524+
525+
/* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
526+
515527
/* get Pwm pulse in micro seconds */
516528
uint32_t AnalogExpansion::getPwmPulse(uint8_t ch) {
529+
if (ch >= OA_FIRST_PWM_CH && ch <= OA_LAST_PWM_CH) {
530+
ch = ch - OA_FIRST_PWM_CH;
531+
}
532+
else if(ch >= OA_PWM_CHANNELS_NUM) {
533+
return 0;
534+
}
535+
517536
uint32_t pul_add = BASE_OA_PWM_ADDRESS + ch + OA_PWM_CHANNELS_NUM;
518537
if (!addressExist(pul_add)) {
519538
return 0;
520539
}
521540
else {
522541
return iregs[pul_add];
523542
}
524-
525543
}
526544

545+
/* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
546+
527547
float AnalogExpansion::getPwmFreqHz(uint8_t ch) {
528548
float period = (float)getPwmPeriod(ch);
529549
if(period > 0) {
@@ -534,11 +554,13 @@ void AnalogExpansion::setPwm(uint8_t ch, uint32_t period, uint32_t pulse) {
534554

535555
}
536556

557+
/* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
558+
537559
float AnalogExpansion::getPwmPulsePerc(uint8_t ch) {
538560
float period = (float)getPwmPeriod(ch);
539561
float pulse = (float)getPwmPulse(ch);
540562
if(period > 0 && pulse <= period) {
541-
return pulse / period;
563+
return pulse * 100.0 / period;
542564
}
543565
return 0.0;
544566

0 commit comments

Comments
 (0)