Skip to content

Commit 10493b5

Browse files
committed
PINIO: fix operand order for backward compat, unify pinioSet/pinioSetDuty
Swap PINIO_PWM operands so operandA=duty, operandB=pin, matching the old LED_PIN_PWM behavior (operandA=duty only) for backward compatibility. Pin numbering: 0=LED pin, 1=USER1, 2=USER2, etc. Consolidate pinioSet into pinioSetDuty: on/off is just PWM at 0% or 100% duty. pinioSetDuty now handles both timer and GPIO pins; pinioSet becomes a one-liner wrapper. Removes redundant state tracking field.
1 parent bd31289 commit 10493b5

2 files changed

Lines changed: 23 additions & 34 deletions

File tree

src/main/drivers/pinio.c

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ typedef struct pinioRuntime_s {
6767
IO_t io;
6868
TCH_t *tch; // Non-NULL when pin is configured in PWM mode
6969
bool inverted;
70-
bool state;
7170
} pinioRuntime_t;
7271

7372
static pinioRuntime_t pinioRuntime[PINIO_COUNT];
@@ -103,7 +102,6 @@ void pinioInit(void)
103102
pinioRuntime[i].tch = tch;
104103
pinioRuntime[i].io = io;
105104
pinioRuntime[i].inverted = (pinioHardware[i].flags & PINIO_FLAGS_INVERTED) != 0;
106-
pinioRuntime[i].state = false;
107105
// Start in the "off" state: HIGH if inverted, LOW if normal
108106
*timerCCR(tch) = pinioRuntime[i].inverted ? 100 : 0;
109107
continue;
@@ -123,11 +121,10 @@ void pinioInit(void)
123121
}
124122

125123
pinioRuntime[i].io = io;
126-
pinioRuntime[i].state = false;
127124
}
128125
}
129126

130-
void pinioSet(int index, bool newState)
127+
void pinioSetDuty(int index, uint8_t duty)
131128
{
132129
if (index < 0 || index >= pinioHardwareCount) {
133130
return;
@@ -137,30 +134,23 @@ void pinioSet(int index, bool newState)
137134
return;
138135
}
139136

140-
if (newState != pinioRuntime[index].state) {
141-
if (pinioRuntime[index].tch) {
142-
*timerCCR(pinioRuntime[index].tch) = (newState ^ pinioRuntime[index].inverted) ? 100 : 0;
143-
} else {
144-
IOWrite(pinioRuntime[index].io, newState ^ pinioRuntime[index].inverted);
145-
}
146-
pinioRuntime[index].state = newState;
147-
}
148-
}
149-
150-
void pinioSetDuty(int index, uint8_t duty)
151-
{
152-
if (index < 0 || index >= pinioHardwareCount) {
153-
return;
137+
// Clamp to valid range
138+
if (duty > 100) {
139+
duty = 100;
154140
}
155141

156-
if (!pinioRuntime[index].tch) {
157-
return;
142+
if (pinioRuntime[index].tch) {
143+
// Timer-capable pin: set PWM duty cycle directly
144+
*timerCCR(pinioRuntime[index].tch) = pinioRuntime[index].inverted ? (100 - duty) : duty;
145+
} else {
146+
// GPIO pin: treat as on/off (0 = off, any non-zero = on)
147+
IOWrite(pinioRuntime[index].io, (duty > 0) ^ pinioRuntime[index].inverted);
158148
}
149+
}
159150

160-
// Clamp to valid range and apply inversion
161-
if (duty > 100) {
162-
duty = 100;
163-
}
164-
*timerCCR(pinioRuntime[index].tch) = pinioRuntime[index].inverted ? (100 - duty) : duty;
151+
// pinioSet is a convenience wrapper: on/off is just PWM at 100% or 0% duty
152+
void pinioSet(int index, bool newState)
153+
{
154+
pinioSetDuty(index, newState ? 100 : 0);
165155
}
166156
#endif

src/main/programming/logic_condition.c

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -509,17 +509,16 @@ static int logicConditionCompute(
509509

510510
#ifdef USE_PINIO
511511
case LOGIC_CONDITION_PINIO_PWM:
512-
// operandA = channel, operandB = duty cycle (0-100)
513-
// Channels 0..PINIO_COUNT-1 = hardware PINIO (PWM capable)
514-
// Channel PINIO_COUNT = LED strip idle level (binary: >0 = HIGH)
512+
// operandA = duty cycle (0-100), operandB = pin (0=LED pin, 1=USER1, 2=USER2, ...)
513+
// operandB=0 preserves backward compatibility with old LED_PIN_PWM behavior
514+
if (operandB == 0) {
515515
#ifdef USE_LED_STRIP
516-
if (operandA == PINIO_COUNT) {
517-
ws2811SetIdleHigh(operandB > 0);
518-
return operandB;
519-
}
516+
ws2811SetIdleHigh(operandA > 0);
520517
#endif
521-
pinioSetDuty(operandA, (uint8_t)constrain(operandB, 0, 100));
522-
return operandB;
518+
return operandA;
519+
}
520+
pinioSetDuty(operandB - 1, (uint8_t)constrain(operandA, 0, 100));
521+
return operandA;
523522
break;
524523
#endif
525524
#ifdef USE_GPS_FIX_ESTIMATION

0 commit comments

Comments
 (0)