Switch Matrix Example
1 Introduction
1.1 matrixtest Statechart
2 Matrix on Various Platforms
2.1 Matrix on Arduino
2.2 Matrix on PIC32 Starter Kit
1 Introduction
1.1 matrixtest Statechart
2 Matrix on Various Plat...
2.1 Matrix on Arduino
2.2 Matrix on PIC32 Starte...
1 Introduction
1.1 matrixtest Statechar...
2 Matrix on Various Pl...
2.1 Matrix on Arduino
2.2 Matrix on PIC32 Star...
1 Introduction
1.1 matrixtest Statech...
2 Matrix on Various ...
2.1 Matrix on Arduino
2.2 Matrix on PIC32 St...

1 Introduction

This is an example of how to use the sw and matrix classes to react to key presses and releases from a multi-row, multi-column keypad.
It uses the reusable class matrixtest in the Classes/Tests directory to demonstrate the use of the OOSMOS publish/subscribe mechanism to deliver key events to a reusable object.

1.1 matrixtest Statechart

Figure 1. matrixtest Statechart

2 Matrix on Various Platforms

Because each platform could have unique pins or header layouts, we show how to setup matrix on multiple platforms.
The matrix test class acts as a simple keypad, reacting when a switch is pressed and as well as when it's released.
1
22
23
24
25
26
27
28
29
30
31
32
[GPLv2]
 
#ifndef matrixtest_h
#define matrixtest_h
 
#include "pin.h"
 
typedef struct matrixtestTag matrixtest;
 
extern matrixtest * matrixtestNew(pin * pRow1, pin * pRow2, pin * pRow3, pin * pCol1, pin * pCol2, pin * pCol3);
 
#endif
matrixtest.h - Portable Matrix Test Interface
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
[GPLv2]
 
#include "oosmos.h"
#include "pin.h"
#include "sw.h"
#include "prt.h"
#include "matrix.h"
#include "matrixtest.h"
 
typedef enum
{
PressedEvent = 1,
ReleasedEvent,
SpecialPressedEvent,
SpecialReleasedEvent,
} eEvents;
 
struct matrixtestTag
{
oosmos_sStateMachine(StateMachine, oosmos_sEvent, 5);
oosmos_sLeaf Idle_State;
 
matrix * m_pMatrix;
};
 
static void NewSwitch(matrixtest * pMatrixTest, int Row, int Column, int PressedEventCode, int ReleasedEventCode, const char * pString)
{
matrix * pMatrix = pMatrixTest->m_pMatrix;
 
pin * pColumnPin = matrixGetColumnPin(pMatrix, Column);
sw * pSwitch = swNewDetached(pColumnPin);
matrixAssignSwitch(pMatrix, pSwitch, Row, Column);
 
swSubscribeCloseEvent(pSwitch, &pMatrixTest->EventQueue, PressedEventCode, (void *) pString);
swSubscribeOpenEvent(pSwitch, &pMatrixTest->EventQueue, ReleasedEventCode, (void *) pString);
}
 
static bool Idle_State_Code(void * pObject, oosmos_sRegion * pRegion, const oosmos_sEvent * pEvent)
{
const char * pString = (const char *) pEvent->pContext;
 
switch (pEvent->Code) {
case PressedEvent:
return prtFormatted("Pressed %s\n", pString);
case ReleasedEvent:
return prtFormatted("Released %s\n", pString);
case SpecialPressedEvent:
return prtFormatted("Pressed special %s\n", pString);
case SpecialReleasedEvent:
return prtFormatted("Released special %s\n", pString);
}
 
return false;
}
 
