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 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| //
|
[MIT] |
|
| // OOSMOS switchtest Class
|
|
| //
|
|
| // Copyright (c) 2014-2026 OOSMOS, LLC
|
|
| // SPDX-License-Identifier: MIT
|
|
| //
|
|
| |
|
| #ifndef switchtest_h
|
|
| #define switchtest_h
|
|
| |
|
| #include "pin.h"
|
|
| |
|
| typedef struct switchtestTag switchtest;
|
|
| |
|
| extern switchtest * switchtestNew(pin * pPin);
|
|
| |
|
| #endif
|
|
| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 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 |
|
| //
|
[MIT] |
|
| // OOSMOS switchtest Class
|
|
| //
|
|
| // Copyright (c) 2014-2026 OOSMOS, LLC
|
|
| // SPDX-License-Identifier: MIT
|
|
| //
|
|
| |
|
| #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;
|
|
| }
|
|
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 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| //
|
[MIT] |
|
| // OOSMOS - The Object-Oriented State Machine Operating System
|
|
| //
|
|
| // Copyright (c) 2014-2026 OOSMOS, LLC
|
|
| // SPDX-License-Identifier: MIT
|
|
| //
|
|
| |
|
| #include "oosmos.h"
|
#includes... |
|
| #include "pin.h"
|
|
| #include "prt.h"
|
|
| #include "switchtest.h"
|
|
| |
|
| // 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