Skip to content

Commit 8ab43c3

Browse files
set INPUT_PULLUP pinMode when attempting to write HIGH to an INPUT pin
1 parent 958b9a9 commit 8ab43c3

File tree

6 files changed

+84
-36
lines changed

6 files changed

+84
-36
lines changed

examples/StandardFirmata/StandardFirmata.ino

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -311,13 +311,15 @@ void setPinModeCallback(byte pin, int mode)
311311
}
312312

313313
/*
314-
* Sets the value of an individual pin.
315-
* Useful if you want to set a pin value but are not tracking the digital port state.
314+
* Sets the value of an individual pin. Useful if you want to set a pin value but
315+
* are not tracking the digital port state.
316+
* Can only be used on pins configured as OUTPUT.
317+
* Cannot be used to enable pull-ups on Digital INPUT pins.
316318
*/
317319
void setPinValueCallback(byte pin, int value)
318320
{
319321
if (pin < TOTAL_PINS && IS_PIN_DIGITAL(pin)) {
320-
if (pinConfig[pin] == OUTPUT || pinConfig[pin] == INPUT) {
322+
if (pinConfig[pin] == OUTPUT) {
321323
pinState[pin] = value;
322324
digitalWrite(PIN_TO_DIGITAL(pin), value);
323325
}
@@ -353,13 +355,19 @@ void digitalWriteCallback(byte port, int value)
353355
for (pin = port * 8; pin < lastPin; pin++) {
354356
// do not disturb non-digital pins (eg, Rx & Tx)
355357
if (IS_PIN_DIGITAL(pin)) {
356-
// only write to OUTPUT and INPUT (enables pullup)
357358
// do not touch pins in PWM, ANALOG, SERVO or other modes
358359
if (pinConfig[pin] == OUTPUT || pinConfig[pin] == INPUT) {
359360
pinValue = ((byte)value & mask) ? 1 : 0;
360-
// temporary fix until INPUT_PULLUP is added
361-
if (pinConfig[pin] == OUTPUT || (pinConfig[pin] == INPUT && pinValue == 1)) {
361+
if (pinConfig[pin] == OUTPUT) {
362362
pinWriteMask |= mask;
363+
} else if (pinConfig[pin] == INPUT && pinValue == 1 && pinState[pin] != 1) {
364+
// only handle INPUT here for backwards compatibility
365+
#if ARDUINO > 100
366+
pinMode(pin, INPUT_PULLUP);
367+
#else
368+
// only write to the INPUT pin to enable pullups if Arduino v1.0.0 or earlier
369+
pinWriteMask |= mask;
370+
#endif
363371
}
364372
pinState[pin] = pinValue;
365373
}

examples/StandardFirmataChipKIT/StandardFirmataChipKIT.ino

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -322,13 +322,15 @@ void setPinModeCallback(byte pin, int mode)
322322
}
323323

324324
/*
325-
* Sets the value of an individual pin.
326-
* Useful if you want to set a pin value but are not tracking the digital port state.
325+
* Sets the value of an individual pin. Useful if you want to set a pin value but
326+
* are not tracking the digital port state.
327+
* Can only be used on pins configured as OUTPUT.
328+
* Cannot be used to enable pull-ups on Digital INPUT pins.
327329
*/
328330
void setPinValueCallback(byte pin, int value)
329331
{
330332
if (pin < TOTAL_PINS && IS_PIN_DIGITAL(pin)) {
331-
if (pinConfig[pin] == OUTPUT || pinConfig[pin] == INPUT) {
333+
if (pinConfig[pin] == OUTPUT) {
332334
pinState[pin] = value;
333335
digitalWrite(PIN_TO_DIGITAL(pin), value);
334336
}
@@ -364,13 +366,19 @@ void digitalWriteCallback(byte port, int value)
364366
for (pin = port * 8; pin < lastPin; pin++) {
365367
// do not disturb non-digital pins (eg, Rx & Tx)
366368
if (IS_PIN_DIGITAL(pin)) {
367-
// only write to OUTPUT and INPUT (enables pullup)
368369
// do not touch pins in PWM, ANALOG, SERVO or other modes
369370
if (pinConfig[pin] == OUTPUT || pinConfig[pin] == INPUT) {
370371
pinValue = ((byte)value & mask) ? 1 : 0;
371-
// temporary fix until INPUT_PULLUP is added
372-
if (pinConfig[pin] == OUTPUT || (pinConfig[pin] == INPUT && pinValue == 1)) {
372+
if (pinConfig[pin] == OUTPUT) {
373373
pinWriteMask |= mask;
374+
} else if (pinConfig[pin] == INPUT && pinValue == 1 && pinState[pin] != 1) {
375+
// only handle INPUT here for backwards compatibility
376+
#if ARDUINO > 100
377+
pinMode(pin, INPUT_PULLUP);
378+
#else
379+
// only write to the INPUT pin to enable pullups if Arduino v1.0.0 or earlier
380+
pinWriteMask |= mask;
381+
#endif
374382
}
375383
pinState[pin] = pinValue;
376384
}

examples/StandardFirmataEthernet/StandardFirmataEthernet.ino

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -427,13 +427,15 @@ void setPinModeCallback(byte pin, int mode)
427427
}
428428

429429
/*
430-
* Sets the value of an individual pin.
431-
* Useful if you want to set a pin value but are not tracking the digital port state.
430+
* Sets the value of an individual pin. Useful if you want to set a pin value but
431+
* are not tracking the digital port state.
432+
* Can only be used on pins configured as OUTPUT.
433+
* Cannot be used to enable pull-ups on Digital INPUT pins.
432434
*/
433435
void setPinValueCallback(byte pin, int value)
434436
{
435437
if (pin < TOTAL_PINS && IS_PIN_DIGITAL(pin)) {
436-
if (pinConfig[pin] == OUTPUT || pinConfig[pin] == INPUT) {
438+
if (pinConfig[pin] == OUTPUT) {
437439
pinState[pin] = value;
438440
digitalWrite(PIN_TO_DIGITAL(pin), value);
439441
}
@@ -469,13 +471,19 @@ void digitalWriteCallback(byte port, int value)
469471
for (pin = port * 8; pin < lastPin; pin++) {
470472
// do not disturb non-digital pins (eg, Rx & Tx)
471473
if (IS_PIN_DIGITAL(pin)) {
472-
// only write to OUTPUT and INPUT (enables pullup)
473474
// do not touch pins in PWM, ANALOG, SERVO or other modes
474475
if (pinConfig[pin] == OUTPUT || pinConfig[pin] == INPUT) {
475476
pinValue = ((byte)value & mask) ? 1 : 0;
476-
// temporary fix until INPUT_PULLUP is added
477-
if (pinConfig[pin] == OUTPUT || (pinConfig[pin] == INPUT && pinValue == 1)) {
477+
if (pinConfig[pin] == OUTPUT) {
478478
pinWriteMask |= mask;
479+
} else if (pinConfig[pin] == INPUT && pinValue == 1 && pinState[pin] != 1) {
480+
// only handle INPUT here for backwards compatibility
481+
#if ARDUINO > 100
482+
pinMode(pin, INPUT_PULLUP);
483+
#else
484+
// only write to the INPUT pin to enable pullups if Arduino v1.0.0 or earlier
485+
pinWriteMask |= mask;
486+
#endif
479487
}
480488
pinState[pin] = pinValue;
481489
}

examples/StandardFirmataEthernetPlus/StandardFirmataEthernetPlus.ino

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -517,13 +517,15 @@ void setPinModeCallback(byte pin, int mode)
517517
}
518518

519519
/*
520-
* Sets the value of an individual pin.
521-
* Useful if you want to set a pin value but are not tracking the digital port state.
520+
* Sets the value of an individual pin. Useful if you want to set a pin value but
521+
* are not tracking the digital port state.
522+
* Can only be used on pins configured as OUTPUT.
523+
* Cannot be used to enable pull-ups on Digital INPUT pins.
522524
*/
523525
void setPinValueCallback(byte pin, int value)
524526
{
525527
if (pin < TOTAL_PINS && IS_PIN_DIGITAL(pin)) {
526-
if (pinConfig[pin] == OUTPUT || pinConfig[pin] == INPUT) {
528+
if (pinConfig[pin] == OUTPUT) {
527529
pinState[pin] = value;
528530
digitalWrite(PIN_TO_DIGITAL(pin), value);
529531
}
@@ -559,13 +561,19 @@ void digitalWriteCallback(byte port, int value)
559561
for (pin = port * 8; pin < lastPin; pin++) {
560562
// do not disturb non-digital pins (eg, Rx & Tx)
561563
if (IS_PIN_DIGITAL(pin)) {
562-
// only write to OUTPUT and INPUT (enables pullup)
563564
// do not touch pins in PWM, ANALOG, SERVO or other modes
564565
if (pinConfig[pin] == OUTPUT || pinConfig[pin] == INPUT) {
565566
pinValue = ((byte)value & mask) ? 1 : 0;
566-
// temporary fix until INPUT_PULLUP is added
567-
if (pinConfig[pin] == OUTPUT || (pinConfig[pin] == INPUT && pinValue == 1)) {
567+
if (pinConfig[pin] == OUTPUT) {
568568
pinWriteMask |= mask;
569+
} else if (pinConfig[pin] == INPUT && pinValue == 1 && pinState[pin] != 1) {
570+
// only handle INPUT here for backwards compatibility
571+
#if ARDUINO > 100
572+
pinMode(pin, INPUT_PULLUP);
573+
#else
574+
// only write to the INPUT pin to enable pullups if Arduino v1.0.0 or earlier
575+
pinWriteMask |= mask;
576+
#endif
569577
}
570578
pinState[pin] = pinValue;
571579
}

examples/StandardFirmataPlus/StandardFirmataPlus.ino

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -449,13 +449,15 @@ void setPinModeCallback(byte pin, int mode)
449449
}
450450

451451
/*
452-
* Sets the value of an individual pin.
453-
* Useful if you want to set a pin value but are not tracking the digital port state.
452+
* Sets the value of an individual pin. Useful if you want to set a pin value but
453+
* are not tracking the digital port state.
454+
* Can only be used on pins configured as OUTPUT.
455+
* Cannot be used to enable pull-ups on Digital INPUT pins.
454456
*/
455457
void setPinValueCallback(byte pin, int value)
456458
{
457459
if (pin < TOTAL_PINS && IS_PIN_DIGITAL(pin)) {
458-
if (pinConfig[pin] == OUTPUT || pinConfig[pin] == INPUT) {
460+
if (pinConfig[pin] == OUTPUT) {
459461
pinState[pin] = value;
460462
digitalWrite(PIN_TO_DIGITAL(pin), value);
461463
}
@@ -491,13 +493,19 @@ void digitalWriteCallback(byte port, int value)
491493
for (pin = port * 8; pin < lastPin; pin++) {
492494
// do not disturb non-digital pins (eg, Rx & Tx)
493495
if (IS_PIN_DIGITAL(pin)) {
494-
// only write to OUTPUT and INPUT (enables pullup)
495496
// do not touch pins in PWM, ANALOG, SERVO or other modes
496497
if (pinConfig[pin] == OUTPUT || pinConfig[pin] == INPUT) {
497498
pinValue = ((byte)value & mask) ? 1 : 0;
498-
// temporary fix until INPUT_PULLUP is added
499-
if (pinConfig[pin] == OUTPUT || (pinConfig[pin] == INPUT && pinValue == 1)) {
499+
if (pinConfig[pin] == OUTPUT) {
500500
pinWriteMask |= mask;
501+
} else if (pinConfig[pin] == INPUT && pinValue == 1 && pinState[pin] != 1) {
502+
// only handle INPUT here for backwards compatibility
503+
#if ARDUINO > 100
504+
pinMode(pin, INPUT_PULLUP);
505+
#else
506+
// only write to the INPUT pin to enable pullups if Arduino v1.0.0 or earlier
507+
pinWriteMask |= mask;
508+
#endif
501509
}
502510
pinState[pin] = pinValue;
503511
}

examples/StandardFirmataYun/StandardFirmataYun.ino

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -323,13 +323,15 @@ void setPinModeCallback(byte pin, int mode)
323323
}
324324

325325
/*
326-
* Sets the value of an individual pin.
327-
* Useful if you want to set a pin value but are not tracking the digital port state.
326+
* Sets the value of an individual pin. Useful if you want to set a pin value but
327+
* are not tracking the digital port state.
328+
* Can only be used on pins configured as OUTPUT.
329+
* Cannot be used to enable pull-ups on Digital INPUT pins.
328330
*/
329331
void setPinValueCallback(byte pin, int value)
330332
{
331333
if (pin < TOTAL_PINS && IS_PIN_DIGITAL(pin)) {
332-
if (pinConfig[pin] == OUTPUT || pinConfig[pin] == INPUT) {
334+
if (pinConfig[pin] == OUTPUT) {
333335
pinState[pin] = value;
334336
digitalWrite(PIN_TO_DIGITAL(pin), value);
335337
}
@@ -365,13 +367,19 @@ void digitalWriteCallback(byte port, int value)
365367
for (pin = port * 8; pin < lastPin; pin++) {
366368
// do not disturb non-digital pins (eg, Rx & Tx)
367369
if (IS_PIN_DIGITAL(pin)) {
368-
// only write to OUTPUT and INPUT (enables pullup)
369370
// do not touch pins in PWM, ANALOG, SERVO or other modes
370371
if (pinConfig[pin] == OUTPUT || pinConfig[pin] == INPUT) {
371372
pinValue = ((byte)value & mask) ? 1 : 0;
372-
// temporary fix until INPUT_PULLUP is added
373-
if (pinConfig[pin] == OUTPUT || (pinConfig[pin] == INPUT && pinValue == 1)) {
373+
if (pinConfig[pin] == OUTPUT) {
374374
pinWriteMask |= mask;
375+
} else if (pinConfig[pin] == INPUT && pinValue == 1 && pinState[pin] != 1) {
376+
// only handle INPUT here for backwards compatibility
377+
#if ARDUINO > 100
378+
pinMode(pin, INPUT_PULLUP);
379+
#else
380+
// only write to the INPUT pin to enable pullups if Arduino v1.0.0 or earlier
381+
pinWriteMask |= mask;
382+
#endif
375383
}
376384
pinState[pin] = pinValue;
377385
}

0 commit comments

Comments
 (0)