Skip to content

Commit 0a84749

Browse files
committed
- DigInDebounce class for digital input debouncing implemented.
- Update to current VSCP framework trunk - Status lamp and init button connection fixed.
1 parent 8c76040 commit 0a84749

File tree

15 files changed

+386
-108
lines changed

15 files changed

+386
-108
lines changed

VSCP/examples/Generic/Generic.ino

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,8 @@ void setup() {
121121

122122
// Setup VSCP framework
123123
vscp.setup(
124-
13, // Status lamp pin
125-
12, // Init button pin
124+
8, // Status lamp pin
125+
7, // Init button pin
126126
nodeGuid, // Node GUID,
127127
255, // Node zone (255 = all zones)
128128
255, // Node sub-zone (255 = all sub-zones)
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#include <DigInDebounce.h>
2+
#include <SwTimer.h>
3+
#include <VSCP.h>
4+
5+
static int gStatusLampPin = 13;
6+
static int gInitButtonPin = 12;
7+
static unsigned int gTimeout = 250;
8+
static SwTimer gSwTimer;
9+
static DigInDebounce gInitButton(gInitButtonPin);
10+
static int gInitButtonState = HIGH;
11+
12+
void setup() {
13+
14+
Serial.begin(115200);
15+
Serial.println("Starts up ...");
16+
17+
pinMode(gStatusLampPin, OUTPUT);
18+
pinMode(gInitButtonPin, INPUT_PULLUP);
19+
gSwTimer.start(gTimeout);
20+
21+
}
22+
23+
void loop() {
24+
25+
gSwTimer.process();
26+
27+
if (true == gSwTimer.isTimeout()) {
28+
29+
int statusLampPinState = digitalRead(gStatusLampPin);
30+
31+
if (LOW == statusLampPinState)
32+
{
33+
statusLampPinState = HIGH;
34+
}
35+
else
36+
{
37+
statusLampPinState = LOW;
38+
}
39+
40+
digitalWrite(gStatusLampPin, statusLampPinState);
41+
}
42+
43+
if (LOW == gInitButton.read()) {
44+
if (HIGH == gInitButtonState) {
45+
gTimeout += 250;
46+
47+
if (1000 <= gTimeout) {
48+
gTimeout = 250;
49+
}
50+
gSwTimer.start(gTimeout);
51+
52+
Serial.println("Init button triggered.");
53+
54+
gInitButtonState = LOW;
55+
}
56+
}
57+
else {
58+
gInitButtonState = HIGH;
59+
}
60+
61+
}

VSCP/examples/Seeed-Studio-CAN_BUS_Shield/Seeed-Studio-CAN_BUS_Shield.ino

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,8 @@ void setup() {
157157

158158
// Setup VSCP framework
159159
vscp.setup(
160-
13, // Status lamp pin
161-
12, // Init button pin
160+
8, // Status lamp pin
161+
7, // Init button pin
162162
nodeGuid, // Node GUID,
163163
255, // Node zone (255 = all zones)
164164
255, // Node sub-zone (255 = all sub-zones)

VSCP/examples/Sparkfun_CAN-BUS_Shield/Sparkfun_CAN-BUS_Shield.ino

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,8 @@ void setup() {
169169

170170
// Setup VSCP framework
171171
vscp.setup(
172-
13, // Status lamp pin
173-
12, // Init button pin
172+
8, // Status lamp pin
173+
7, // Init button pin
174174
nodeGuid, // Node GUID,
175175
255, // Node zone (255 = all zones)
176176
255, // Node sub-zone (255 = all sub-zones)

VSCP/src/DigInDebounce.h

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
/* The MIT License (MIT)
2+
*
3+
* Copyright (c) 2014 - 2015, Andreas Merkle
4+
* http://www.blue-andi.de
5+
* vscp@blue-andi.de
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in all
15+
* copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
* SOFTWARE.
24+
*
25+
*/
26+
27+
/*******************************************************************************
28+
DESCRIPTION
29+
*******************************************************************************/
30+
/**
31+
@brief Digital input debounce class
32+
@file DigInDebounce.h
33+
@author Andreas Merkle, http://www.blue-andi.de
34+
35+
@section desc Description
36+
This module provides a class for debouncing ditial in.
37+
38+
*******************************************************************************/
39+
/** @defgroup DigInDebounce Digital input debounce class
40+
* This module provides a class for debouncing ditial in.
41+
* @{
42+
*/
43+
44+
/*
45+
* Don't forget to set JAVADOC_AUTOBRIEF to YES in the doxygen file to generate
46+
* a correct module description.
47+
*/
48+
49+
#ifndef __DIGINDEBOUNCE_H__
50+
#define __DIGINDEBOUNCE_H__
51+
52+
/*******************************************************************************
53+
INCLUDES
54+
*******************************************************************************/
55+
#if (100 <= ARDUINO)
56+
#include "Arduino.h"
57+
#else /* (100 > ARDUINO) */
58+
#include "WProgram.h"
59+
#endif /* (100 > ARDUINO) */
60+
61+
#ifdef __cplusplus
62+
extern "C"
63+
{
64+
#endif
65+
66+
/*******************************************************************************
67+
COMPILER SWITCHES
68+
*******************************************************************************/
69+
70+
/*******************************************************************************
71+
CONSTANTS
72+
*******************************************************************************/
73+
74+
/*******************************************************************************
75+
MACROS
76+
*******************************************************************************/
77+
78+
/*******************************************************************************
79+
CLASSES, TYPES AND STRUCTURES
80+
*******************************************************************************/
81+
82+
/**
83+
* Digital in debouncing
84+
*/
85+
class DigInDebounce
86+
{
87+
public:
88+
89+
/** Initialize and instantiate a digital in debouncer.
90+
*
91+
* @param[in] pin Digital input pin
92+
*/
93+
DigInDebounce(int pin = 0) :
94+
mPin(pin),
95+
mState(HIGH),
96+
mLastReadState(HIGH),
97+
mcDebounceDelay(50)
98+
{
99+
}
100+
101+
/** Destroy a digital in debouncer instance. */
102+
~DigInDebounce()
103+
{
104+
}
105+
106+
/**
107+
* Setup digital input pin.
108+
*/
109+
void setup(int pin)
110+
{
111+
mPin = pin;
112+
113+
mState = digitalRead(mPin);
114+
mLastReadState = mState;
115+
116+
return;
117+
}
118+
119+
/**
120+
* Returns the digital input state.
121+
*/
122+
int read(void)
123+
{
124+
int readState = digitalRead(mPin);
125+
126+
if (readState != mLastReadState)
127+
{
128+
mLastDebounceTime = millis();
129+
}
130+
else if ((millis() - mLastDebounceTime) > mcDebounceDelay)
131+
{
132+
if (readState != mState)
133+
{
134+
mState = readState;
135+
}
136+
}
137+
138+
mLastReadState = readState;
139+
140+
return mState;
141+
}
142+
143+
private:
144+
145+
int mPin; /**< Pin number */
146+
int mState; /**< Current digital in state */
147+
int mLastReadState; /**< Last read digital in state */
148+
unsigned long mLastDebounceTime; /**< Last time the digital in state changed */
149+
150+
const unsigned long mcDebounceDelay; /**< Debounce delay */
151+
};
152+
153+
/*******************************************************************************
154+
VARIABLES
155+
*******************************************************************************/
156+
157+
/*******************************************************************************
158+
FUNCTIONS
159+
*******************************************************************************/
160+
161+
#ifdef __cplusplus
162+
}
163+
#endif
164+
165+
#endif /* __DIGINDEBOUNCE_H__ */
166+
167+
/** @} */

VSCP/src/VSCP.cpp

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,6 @@
3535
@section desc Description
3636
@see VSCP.h
3737
38-
@section svn Subversion
39-
$Author: amerkle $
40-
$Rev: 449 $
41-
$Date: 2015-01-05 20:23:52 +0100 (Mo, 05 Jan 2015) $
4238
*******************************************************************************/
4339

4440
/*******************************************************************************
@@ -88,6 +84,8 @@ VSCP::VSCP() :
8884
mIsInitialized(false), /* Call setup() later */
8985
mStatusLampPin(0), /* Status Lamp pin */
9086
mInitButtonPin(0), /* Segment initialization button pin */
87+
mInitButton(), /* Default constructor init button debouncer */
88+
mLastInitButtonState(HIGH), /* Last init button state */
9189
mStatusLampState(VSCP_LAMP_STATE_OFF), /* Status lamp startup state is off */
9290
mStatusLampFastPeriod(250), /* 250 ms fast status lamp blinking period */
9391
mStatusLampSlowPeriod(1000), /* 1 s slow status lamp blinking period */
@@ -122,6 +120,7 @@ void VSCP::setup(
122120

123121
mStatusLampPin = statusLampPin;
124122
mInitButtonPin = initButtonPin;
123+
mInitButton.setup(mInitButtonPin);
125124

126125
/* Initialize the VSCP core and the user specific stuff. */
127126
(void)vscp_core_init();
@@ -181,9 +180,18 @@ void VSCP::process(void)
181180
processStatusLamp();
182181

183182
/* Handle segment initialization button */
184-
if (LOW == digitalRead(mInitButtonPin))
183+
if (LOW == mInitButton.read())
185184
{
186-
vscp_core_startNodeSegmentInit();
185+
if (HIGH == mLastInitButtonState)
186+
{
187+
vscp_core_startNodeSegmentInit();
188+
}
189+
190+
mLastInitButtonState = LOW;
191+
}
192+
else
193+
{
194+
mLastInitButtonState = HIGH;
187195
}
188196
}
189197

@@ -280,22 +288,28 @@ void VSCP::processStatusLamp(void)
280288
{
281289
mStatusLampState = statusLampState;
282290

291+
292+
283293
switch(mStatusLampState)
284294
{
285295
case VSCP_LAMP_STATE_OFF:
286296
digitalWrite(mStatusLampPin, LOW);
297+
Serial.println("Off");
287298
break;
288299

289300
case VSCP_LAMP_STATE_ON:
290301
digitalWrite(mStatusLampPin, HIGH);
302+
Serial.println("On");
291303
break;
292304

293305
case VSCP_LAMP_STATE_BLINK_SLOW:
294306
mStatusLampTimer.start(mStatusLampSlowPeriod, false);
307+
Serial.println("Slow");
295308
break;
296309

297310
case VSCP_LAMP_STATE_BLINK_FAST:
298311
mStatusLampTimer.start(mStatusLampFastPeriod, false);
312+
Serial.println("Fast");
299313
break;
300314

301315
default:

VSCP/src/VSCP.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,6 @@
3535
@section desc Description
3636
This module provides a C++ wrapper for the VSCP framework.
3737
38-
@section svn Subversion
39-
$Author: amerkle $
40-
$Rev: 449 $
41-
$Date: 2015-01-05 20:23:52 +0100 (Mo, 05 Jan 2015) $
4238
*******************************************************************************/
4339
/** @defgroup vscpFramework VSCP framework class
4440
* C++ wrapper for the VSCP framework.
@@ -62,6 +58,7 @@ This module provides a C++ wrapper for the VSCP framework.
6258
#include "WProgram.h"
6359
#endif /* (100 > ARDUINO) */
6460

61+
#include "DigInDebounce.h"
6562
#include "SwTimer.h"
6663
#include "framework/vscp_platform.h"
6764
#include "framework/vscp_types.h"
@@ -72,6 +69,7 @@ This module provides a C++ wrapper for the VSCP framework.
7269
#include "framework/vscp_type_control.h"
7370
#include "framework/vscp_type_display.h"
7471
#include "framework/vscp_type_information.h"
72+
#include "framework/vscp_type_log.h"
7573
#include "framework/vscp_type_measurement.h"
7674
#include "framework/vscp_type_measurezone.h"
7775
#include "framework/vscp_type_phone.h"
@@ -249,6 +247,8 @@ class VSCP
249247

250248
int mStatusLampPin; /**< Status lamp pin */
251249
int mInitButtonPin; /**< Segment initialization button pin */
250+
DigInDebounce mInitButton; /**< Debounced init button state */
251+
int mLastInitButtonState; /**< Last init button state */
252252

253253
VSCP_LAMP_STATE mStatusLampState; /**< Current state of the status lamp */
254254
const unsigned int mStatusLampFastPeriod; /**< Fast status lamp blinking periode in ms */

VSCP/src/framework/vscp_config.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ extern "C"
202202
#ifndef VSCP_CONFIG_NODE_SEGMENT_INIT_TIMEOUT
203203

204204
/** Timeout in ms for the node segment initialization.
205-
* See VSCP v1.10.15, chapter VSCP Level I Specifics, Node segment initialization. Dynamic nodes, Step 2
205+
* See VSCP specification, chapter VSCP Level I Specifics, Node segment initialization. Dynamic nodes, Step 2
206206
*/
207207
#define VSCP_CONFIG_NODE_SEGMENT_INIT_TIMEOUT ((uint16_t)5000)
208208

0 commit comments

Comments
 (0)