extern matrixtest * matrixtestNew(pin * pRow1, pin * pRow2, pin * pRow3, pin * pCol1, pin * pCol2, pin * pCol3)
{
oosmos_Allocate(pMatrixTest, matrixtest, 1, NULL);
 
// StateName Parent Default
// =====================================================
oosmos_StateMachineInit(pMatrixTest, StateMachine, NULL, Idle_State);
oosmos_LeafInit (pMatrixTest, Idle_State, StateMachine );
 
pMatrixTest -> m_pMatrix = matrixNew(3, 3, pRow1, pRow2, pRow3, pCol1, pCol2, pCol3);
 
NewSwitch(pMatrixTest, 1, 1, PressedEvent, ReleasedEvent, "1");
NewSwitch(pMatrixTest, 1, 2, PressedEvent, ReleasedEvent, "2");
NewSwitch(pMatrixTest, 1, 3, PressedEvent, ReleasedEvent, "3");
 
NewSwitch(pMatrixTest, 2, 1, SpecialPressedEvent, SpecialReleasedEvent, "4");
NewSwitch(pMatrixTest, 2, 2, PressedEvent, ReleasedEvent, "5");
NewSwitch(pMatrixTest, 2, 3, PressedEvent, ReleasedEvent, "6");
 
NewSwitch(pMatrixTest, 3, 1, PressedEvent, ReleasedEvent, "7");
NewSwitch(pMatrixTest, 3, 2, PressedEvent, ReleasedEvent, "8");
NewSwitch(pMatrixTest, 3, 3, PressedEvent, ReleasedEvent, "9");
 
return pMatrixTest;
}
matrixtest.c - Portable Matrix Test Implementation

2.1 Matrix 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.
Note that the red power jumper wire on the breadboard is not used. By convention, we wire each breadboard to connect the power rails to allow for intuitive part layouts.
Figure 2. Arduino Wiring Diagram
This is the main program for the Arduino.
1
22
23
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
[GPLv2]
 
#includes...
 
// Required by prt...
unsigned long prtArduinoBaudRate = 115200;
 
extern void setup()
{
pin * pRow1 = pinNew(7, pinOut, pinActiveLow);
pin * pRow2 = pinNew(6, pinOut, pinActiveLow);
pin * pRow3 = pinNew(5, pinOut, pinActiveLow);
 
pin * pCol1 = pinNew(4, pinIn, pinActiveLow);
pin * pCol2 = pinNew(3, pinIn, pinActiveLow);
pin * pCol3 = pinNew(2, pinIn, pinActiveLow);
 
matrixtestNew(pRow1, pRow2, pRow3, pCol1, pCol2, pCol3);
}
 
extern void loop()
{
oosmos_RunStateMachines();
}
MatrixExample.ino - Matrix main program for Arduino

2.2 Matrix on PIC32 Starter Kit

The PIC32 Starter Kit board is a very small board with virtually no breakout pins. To access the large number of pins on the PIC32 chip, Microchip sells an add-on expansion board onto which the starter kit board can be connected. The diagram below shows about half of the female expansion board connector J10.
Figure 3. PIC32 Wiring Diagram
This is the main program for the PIC32.
1
22
23
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
[GPLv2]
 
#includes...
 
#pragma config FPLLMUL = MUL_20, FPLLIDIV = DIV_2, FPLLODIV = DIV_1, FWDTEN = OFF
#pragma config POSCMOD = HS, FNOSC = PRIPLL, FPBDIV = DIV_1
 
extern int main(void)
{
oosmos_ClockSpeedInMHz(80);
 
pin * pRow1 = pinNew(IOPORT_E, BIT_7, pinOut, pinActiveLow);
pin * pRow2 = pinNew(IOPORT_E, BIT_5, pinOut, pinActiveLow);
pin * pRow3 = pinNew(IOPORT_E, BIT_3, pinOut, pinActiveLow);
 
pin * pCol1 = pinNew(IOPORT_C, BIT_1, pinIn, pinActiveLow);
pin * pCol2 = pinNew(IOPORT_C, BIT_2, pinIn, pinActiveLow);
pin * pCol3 = pinNew(IOPORT_C, BIT_3, pinIn, pinActiveLow);
 
matrixtestNew(pRow1, pRow2, pRow3, pCol1, pCol2, pCol3);
 
while (true) {
oosmos_RunStateMachines();
}
}
main.c - Matrix main program for PIC32
Copyright © 2014-2016  OOSMOS, LLC