OOSMOS Switch Example
1. Introduction
2. Switch on Various Platforms

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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
[GPLv2]
 
#include "oosmos.h"
#include "pin.h"
#include "sw.h"
#include "switchtest.h"
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
 
#ifndef MAX_SWITCHTESTS
#define MAX_SWITCHTESTS 2
#endif
 
//>>>EVENTS
enum {
evClosed = 1,
evOpen = 2
};
 
#ifdef oosmos_DEBUG
static const char * OOSMOS_EventNames(int EventCode)
{
switch (EventCode) {
case evClosed: return "evClosed";
case evOpen: return "evOpen";
default: return "";
}
}
#endif
//<<<EVENTS
 
typedef union {
oosmos_sEvent Base;
} uEvents;
 
struct switchtestTag
{
//>>>DECL
oosmos_sStateMachine(ROOT, uEvents, 3);
oosmos_sLeaf Idle_State;
//<<<DECL
};
 
//>>>CODE
static bool Idle_State_Code(void * pObject, oosmos_sState * pState, const oosmos_sEvent * pEvent)
{
switch (oosmos_EventCode(pEvent)) {
case evOpen: {
printf("Open\n");
return true;
}
case evClosed: {
printf("Close\n");
return true;
}
}
 
oosmos_UNUSED(pObject);
oosmos_UNUSED(pState);
return false;
}
//<<<CODE
 
extern switchtest * switchtestNew(pin * pPin)
{
oosmos_Allocate(pSwitchTest, switchtest, MAX_SWITCHTESTS, NULL);
 
//>>>INIT
oosmos_StateMachineInit(pSwitchTest, ROOT, NULL, Idle_State);
oosmos_LeafInit(pSwitchTest, Idle_State, ROOT, Idle_State_Code);
 
oosmos_Debug(pSwitchTest, OOSMOS_EventNames);
//<<<INIT
 
sw * pSwitch = swNew(pPin);
 
swSubscribeOpenEvent(pSwitch, oosmos_EventQueue(pSwitchTest), evOpen, NULL);
swSubscribeCloseEvent(pSwitch, oosmos_EventQueue(pSwitchTest), evClosed, 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-2024  OOSMOS, LLC