Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
V
VizBlocksFramework
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Container Registry
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Design Informatics
VizBlocks
VizBlocksFramework
Commits
8304c322
Commit
8304c322
authored
4 years ago
by
Joe Revans
Browse files
Options
Downloads
Patches
Plain Diff
3 button code
parent
1deb672d
No related branches found
No related tags found
3 merge requests
!4
Joe merge
,
!2
Docs
,
!1
Pactman/VizBlocks Merge
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
examples/ThreeButtonNodePactman/ThreeButtonNodePactman.ino
+178
-0
178 additions, 0 deletions
examples/ThreeButtonNodePactman/ThreeButtonNodePactman.ino
with
178 additions
and
0 deletions
examples/ThreeButtonNodePactman/ThreeButtonNodePactman.ino
0 → 100644
+
178
−
0
View file @
8304c322
#include
"NameDictionary.h"
#include
"Button.h"
#include
"Potentiometer.h"
#include
"RotaryEncoder.h"
#include
<VizBlocks.h>
#include
<Behaviours.h>
//Get device name
const
String
device_id
=
String
(
ESP
.
getChipId
(),
HEX
);
NameDictionary
d
;
const
String
name
=
d
.
get
(
device_id
);
char
_name
[
10
];
//Init as a VizBlock node with "null" ID.
VizBlocks
node
(
"null"
,
// Our ID
"NodeRedServer"
,
//Wifi Access Point
"NodeR3dAP"
,
//WiFi Password
"192.168.4.1"
,
//IP address of Node RED server
1883
//Port for Node RED server
);
//Init button
const
int
b0Pin
=
2
;
const
int
b1Pin
=
3
;
// change this to correct pin number
const
int
b2Pin
=
4
;
// change this to correct pin number
Button
b0
(
b0Pin
,
0
);
Button
b1
(
b1Pin
,
1
);
Button
b2
(
b2Pin
,
2
);
//Button Interrupt Service Routine
void
ICACHE_RAM_ATTR
b0ISR
()
{
b0
.
tick
();
}
void
ICACHE_RAM_ATTR
b1ISR
()
{
b1
.
tick
();
}
void
ICACHE_RAM_ATTR
b2ISR
()
{
b2
.
tick
();
}
unsigned
long
t
=
0
;
const
int
pingInterval
=
4000
;
void
setup
()
{
//Get the serial port ready
Serial
.
begin
(
115200
);
Serial
.
println
(
"Serial started!"
);
delay
(
500
);
//Pass event handlers into button
b0
.
initInterrupts
(
b0ISR
);
b1
.
initInterrupts
(
b1ISR
);
b2
.
initInterrupts
(
b2ISR
);
b0
.
setEventHandler
(
bCB
);
b1
.
setEventHandler
(
bCB
);
b2
.
setEventHandler
(
bCB
);
//Print device name
if
(
name
==
device_id
)
{
Serial
.
println
(
"!!! This device doesn't have a name yet. Let's call it: "
+
name
);
}
else
{
Serial
.
println
(
"Device name: "
+
name
);
}
name
.
toCharArray
(
_name
,
10
);
node
.
setID
(
_name
);
//Add in the custom behaviour we defined earlier.
node
.
add
(
new
SendCapabilities
(
&
node
)
);
node
.
add
(
new
PingServer
(
&
node
)
);
node
.
add
(
new
Link
(
&
node
)
);
node
.
add
(
new
ButtonPressed
(
&
node
)
);
node
.
add
(
new
ButtonReleased
(
&
node
)
);
node
.
add
(
new
ButtonClicked
(
&
node
)
);
node
.
add
(
new
ButtonHeld
(
&
node
)
);
node
.
add
(
new
ButtonTick
(
&
node
)
);
node
.
add
(
new
PotentiometerUpdated
(
&
node
)
);
node
.
add
(
new
RotaryEncoderUpdated
(
&
node
)
);
//Initialise the whole infrastructure
node
.
set_wifi
(
true
);
node
.
init
();
delay
(
500
);
node
.
process
(
"PingServer"
);
}
void
loop
()
{
node
.
run
();
b0
.
check
();
b1
.
check
();
b2
.
check
();
pingComms
(
pingInterval
);
}
void
pingComms
(
int
interval
)
{
unsigned
long
timeNow
=
millis
();
if
(
timeNow
>
t
+
interval
)
{
t
=
timeNow
;
Serial
.
println
(
"Link "
+
name
);
}
}
void
bCB
(
Button
*
button
,
uint8_t
eventType
,
bool
state
)
{
/*
* Button event handler that triggers VizBlocks behaviours
*/
String
idString
=
String
(
button
->
getId
());
Serial
.
println
(
"Button ID: "
+
idString
+
" Event Type: "
+
String
(
eventType
)
+
" State: "
+
String
(
state
));
switch
(
eventType
)
{
case
Button
::
kEventPressed
:
//Do something
break
;
case
Button
::
kEventReleased
:
//Do something else
node
.
input_event
(
"ButtonReleased "
+
idString
);
break
;
case
Button
::
kEventClicked
:
//Do something else
node
.
input_event
(
"ButtonClicked "
+
idString
);
break
;
case
Button
::
kEventHeld
:
//Do something else
node
.
input_event
(
"ButtonHeld "
+
idString
);
break
;
case
Button
::
kEventTick
:
//Do something else
node
.
input_event
(
"ButtonTick "
+
idString
);
break
;
}
}
void
pCB
(
Potentiometer
*
potentiometer
,
uint8_t
eventType
,
uint8_t
sensorValue
)
{
/*
* Potentiometer Event Handler that triggers VizBlocks behaviours
*/
String
idString
=
String
(
potentiometer
->
getId
());
Serial
.
println
(
"Slider ID: "
+
idString
+
" Event Type: "
+
String
(
eventType
)
+
" Sensor Value: "
+
String
(
sensorValue
));
switch
(
eventType
)
{
case
Potentiometer
::
kEventStableUpdate
:
//Do something
node
.
input_event
(
"PotentiometerUpdated "
+
idString
+
" "
+
String
(
sensorValue
));
break
;
case
Potentiometer
::
kEventUnstableUpdate
:
//Do something else
break
;
}
}
void
reCB
(
RotaryEncoder
*
rotaryEncoder
,
uint8_t
eventType
,
int
position
)
{
/*
* Rotary Encoder event handler that triggers VizBlocks behaviours
*/
String
idString
=
String
(
rotaryEncoder
->
getId
());
Serial
.
println
(
"Encoder ID: "
+
idString
+
" Event Type: "
+
String
(
eventType
)
+
" Position: "
+
String
(
position
));
switch
(
eventType
)
{
case
RotaryEncoder
::
kEventStableUpdate
:
//Do something
node
.
input_event
(
"RotaryEncoderUpdated "
+
idString
+
" "
+
String
(
position
));
break
;
case
RotaryEncoder
::
kEventUnstableUpdate
:
//Do something else
break
;
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment