@@ -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