Skip to content

Commit 601b781

Browse files
committed
Cleanup
1 parent 0c9ce48 commit 601b781

File tree

9 files changed

+89
-52
lines changed

9 files changed

+89
-52
lines changed

src/Button.h

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,17 @@
2222
#include "GPIO.h"
2323

2424
/**
25-
* Debounced input pin template class using GPIO. Internal pullup
25+
* Debounced Input Pin template class using GPIO. The internal pullup
2626
* resistor is used. The button/switch should be connected to ground.
2727
* @param[in] PIN board pin for input signal.
2828
*/
2929
template<BOARD::pin_t PIN>
3030
class Button {
3131
public:
3232
/**
33-
* Construct debounced input pin (Button) instance with given
34-
* template parameters. Initiate GPIO pins input mode with pullup
35-
* resistor.
36-
* @param[in] ms debouce time (Default 50 ms).
33+
* Construct debounced input pin instance with given template
34+
* parameters. Initiate GPIO pins input mode with pullup resistor.
35+
* @param[in] ms debounce time limit (Default 50 ms).
3736
*/
3837
Button(uint16_t ms = 50) :
3938
DEBOUNCE(ms),
@@ -44,18 +43,18 @@ class Button {
4443
}
4544

4645
/**
47-
* Return true(1) if button change was detected, otherwise false(0).
48-
* Rising or falling edge is determined by reading the debounced pin
49-
* state.
50-
* @return true(1) if button change was detected, otherwise false(0).
46+
* Return true(1) if a button state change was detected, otherwise
47+
* false(0). Rising or falling edge is determined by reading the
48+
* debounced pin state.
49+
* @return bool.
5150
*/
5251
bool ischanged()
5352
{
54-
// Check debounce time has elapsed
53+
// Check if debounce time limit has elapsed
5554
if (millis() - m_timestamp < DEBOUNCE) return (false);
5655
m_timestamp = millis();
5756

58-
// Check for pin state change
57+
// Check for the pin state has changed
5958
bool state = m_pin;
6059
if (state == m_state) return (false);
6160
m_state = state;
@@ -100,7 +99,7 @@ class Button {
10099
/** Timestamp for latest pin read. */
101100
uint16_t m_timestamp;
102101

103-
/** Latest pin read. */
102+
/** Latest pin read; debounced pin state. */
104103
bool m_state;
105104
};
106105

src/Hardware/AVR/Board.h

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* @file Board.h
3-
* @version 1.5
3+
* @version 1.6
44
*
55
* @section License
66
* Copyright (C) 2017, Mikael Patel
@@ -33,15 +33,35 @@
3333
*/
3434
#define GPIO_REG(pin) ((pin) >> 4)
3535

36-
/** Maximum port control register address for atomic bit instructions. */
37-
#define GPIO_ATOMIC_MAX GPIO_PIN(0x40,0)
38-
3936
/**
4037
* Return pin mask from board pin value.
4138
* @return pin mask
4239
*/
4340
#define GPIO_MASK(pin) _BV((pin) & 0xf)
4441

42+
/** Maximum port control register address for atomic bit instructions. */
43+
#define GPIO_ATOMIC_MAX GPIO_PIN(0x40,0)
44+
45+
/**
46+
* Forces given expression to be atomic. Higher port addresses
47+
* cannot be accessed with a single instruction and require
48+
* disabling of interrupts to become atomic.
49+
* @param[in] expr expression to be atomic.
50+
*/
51+
#define GPIO_ATOMIC(expr) \
52+
do { \
53+
if (PIN < GPIO_ATOMIC_MAX) { \
54+
expr; \
55+
} \
56+
else { \
57+
uint8_t sreg = SREG; \
58+
__asm__ __volatile__("cli" ::: "memory"); \
59+
expr; \
60+
SREG = sreg; \
61+
__asm__ __volatile__("" ::: "memory"); \
62+
} \
63+
} while (0)
64+
4565
#if defined(__AVR_ATmega168__) || defined(__AVR_ATmega328P__)
4666
/**
4767
* GPIO digital pin symbols for ATmega168/ATmega328P based boards.

src/Hardware/AVR/GPIO.h

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* @file Hardware/AVR/GPIO.h
3-
* @version 1.5
3+
* @version 1.6
44
*
55
* @section License
66
* Copyright (C) 2017, Mikael Patel
@@ -21,30 +21,10 @@
2121

2222
#include "Hardware/AVR/Board.h"
2323

24-
/**
25-
* Forces given expression to be atomic. Higher port addresses
26-
* cannot be accessed with a single instruction and require
27-
* disabling of interrupts to become atomic.
28-
* @param[in] expr expression to be atomic.
29-
*/
30-
#define GPIO_ATOMIC(expr) \
31-
do { \
32-
if (PIN < GPIO_ATOMIC_MAX) { \
33-
expr; \
34-
} \
35-
else { \
36-
uint8_t sreg = SREG; \
37-
__asm__ __volatile__("cli" ::: "memory"); \
38-
expr; \
39-
SREG = sreg; \
40-
__asm__ __volatile__("" ::: "memory"); \
41-
} \
42-
} while (0)
43-
4424
/**
4525
* General Purpose Digital I/O pin template class. Highly optimized
46-
* pin access. The PIN address is bit pointer to the port control
47-
* register and pin bit position. See Hardware/AVR/Board.h for details.
26+
* pin access. The PIN address is a bit pointer to the port control
27+
* register. See Hardware/AVR/Board.h for details.
4828
* @param[in] PIN board pin definition.
4929
*/
5030
template<BOARD::pin_t PIN>

src/Hardware/SAM/Board.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
#define HARDWARE_SAM_BOARD_H
2121

2222
/**
23-
* Board pin values are bit-pointers and constructed from port control
24-
* register index and pin bit position.
23+
* Board pin values are constructed from port control register index
24+
* and pin bit position.
2525
* @param[in] index control register index.
2626
* @param[in] pin pin bit position in control register.
2727
*/

src/Hardware/SAM/GPIO.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
/**
2525
* General Purpose Digital I/O pin template class. Highly optimized
26-
* pin access. The PIN address is the port control register index and
26+
* pin access. The PIN address is a port control register index and
2727
* pin bit position. See Hardware/SAM/Board.h for details.
2828
* @param[in] PIN board pin definition.
2929
*/

src/SRPI.h

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,16 @@ class SRPI {
4545
* Return deserialized value according to the template bit order
4646
* parameter. Generates a positive clock pulse for each bit
4747
* transfer.
48-
* @param[out] value from data input signal.
48+
* @return read byte.
4949
*/
50-
void operator>>(uint8_t& value)
50+
uint8_t read()
5151
{
52+
uint8_t res = 0;
5253
if (BITORDER == LSBFIRST) {
5354
uint8_t mask = 1;
5455
do {
5556
m_clock.toggle();
56-
value |= (m_data ? mask : 0);
57+
res |= (m_data ? mask : 0);
5758
m_clock.toggle();
5859
mask <<= 1;
5960
} while (mask);
@@ -62,11 +63,21 @@ class SRPI {
6263
uint8_t mask = 0x80;
6364
do {
6465
m_clock.toggle();
65-
value |= (m_data ? mask : 0);
66+
res |= (m_data ? mask : 0);
6667
m_clock.toggle();
6768
mask >>= 1;
6869
} while (mask);
6970
}
71+
return (res);
72+
}
73+
74+
/**
75+
* Serial input operator. Shorthand for read().
76+
* @param[out] value from data input signal.
77+
*/
78+
void operator>>(uint8_t& value)
79+
{
80+
value = read();
7081
}
7182

7283
protected:

src/SRPIO.h

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class SRPIO {
4747
* parameter. Generate a positive clock pulse for each bit transfer.
4848
* @param[in] value to data output signal.
4949
*/
50-
void operator<<(uint8_t value)
50+
void write(uint8_t value)
5151
{
5252
if (BITORDER == LSBFIRST) {
5353
uint8_t mask = 1;
@@ -70,13 +70,22 @@ class SRPIO {
7070
m_data.input();
7171
}
7272

73+
/**
74+
* Serial output operator. Shorthand for write().
75+
* @param[in] value to data output signal.
76+
*/
77+
void operator<<(uint8_t value)
78+
{
79+
write(value);
80+
}
81+
7382
/**
7483
* Return deserialized value according to the template bit order
7584
* parameter. Generates a positive clock pulse for each bit
7685
* transfer.
77-
* @param[out] value from data input signal.
86+
* @return byte read from data input signal.
7887
*/
79-
void operator>>(uint8_t& value)
88+
uint8_t read()
8089
{
8190
if (BITORDER == LSBFIRST) {
8291
uint8_t mask = 1;
@@ -98,6 +107,15 @@ class SRPIO {
98107
}
99108
}
100109

110+
/**
111+
* Serial input operator. Shorthand for read().
112+
* @param[out] value from data input signal.
113+
*/
114+
void operator>>(uint8_t& value)
115+
{
116+
value = read();
117+
}
118+
101119
protected:
102120
GPIO<DATA_PIN> m_data;
103121
GPIO<CLOCK_PIN> m_clock;

src/SRPO.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323

2424
/**
2525
* Shift Register Parallel Output (SRPO) template class using GPIO.
26+
* @param[in] BITORDER LSBFIRST or MSBFIRST.
2627
* @param[in] DATA_PIN board pin for data output signal.
2728
* @param[in] CLOCK_PIN board pin for clock output signal.
28-
* @param[in] BIT_ORDER LSBFIRST or MSBFIRST.
2929
*/
3030
template<uint8_t BITORDER, BOARD::pin_t DATA_PIN, BOARD::pin_t CLOCK_PIN>
3131
class SRPO {
@@ -46,7 +46,7 @@ class SRPO {
4646
* parameter. Generate a positive clock pulse for each bit transfer.
4747
* @param[in] value to data output signal.
4848
*/
49-
void operator<<(uint8_t value)
49+
void write(uint8_t value)
5050
{
5151
if (BITORDER == LSBFIRST) {
5252
uint8_t mask = 1;
@@ -68,6 +68,15 @@ class SRPO {
6868
}
6969
}
7070

71+
/**
72+
* Serial output operator. Shorthand for write().
73+
* @param[in] value to data output signal.
74+
*/
75+
void operator<<(uint8_t value)
76+
{
77+
write(value);
78+
}
79+
7180
protected:
7281
GPIO<DATA_PIN> m_data;
7382
GPIO<CLOCK_PIN> m_clock;

src/Software/Serial.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class Serial : public ::Stream {
3232
public:
3333
/**
3434
* Construct Output only Software Serial instance with
35-
* given template parameters. Initiate GPIO transit pin
35+
* given template parameters. Initiate GPIO transmit pin
3636
* to output mode and set default baudrate (57600 bps).
3737
*/
3838
Serial() :

0 commit comments

Comments
 (0)