Repository navigation
Expand file tree
/
Copy pathMyMenu.cpp
More file actions
155 lines (135 loc) · 4.33 KB
/
Copy pathMyMenu.cpp
File metadata and controls
155 lines (135 loc) · 4.33 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
////////////////////////////////////////////////////////////////////////////
// MyMenu.cpp - Library for MyMenu
// Created by Douglas Gilliland. 2015-04-23
// MyMenu is a card which has all of the menu parts
// I2C Two-wire interface
// OLED pass-thru connector
// MCP23008 I2C Port Expander GPIO
// Keypad (Up, Down, Left, Right, Select)
// (3) LEDs
// http://land-boards.com/blwiki/index.php?title=MyMenu
////////////////////////////////////////////////////////////////////////////
#include "MyMenu.h"
#include <inttypes.h>
////////////////////////////////////////////////////////////////////////////
// MyMenu constructor - As I learned by looking at the Adafruit LiquidCrystal
// I2C library, can't can't call begin from the constructor.
// This probably has something to do with the constructor being a global.
// The begin gets called from the setup code.
////////////////////////////////////////////////////////////////////////////
MyMenu::MyMenu(void)
{
return;
}
void MyMenu::begin(int portNum)
{
mcp.begin(portNum); // use default address 0
TWBR = 12; // go to 400 KHz I2C speed mode
initPins();
}
void MyMenu::begin(void)
{
mcp.begin(); // use default address 0
TWBR = 12; // go to 400 KHz I2C speed mode
initPins();
}
////////////////////////////////////////////////////////////////////////////
// initPins() - Initialize the pins on MyMenu card
////////////////////////////////////////////////////////////////////////////
void MyMenu::initPins(void)
{
mcp.pinMode(0, OUTPUT);
mcp.pinMode(1, OUTPUT);
mcp.pinMode(2, OUTPUT);
mcp.pinMode(3, INPUT);
mcp.pinMode(4, INPUT);
mcp.pinMode(5, INPUT);
mcp.pinMode(6, INPUT);
mcp.pinMode(7, INPUT);
return;
}
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
void MyMenu::setLED(int ledNum, int val)
{
mcp.digitalWrite(ledNum, val);
}
////////////////////////////////////////////////////////////////////////////
// pollKeypad() - Returns the value of the instantaneously pressed keys
////////////////////////////////////////////////////////////////////////////
uint8_t MyMenu::pollKeypad(void)
{
uint8_t readVal;
readVal = mcp.readGPIO();
if ((readVal & 0xf8) == 0xf8)
return(NONE);
else if ((readVal & 0x08) == 0)
return(SELECT);
else if ((readVal & 0x10) == 0)
return(RIGHT);
else if ((readVal & 0x20) == 0)
return(DOWN);
else if ((readVal & 0x40) == 0)
return(UP);
else if ((readVal & 0x80) == 0)
return(LEFT);
else
return(NONE);
}
////////////////////////////////////////////////////////////////////////////
// waitForKeyRelease() - Stick in the routine as long as the key is pressed
////////////////////////////////////////////////////////////////////////////
void MyMenu::waitForKeyRelease(void)
{
while (pollKeypad() != NONE)
delay(5);
return;
}
////////////////////////////////////////////////////////////////////////////
// getKeyPressed() - Hang around polling the keypad waiting for a key
// to be pressed.
// When the key is pressed, hang around waiting for the key to be released.
// Returns the value of the key that was pressed.
////////////////////////////////////////////////////////////////////////////
uint8_t MyMenu::getKeyPressed(void)
{
signed char keyPadVal2;
keyPadVal2 = pollKeypad();
if (keyPadVal2 == NONE)
return((byte)NONE);
waitForKeyRelease();
return((byte)keyPadVal2);
}
////////////////////////////////////////////////////////////////////////////
// waitKeyPressed()
////////////////////////////////////////////////////////////////////////////
uint8_t MyMenu::waitKeyPressed(void)
{
uint8_t keyPadVal3;
do
{
keyPadVal3 = pollKeypad();
}
while (keyPadVal3 == NONE);
delay(5);
waitForKeyRelease();
return((uint8_t)keyPadVal3);
}
////////////////////////////////////////////////////////////////////////////
// delayAvailable(int delayTime) - Delay but check the Serial.available flag
// This is useful for places that a delay lasts for a noticeable time and
// there is a reason to break into the delay for serial input.
////////////////////////////////////////////////////////////////////////////
uint8_t MyMenu::delayAvailable(int delayTime)
{
int delayTimeDiv4;
delayTimeDiv4 = delayTime >> 2;
while (delayTimeDiv4 > 0)
{
if (pollKeypad() != NONE)
return((byte)1);
delay(4);
delayTimeDiv4--;
}
return(0);
}