OOSMOS Switch Example
1 Introduction
2 Switch on Various Platforms
2.1 Switch on Arduino
1 Introduction
2 Switch on Various Plat...
2.1 Switch on Arduino
1 Introduction
2 Switch on Various Pl...
2.1 Switch on Arduino
1 Introduction
2 Switch on Various ...
2.1 Switch on Arduino

1 Introduction

This example demonstrates:
  • Reusable class sw used by another reusable class, switchtest.
  • Publish/Subscribe events.
All platforms can use the portable class switchtest. Source code below.
1
22
23
24
25
26
27
28
29
30
31
32
[GPLv2]
 
#ifndef switchtest_h
#define switchtest_h
 
#include "pin.h"
 
typedef struct switchtestTag switchtest;
 
extern switchtest * switchtestNew(pin * pPin);
 
#endif
switchtest.h
1
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
[GPLv2]
 
#include "oosmos.h"
#include "pin.h"
#include "sw.h"
#include "prt.h"
#include "switchtest.h"
 
#ifndef MAX_SWITCHTESTS
#define MAX_SWITCHTESTS 2
#endif
 
typedef enum {
OpenEvent = 1,
ClosedEvent,
} eEvents;
 
struct switchtestTag
{
oosmos_sStateMachine(StateMachine, oosmos_sEvent, 2);
oosmos_sLeaf Idle_State;
};
 
static bool Idle_State_Code(void * pObject, oosmos_sRegion * pRegion, const oosmos_sEvent * pEvent)
{
switchtest * pSwitchTest = (switchtest *) pObject;
 
switch (pEvent->Code) {
case OpenEvent:
return prtFormatted("Switch %p opened\n", pSwitchTest);
case ClosedEvent:
return prtFormatted("Switch %p closed\n", pSwitchTest);
}
 
return false;
}
 
extern switchtest * switchtestNew(pin * pPin)
{
oosmos_Allocate(pSwitchTest, switchtest, MAX_SWITCHTESTS, NULL);
 
// StateName Parent Default
// =====================================================
oosmos_StateMachineInit(pSwitchTest, StateMachine, NULL, Idle_State);
oosmos_LeafInit (pSwitchTest, Idle_State, StateMachine );
 
sw * pSwitch = swNew(pPin);
 
swSubscribeOpenEvent(pSwitch, &pSwitchTest->EventQueue, OpenEvent, NULL);
swSubscribeCloseEvent(pSwitch, &pSwitchTest->EventQueue, ClosedEvent, NULL);
 
return pSwitchTest;
}
switchtest.c

2 Switch on Various Platforms

We show the switch example on Arduino. It should be a simple matter to use any other board with GPIO pins. See the Blink example for wiring and pin configurations on other boards.

2.1 Switch on Arduino

The Arduino is a breeze to wire up. The pin assignments are printed on the Arduino circuit board and those are the numbers used to address them in the code.
Figure 1. Arduino Wiring Diagram
This is the main program for the Arduino.
1
22
23
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
[GPLv2]
 
#includes...
 
// Required by prt for Arduino...
unsigned long prtArduinoBaudRate = 115200;
 
static void CreateSwitchTest(const int PinNumber)
{
pin * pPin = pinNew_Debounce(PinNumber, pinIn, pinActiveLow, 50);
switchtestNew(pPin);
}
 
extern void setup()
{
CreateSwitchTest(3);
CreateSwitchTest(2);
}
 
extern void loop()
{
oosmos_RunStateMachines();
}
SwitchExample.ino - Switch for Arduino
Copyright © 2014-2016  OOSMOS, LLC