Skip to content

Commit 787a742

Browse files
committed
Improvements
1 parent fac1fd3 commit 787a742

File tree

8 files changed

+93
-59
lines changed

8 files changed

+93
-59
lines changed

.github/workflows/LibraryBuild.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,12 @@ jobs:
5353
platform-url: https://raw.githubusercontent.com/ArminJo/DigistumpArduino/master/package_digistump_index.json
5454

5555
- arduino-boards-fqbn: ATTinyCore:avr:attinyx5:chip=85,clock=1internal # ATtiny85 board @1 MHz / Digispark
56-
platform-url: http://drazzy.com/package_drazzy.com_index.json
56+
platform-url: https://felias-fogg.github.io/downloads/package_debug_enabled_index.json
5757
build-properties:
5858
TwoButtons: -DTX_PIN=PB0
5959

6060
- arduino-boards-fqbn: ATTinyCore:avr:attinyx7micr:sketchclock=1external16 # Digispark pro
61-
platform-url: http://drazzy.com/package_drazzy.com_index.json
61+
platform-url: https://felias-fogg.github.io/downloads/package_debug_enabled_index.json
6262
build-properties:
6363
TwoButtons: -DTX_PIN=PB0
6464

.github/workflows/PlatformIoPublish.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,3 @@ jobs:
4040
# run: |
4141
# pio package pack
4242
# pio package publish --owner arminjo --non-interactive
43-

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ The easiest way is to use the button release handler. Keep in mind, that you wil
123123
#include "EasyButtonAtInt01.hpp"
124124
125125
void handleButtonRelease(bool aButtonToggleState, uint16_t aButtonPressDurationMillis);
126-
EasyButton Button0AtPin2(NULL, &handleButtonRelease); // Button is connected to INT0 (pin2)
126+
EasyButton Button0AtPin2(nullptr, &handleButtonRelease); // Button is connected to INT0 (pin2)
127127
128128
handleButtonRelease(bool aButtonToggleState, uint16_t aButtonPressDurationMillis) {
129129
if (aButtonPressDurationMillis >= EASY_BUTTON_LONG_PRESS_DEFAULT_MILLIS) { // 400 ms

examples/DebounceTest/DebounceTest.ino

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,26 @@ EasyButton Button0AtPin2; // Only 1. button (USE_BUTTON_0) enabled -> button is
4646
#define LED_BUILTIN PB1 // define port of built in LED for your ATtiny
4747
#endif
4848

49+
// Helper macro for getting a macro definition as string
50+
#if !defined(STR_HELPER) && !defined(STR)
4951
#define STR_HELPER(x) #x
5052
#define STR(x) STR_HELPER(x)
53+
#endif
54+
5155
void setup() {
5256
// initialize the digital pin as an output.
5357
pinMode(LED_BUILTIN, OUTPUT);
5458

5559
Serial.begin(115200);
60+
5661
#if defined(__AVR_ATmega32U4__) || defined(SERIAL_PORT_USBVIRTUAL) || defined(SERIAL_USB) /*stm32duino*/|| defined(USBCON) /*STM32_stm32*/ \
5762
|| defined(SERIALUSB_PID) || defined(ARDUINO_ARCH_RP2040) || defined(ARDUINO_attiny3217)
58-
delay(4000); // To be able to connect Serial monitor after reset or power up and before first print out. Do not wait for an attached Serial Monitor!
63+
// Wait until Serial Monitor is attached.
64+
// Required for boards using USB code for Serial like Leonardo.
65+
// Is void for USB Serial implementations using external chips e.g. a CH340.
66+
while (!Serial)
67+
;
68+
// !!! Program will not proceed if no Serial Monitor is attached !!!
5969
#endif
6070
// Just to know which program is running on my Arduino
6171
Serial.println(F("START " __FILE__ "\r\nUsing library version " VERSION_EASY_BUTTON " from " __DATE__));

examples/TwoButtons/ATtinySerialOut.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ class TinySerialOut
195195

196196
// virtual functions of Print class
197197
size_t write(uint8_t aByte);
198-
operator bool() { return true; } // To support "while (!Serial); // wait for serial port to connect. Required for Leonardo only
198+
operator bool() { return true; } // To support "while (!Serial); // Dummy wait for serial port to be connected.
199199

200200
#if !defined(TINY_SERIAL_INHERIT_FROM_PRINT)
201201
void print(const __FlashStringHelper *aStringPtr);

src/EasyButtonAtInt01.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -354,9 +354,9 @@ class EasyButton {
354354
#endif
355355

356356
volatile bool isButtonAtINT0;
357-
void (*ButtonPressCallback)(bool aButtonToggleState) = NULL; // If not null, is called on every button press with ButtonToggleState as parameter
357+
void (*ButtonPressCallback)(bool aButtonToggleState) = nullptr; // If not null, is called on every button press with ButtonToggleState as parameter
358358
#if !defined(NO_BUTTON_RELEASE_CALLBACK)
359-
void (*ButtonReleaseCallback)(bool aButtonToggleState, uint16_t aButtonPressDurationMillis) = NULL; // If not null, is called on every button release with ButtonPressDurationMillis as parameter
359+
void (*ButtonReleaseCallback)(bool aButtonToggleState, uint16_t aButtonPressDurationMillis) = nullptr; // If not null, is called on every button release with ButtonPressDurationMillis as parameter
360360
#endif
361361

362362
#if defined(USE_BUTTON_0)

src/EasyButtonAtInt01.hpp

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,6 @@
7373
*
7474
*/
7575

76-
#if defined(TRACE)
77-
#define LOCAL_TRACE
78-
#else
79-
//#define LOCAL_TRACE // This enables trace output only for this file
80-
#endif
81-
8276
// For external measurement of code timing
8377
//#define MEASURE_EASY_BUTTON_INTERRUPT_TIMING
8478
#if defined(MEASURE_EASY_BUTTON_INTERRUPT_TIMING) || defined(BUTTON_LED_FEEDBACK)
@@ -95,8 +89,15 @@ EasyButton *EasyButton::sPointerToButton1ForISR;
9589
#error One of USE_BUTTON_0 or USE_BUTTON_1 must be defined
9690
#endif
9791

92+
// After all includes
93+
#if defined(TRACE)
94+
#define LOCAL_TRACE
95+
#else
96+
//#define LOCAL_TRACE // This enables trace output only for this file
97+
#endif
98+
9899
// The eclipse formatter has problems with // comments in undefined code blocks
99-
// !!! Must be without comment and closed by @formatter:on
100+
// !!! Must be without trailing comment and closed by @formatter:on
100101
// @formatter:off
101102

102103
/*
@@ -221,7 +222,11 @@ void EasyButton::init(bool aIsButtonAtINT0) {
221222
# endif
222223
sPointerToButton0ForISR = this;
223224
# if defined(USE_ATTACH_INTERRUPT)
225+
# if defined(ARDUINO_ARCH_SAMD) // see https://www.arduino.cc/reference/tr/language/functions/external-interrupts/attachinterrupt/ paragraph: Syntax
226+
attachInterrupt(INT0_PIN, &handleINT1Interrupt, CHANGE);
227+
# else
224228
attachInterrupt(digitalPinToInterrupt(INT0_PIN), &handleINT0Interrupt, CHANGE);
229+
# endif
225230

226231
# elif defined(USE_INT2_FOR_BUTTON_0)
227232
EICRA |= _BV(ISC20); // interrupt on any logical change
@@ -261,7 +266,11 @@ void EasyButton::init(bool aIsButtonAtINT0) {
261266
PCMSK2 = digitalPinToBitMask(INT1_PIN);
262267
# else
263268
# if defined(USE_ATTACH_INTERRUPT)
269+
# if defined(ARDUINO_ARCH_SAMD) // see https://www.arduino.cc/reference/tr/language/functions/external-interrupts/attachinterrupt/ paragraph: Syntax
270+
attachInterrupt(INT1_PIN, &handleINT1Interrupt, CHANGE);
271+
# else
264272
attachInterrupt(digitalPinToInterrupt(INT1_PIN), &handleINT1Interrupt, CHANGE);
273+
# endif
265274
# else
266275
EICRA |= _BV(ISC10); // interrupt on any logical change
267276
EIFR |= _BV(INTF1); // clear interrupt bit
@@ -283,7 +292,11 @@ void EasyButton::init(bool aIsButtonAtINT0) {
283292
# endif
284293
sPointerToButton0ForISR = this;
285294
# if defined(USE_ATTACH_INTERRUPT)
295+
# if defined(ARDUINO_ARCH_SAMD) // see https://www.arduino.cc/reference/tr/language/functions/external-interrupts/attachinterrupt/ paragraph: Syntax
296+
attachInterrupt(INT1_PIN, &handleINT1Interrupt, CHANGE);
297+
# else
286298
attachInterrupt(digitalPinToInterrupt(INT0_PIN), &handleINT0Interrupt, CHANGE);
299+
# endif
287300
# else
288301
EICRA |= _BV(ISC00); // interrupt on any logical change
289302
EIFR |= _BV(INTF0); // clear interrupt bit
@@ -331,7 +344,11 @@ void EasyButton::init(bool aIsButtonAtINT0) {
331344
PCMSK1 = digitalPinToBitMask(INT1_PIN);
332345
# else
333346
# if defined(USE_ATTACH_INTERRUPT)
347+
# if defined(ARDUINO_ARCH_SAMD) // see https://www.arduino.cc/reference/tr/language/functions/external-interrupts/attachinterrupt/ paragraph: Syntax
348+
attachInterrupt(INT1_PIN, &handleINT1Interrupt, CHANGE);
349+
# else
334350
attachInterrupt(digitalPinToInterrupt(INT1_PIN), &handleINT1Interrupt, CHANGE);
351+
# endif
335352
# else
336353
// ATmega328 here
337354
EICRA |= _BV(ISC10); // interrupt on any logical change
@@ -646,7 +663,7 @@ void EasyButton::handleINT01Interrupts() {
646663
digitalWriteFast(BUTTON_LED_FEEDBACK_PIN, HIGH);
647664
#endif
648665
ButtonToggleState = !ButtonToggleState;
649-
if (ButtonPressCallback != NULL) {
666+
if (ButtonPressCallback != nullptr) {
650667
/*
651668
* Call callback function.
652669
* interrupts() is required if callback function needs more time to allow millis() to proceed.
@@ -677,7 +694,7 @@ void EasyButton::handleINT01Interrupts() {
677694
ButtonPressDurationMillis = tDeltaMillis;
678695
ButtonReleaseMillis = tMillis;
679696
#if !defined(NO_BUTTON_RELEASE_CALLBACK)
680-
if (ButtonReleaseCallback != NULL) {
697+
if (ButtonReleaseCallback != nullptr) {
681698
/*
682699
* Call callback function.
683700
* interrupts() is required if callback function needs more time to allow millis() to proceed.

src/digitalWriteFast.h

Lines changed: 50 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
* by Watterott electronic (www.watterott.com)
66
* based on https://code.google.com/p/digitalwritefast
77
*
8+
* The value of DigitalReadFast() is the content of the input register e.g. 0x04 for pin2 and NOT always 0 or 1.
9+
*
810
* License: BSD 3-Clause License (https://opensource.org/licenses/BSD-3-Clause)
911
*/
1012

@@ -324,18 +326,22 @@ int NonConstantsUsedForDigitalReadFast( void ) __attribute__ (( error("Paramete
324326
# if (defined(__AVR__) || defined(ARDUINO_ARCH_AVR)) && defined(__digitalPinToPortReg)
325327
# if defined(THROW_ERROR_IF_NOT_FAST)
326328
#define digitalWriteFast(P, V) \
327-
if (__builtin_constant_p(P)) { \
328-
BIT_WRITE(*__digitalPinToPortReg(P), __digitalPinToBit(P), (V)); \
329-
} else { \
329+
do { \
330+
if (__builtin_constant_p(P)) { \
331+
BIT_WRITE(*__digitalPinToPortReg(P), __digitalPinToBit(P), (V)); \
332+
} else { \
330333
NonConstantsUsedForDigitalWriteFast(); \
331-
}
334+
} \
335+
} while (0)
332336
# else
333337
#define digitalWriteFast(P, V) \
334-
if (__builtin_constant_p(P)) { \
335-
BIT_WRITE(*__digitalPinToPortReg(P), __digitalPinToBit(P), (V)); \
336-
} else { \
337-
digitalWrite((P), (V)); \
338-
}
338+
do { \
339+
if (__builtin_constant_p(P)) { \
340+
BIT_WRITE(*__digitalPinToPortReg(P), __digitalPinToBit(P), (V)); \
341+
} else { \
342+
digitalWrite((P), (V)); \
343+
} \
344+
} while (0)
339345
# endif // defined(THROW_ERROR_IF_NOT_FAST)
340346
# else
341347
#define digitalWriteFast digitalWrite
@@ -346,28 +352,32 @@ if (__builtin_constant_p(P)) { \
346352
# if (defined(__AVR__) || defined(ARDUINO_ARCH_AVR)) && defined(__digitalPinToPortReg)
347353
# if defined(THROW_ERROR_IF_NOT_FAST)
348354
#define pinModeFast(P, V) \
349-
if (__builtin_constant_p(P) && __builtin_constant_p(V)) { \
350-
if (V == INPUT_PULLUP) {\
351-
BIT_CLEAR(*__digitalPinToDDRReg(P), __digitalPinToBit(P)); \
352-
BIT_SET(*__digitalPinToPortReg(P), __digitalPinToBit(P)); \
355+
do { \
356+
if (__builtin_constant_p(P) && __builtin_constant_p(V)) { \
357+
if (V == INPUT_PULLUP) {\
358+
BIT_CLEAR(*__digitalPinToDDRReg(P), __digitalPinToBit(P)); \
359+
BIT_SET(*__digitalPinToPortReg(P), __digitalPinToBit(P)); \
360+
} else { \
361+
BIT_WRITE(*__digitalPinToDDRReg(P), __digitalPinToBit(P), (V)); \
362+
} \
353363
} else { \
354-
BIT_WRITE(*__digitalPinToDDRReg(P), __digitalPinToBit(P), (V)); \
355-
} \
356-
} else { \
357364
NonConstantsUsedForPinModeFast(); \
358-
}
365+
} \
366+
} while (0)
359367
# else
360368
#define pinModeFast(P, V) \
361-
if (__builtin_constant_p(P) && __builtin_constant_p(V)) { \
362-
if (V == INPUT_PULLUP) {\
363-
BIT_CLEAR(*__digitalPinToDDRReg(P), __digitalPinToBit(P)); \
364-
BIT_SET(*__digitalPinToPortReg(P), __digitalPinToBit(P)); \
369+
do { \
370+
if (__builtin_constant_p(P) && __builtin_constant_p(V)) { \
371+
if (V == INPUT_PULLUP) {\
372+
BIT_CLEAR(*__digitalPinToDDRReg(P), __digitalPinToBit(P)); \
373+
BIT_SET(*__digitalPinToPortReg(P), __digitalPinToBit(P)); \
374+
} else { \
375+
BIT_WRITE(*__digitalPinToDDRReg(P), __digitalPinToBit(P), (V)); \
376+
} \
365377
} else { \
366-
BIT_WRITE(*__digitalPinToDDRReg(P), __digitalPinToBit(P), (V)); \
378+
pinMode((P), (V)); \
367379
} \
368-
} else { \
369-
pinMode((P), (V)); \
370-
}
380+
} while (0)
371381
# endif // defined(THROW_ERROR_IF_NOT_FAST)
372382
# else
373383
#define pinModeFast pinMode
@@ -379,17 +389,11 @@ if (__builtin_constant_p(P) && __builtin_constant_p(V)) { \
379389
# if defined(THROW_ERROR_IF_NOT_FAST)
380390
#define digitalReadFast(P) ( (int) __digitalReadFast((P)) )
381391
// since we have return values, it is easier to implement it by ?:
382-
#define __digitalReadFast(P ) \
383-
(__builtin_constant_p(P) ) ? \
384-
(( BIT_READ(*__digitalPinToPINReg(P), __digitalPinToBit(P))) ? HIGH:LOW ) : \
385-
NonConstantsUsedForDigitalReadFast()
392+
#define __digitalReadFast(P ) ( (__builtin_constant_p(P) ) ? (( BIT_READ(*__digitalPinToPINReg(P), __digitalPinToBit(P))) ? HIGH:LOW ) : NonConstantsUsedForDigitalReadFast() )
386393
# else
387394
#define digitalReadFast(P) ( (int) __digitalReadFast((P)) )
388395
// since we have return values, it is easier to implement it by ?:
389-
#define __digitalReadFast(P ) \
390-
(__builtin_constant_p(P) ) ? \
391-
(( BIT_READ(*__digitalPinToPINReg(P), __digitalPinToBit(P))) ? HIGH:LOW ) : \
392-
digitalRead((P))
396+
#define __digitalReadFast(P ) ( (__builtin_constant_p(P) ) ? (( BIT_READ(*__digitalPinToPINReg(P), __digitalPinToBit(P))) ? HIGH:LOW ) : digitalRead((P)) )
393397
# endif // defined(THROW_ERROR_IF_NOT_FAST)
394398
# else
395399
#define digitalReadFast digitalRead
@@ -400,18 +404,22 @@ if (__builtin_constant_p(P) && __builtin_constant_p(V)) { \
400404
# if (defined(__AVR__) || defined(ARDUINO_ARCH_AVR)) && defined(__digitalPinToPINReg)
401405
# if defined(THROW_ERROR_IF_NOT_FAST)
402406
#define digitalToggleFast(P) \
403-
if (__builtin_constant_p(P)) { \
404-
BIT_SET(*__digitalPinToPINReg(P), __digitalPinToBit(P)); \
405-
} else { \
407+
do { \
408+
if (__builtin_constant_p(P)) { \
409+
BIT_SET(*__digitalPinToPINReg(P), __digitalPinToBit(P)); \
410+
} else { \
406411
NonConstantsUsedForDigitalToggleFast(); \
407-
}
412+
} \
413+
} while (0)
408414
# else
409415
#define digitalToggleFast(P) \
410-
if (__builtin_constant_p(P)) { \
411-
BIT_SET(*__digitalPinToPINReg(P), __digitalPinToBit(P)); \
412-
} else { \
413-
digitalWrite(P, ! digitalRead(P)); \
414-
}
416+
do { \
417+
if (__builtin_constant_p(P)) { \
418+
BIT_SET(*__digitalPinToPINReg(P), __digitalPinToBit(P)); \
419+
} else { \
420+
digitalWrite(P, ! digitalRead(P)); \
421+
} \
422+
} while (0)
415423
# endif // defined(THROW_ERROR_IF_NOT_FAST)
416424
# else
417425
#define digitalToggleFast(P) digitalWrite(P, ! digitalRead(P))

0 commit comments

Comments
 (0)