Working transmission

master
Richard Hopkins 2020-05-22 22:09:52 +01:00
parent ce066c2a38
commit 5df11d3c3d
2 changed files with 39 additions and 46 deletions

17
.vscode/launch.json vendored Normal file
View File

@ -0,0 +1,17 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"skipFiles": [
"<node_internals>/**"
],
"program": "${workspaceFolder}/http-server"
}
]
}

View File

@ -1,21 +1,15 @@
/* /*
========================================================== ==========================================================
Simple event based robot controller that enables robot Simple event based robot controller that enables robot
to switch into automatic or manual control modes. Behaviours to switch into automatic or manual control modes. Behaviours
are controlled via a simple finite state machine. are controlled via a simple finite state machine.
In automatic mode the In automatic mode the
robot will look after itself. In manual mode, the watch robot will look after itself. In manual mode, the watch
will provide simple forward, back, left and right commands. will provide simple forward, back, left and right commands.
The messages will be transmitted to a partner BLE Espruino The messages will be transmitted to a partner BLE Espruino
using BLE using BLE
Written by Richard Hopkins, May 2020 Written by Richard Hopkins, May 2020
========================================================== ==========================================================
declare global variables for watch button statuses */ declare global variables for watch button statuses */
top_btn = false; top_btn = false;
middle_btn = false; middle_btn = false;
@ -23,10 +17,10 @@ left_btn= false; // the left side of the touch screen
right_btn = false; // the right side of the touch screen right_btn = false; // the right side of the touch screen
bottom_btn = false; bottom_btn = false;
msgNum = 0; // message number
/* /*
CONFIGURATION AREA - STATE VARIABLES CONFIGURATION AREA - STATE VARIABLES
declare global variables for the toggle button declare global variables for the toggle button
statuses; if you add an additional toggle button statuses; if you add an additional toggle button
you should declare it and initiase it here */ you should declare it and initiase it here */
@ -35,24 +29,32 @@ var status_auto = {value: false};
var status_mic = {value: true}; var status_mic = {value: true};
var status_spk = {value: true}; var status_spk = {value: true};
/* trsnsmit message */ /* trsnsmit message
where
s = first character of state,
o = first three character of object name
v = value of state.object
*/
const transmit = (state,object,status) => { const transmit = (state,object,status) => {
message = { msgNum ++;
state: state, msg = {
obj: object, n: msgNum.toString().slice(-4),
val: status, s: state.substr(0,4),
o: object.substr(0,4),
v: status.substr(0.4),
}; };
print(message); message= msg.n + "," + msg.s + "," + msg.o + "," + msg.v;
return JSON.stringify(message); NRF.setAdvertising({},{
showName: false,
manufacturer: 0x0590,
manufacturerData: JSON.stringify(message)});
}; };
/* /*
CONFIGURATION AREA - ICON DEFINITIONS CONFIGURATION AREA - ICON DEFINITIONS
Retrieve 30px PNG icons from: Retrieve 30px PNG icons from:
https://icons8.com/icon/set/speak/ios-glyphs https://icons8.com/icon/set/speak/ios-glyphs
Create icons using: Create icons using:
https://www.espruino.com/Image+Converter https://www.espruino.com/Image+Converter
Use compression: true Use compression: true
@ -60,7 +62,6 @@ Transparency: true
Diffusion: flat Diffusion: flat
Colours: 16bit RGB Colours: 16bit RGB
Ouput as: Image Object Ouput as: Image Object
Add an additional element to the icons array Add an additional element to the icons array
with a unique name and the data from the Image Object with a unique name and the data from the Image Object
*/ */
@ -133,20 +134,16 @@ const drawIcon = (name) => {
}; };
/* /*
CONFIGURATION AREA - BUTTON DEFINITIONS CONFIGURATION AREA - BUTTON DEFINITIONS
for a simple button, just define a primary colour for a simple button, just define a primary colour
and an icon name from the icon array and and an icon name from the icon array and
the text to display beneath the button the text to display beneath the button
for toggle buttons, additionally provide secondary for toggle buttons, additionally provide secondary
colours, icon name and text. Also provide a reference colours, icon name and text. Also provide a reference
to a global variable for the value of the button. to a global variable for the value of the button.
The global variable should be declared at the start of The global variable should be declared at the start of
the program and it may be adviable to use the 'status_name' the program and it may be adviable to use the 'status_name'
format to ensure it is clear. format to ensure it is clear.
*/ */
var joystickBtn = { var joystickBtn = {
primary_colour: 0x653E, primary_colour: 0x653E,
@ -200,18 +197,14 @@ var spkBtn = {
}; };
/* /*
CONFIGURATION AREA - SCREEN DEFINITIONS CONFIGURATION AREA - SCREEN DEFINITIONS
a screen can have a button (as defined above) a screen can have a button (as defined above)
on the left and/or the right of the screen. on the left and/or the right of the screen.
in adddition a screen can optionally have in adddition a screen can optionally have
an icon for each of the three buttons on an icon for each of the three buttons on
the left hand side of the screen. These the left hand side of the screen. These
are defined as btn1, bt2 and bt3. The are defined as btn1, bt2 and bt3. The
values are names from the icon array. values are names from the icon array.
*/ */
const menuScreen = { const menuScreen = {
left: autoBtn, left: autoBtn,
@ -234,11 +227,9 @@ const commsScreen = {
}; };
/* base state definition /* base state definition
Each of the screens correspond to a state; Each of the screens correspond to a state;
this class provides a constuctor for each this class provides a constuctor for each
of the states of the states
*/ */
class State { class State {
constructor(params) { constructor(params) {
@ -249,39 +240,28 @@ class State {
} }
/* /*
CONFIGURATION AREA - BUTTON BEHAVIOURS/STATE TRANSITIONS CONFIGURATION AREA - BUTTON BEHAVIOURS/STATE TRANSITIONS
This area defines how each screen behaves. This area defines how each screen behaves.
Each screen corresponds to a different State of the Each screen corresponds to a different State of the
state machine. This makes it much easier to isolate state machine. This makes it much easier to isolate
behaviours between screens. behaviours between screens.
The state value is transmitted whenever a button is pressed The state value is transmitted whenever a button is pressed
to provide context (so the receiving device, knows which to provide context (so the receiving device, knows which
button was pressed on which screen). button was pressed on which screen).
The screens are defined above. The screens are defined above.
The events section identifies if a particular button has been The events section identifies if a particular button has been
pressed and released on the screen and an action can then be taken. pressed and released on the screen and an action can then be taken.
The events function receives a notification from a mySetWatch which The events function receives a notification from a mySetWatch which
provides an event object that identifies which button and whether provides an event object that identifies which button and whether
it has been pressed down or released. Actions can then be taken. it has been pressed down or released. Actions can then be taken.
The events function will always return a State object. The events function will always return a State object.
If the events function returns different State from the current If the events function returns different State from the current
one, then the state machine will change to that new State and redrsw one, then the state machine will change to that new State and redrsw
the screen appropriately. the screen appropriately.
To add in additional capabilities for button presses, simply add To add in additional capabilities for button presses, simply add
an additional 'if' statement. an additional 'if' statement.
For toggle buttons, the value of the sppropiate status object is For toggle buttons, the value of the sppropiate status object is
inversed and the new value transmitted. inversed and the new value transmitted.
*/ */
/* The Home State/Page is where the application beings */ /* The Home State/Page is where the application beings */
@ -352,19 +332,15 @@ const onOff= status => status ? "on" : "off";
/* create watching functions that will change the global /* create watching functions that will change the global
button status when pressed or released button status when pressed or released
This is actuslly the hesrt of the program. When a button This is actuslly the hesrt of the program. When a button
is not being pressed, nothing is happening (no loops). is not being pressed, nothing is happening (no loops).
This makes the progrsm more battery efficient. This makes the progrsm more battery efficient.
When a setWatch event is raised, the custom callbacks defined When a setWatch event is raised, the custom callbacks defined
here will be called. These then fired as events to the current here will be called. These then fired as events to the current
state/screen of the state mschine. state/screen of the state mschine.
Some events, will result in the stste of the state machine Some events, will result in the stste of the state machine
chsnging, which is why the screen is redrswn after each chsnging, which is why the screen is redrswn after each
button press. button press.
*/ */
const setMyWatch = (params) => { const setMyWatch = (params) => {
setWatch(() => { setWatch(() => {
@ -378,7 +354,7 @@ const setMyWatch = (params) => {
*/ */
const buttons = [ const buttons = [
{bool : bottom_btn, label : "bottom",btn : BTN3}, {bool : bottom_btn, label : "bottom",btn : BTN3},
{bool : middle_btn, label : "mdiddle",btn : BTN2}, {bool : middle_btn, label : "middle",btn : BTN2},
{bool : top_btn, label : "top",btn : BTN1}, {bool : top_btn, label : "top",btn : BTN1},
{bool : left_btn, label : "left",btn : BTN4}, {bool : left_btn, label : "left",btn : BTN4},
{bool : right_btn, label : "right",btn : BTN5} {bool : right_btn, label : "right",btn : BTN5}
@ -421,4 +397,4 @@ const drawScreen = (params) => {
machine = Home; // instantiate the state machine at Home machine = Home; // instantiate the state machine at Home
Bangle.drawWidgets(); // draw active widgets Bangle.drawWidgets(); // draw active widgets
drawScreen(machine.screen); // draw the screen drawScreen(machine.screen); // draw the screen