Serial Wombat Arduino Library
Loading...
Searching...
No Matches
SerialWombatMatrixKeypad.h
Go to the documentation of this file.
1#pragma once
2
3/*
4Copyright 2021-2025 Broadwell Consulting Inc.
5
6"Serial Wombat" is a registered trademark of Broadwell Consulting Inc. in
7the United States. See SerialWombat.com for usage guidance.
8
9Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
15
16The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
18
19THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
23 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
24 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25 * OTHER DEALINGS IN THE SOFTWARE.
26*/
27
28#include "Stream.h"
29#include "SerialWombat.h"
86
88 public Stream
89{
90public:
95 SerialWombatMatrixKeypad(SerialWombatChip& serialWombat):_sw(serialWombat)
96 {
97 _sw = serialWombat;
98 }
99
114 int16_t begin(uint8_t controlPin,
115 uint8_t row0pin, uint8_t row1pin, uint8_t row2pin, uint8_t row3pin,
116 uint8_t column0pin, uint8_t column1pin, uint8_t column2pin, uint8_t column3pin,
117 uint8_t bufferMode = 0, uint8_t queueMode = 1, uint8_t rowTiming = 5)
118 {
119 _pin = controlPin;
120
121 uint8_t tx[] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_MODE0,
122 _pin,
123 (uint8_t)PIN_MODE_MATRIX_KEYPAD ,
124 row0pin,
125 row1pin,
126 row2pin,
127 row3pin,
128 column0pin };
129 int16_t result = _sw.sendPacket(tx);
130 if (result < 0)
131 {
132 return result;
133 }
134
135 uint8_t tx5[] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_MODE5,
136 _pin,
137 (uint8_t)PIN_MODE_MATRIX_KEYPAD ,
138 column1pin,
139 column2pin,
140 column3pin,
141 bufferMode,
142 queueMode };
143 result = _sw.sendPacket(tx5);
144 if (result < 0)
145 {
146 return result;
147 }
148 uint8_t tx8[] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_MODE8,
149 _pin,
150 (uint8_t)PIN_MODE_MATRIX_KEYPAD ,
151rowTiming,
1520x55,
1530x55,
1540x55,
1550x55
156 };
157 return _sw.sendPacket(tx8);
158 }
159
160
174 int16_t writeQueueMask(uint16_t mask)
175 {
176 uint8_t tx[8] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_MODE7,
178 SW_LE16(mask),0x55,0x55,0x55 };
179 return _sw.sendPacket(tx);
180 }
181
198 int16_t writeAsciiTable(uint8_t tableIndex, uint8_t asciiValue )
199 {
200 uint8_t tx[8] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_MODE9,
202 tableIndex,asciiValue,0x55,0x55,0x55};
203 return _sw.sendPacket(tx);
204 }
205
210 {
211 uint8_t tx[8] = { 201, _pin, (uint8_t)PIN_MODE_MATRIX_KEYPAD, 0,0x55,0x55,0x55,0x55 };
212 uint8_t rx[8];
213 _sw.sendPacket(tx, rx);
214 return (rx[4]);
215 }
216
221 int read()
222 {
223 uint8_t tx[8] = { 202, _pin,(uint8_t)PIN_MODE_MATRIX_KEYPAD, 1,0x55,0x55,0x55,0x55 };
224 uint8_t rx[8];
225 if (_sw.sendPacket(tx, rx) < 0)
226 {
227 return -1;
228 }
229
230 if (rx[3] != 0)
231 {
232 return (rx[4]);
233 }
234 else
235 {
236 return (-1);
237 }
238 }
239
240 void flush()
241 {
242 //TODO
243 }
244
248 int peek()
249 {
250 uint8_t tx[8] = { 203, _pin,(uint8_t)PIN_MODE_MATRIX_KEYPAD,0x55,0x55,0x55,0x55,0x55 };
251 uint8_t rx[8];
252 _sw.sendPacket(tx, rx);
253 if (rx[4] > 0)
254 {
255 return (rx[5]);
256 }
257 else
258 {
259 return (-1);
260 }
261 }
262
270 size_t write(uint8_t data)
271{
272 (void)data; // Avoid compiler warning about unused parameter
273
274 return (1);
275}
276
285 size_t write(const uint8_t* buffer, size_t size)
286{
287 (void)buffer; // Avoid compiler warning about unused parameter
288 return(size);
289}
290
296 {
297 return(0);
298 }
299
310 size_t readBytes(char* buffer, size_t length)
311{
312 int index = 0;
313 int bytesAvailable = 0;
314 uint32_t timeoutMillis = millis() + timeout;
315 while (length > 0 && timeoutMillis > millis())
316 {
317 int bytecount = 4;
318 if (length < 4)
319 {
320 bytecount = length;
321 }
322 {
323
324 uint8_t tx[8] = { 202, _pin,(uint8_t)PIN_MODE_MATRIX_KEYPAD, (uint8_t)bytecount,0x55,0x55,0x55,0x55 };
325 uint8_t rx[8];
326 _sw.sendPacket(tx, rx);
327 bytesAvailable = rx[3];
328
329 if (bytesAvailable == 0)
330 {
331 continue;
332 }
333 else
334 {
335 timeoutMillis = millis() + timeout;
336 }
337 uint8_t bytesReturned = bytecount;
338 if (rx[3] < bytecount)
339 {
340 bytesReturned = rx[3];
341 }
342 for (int i = 0; i < bytesReturned; ++i)
343 {
344 buffer[index] = rx[i + 4];
345 ++index;
346 --bytesAvailable;
347 --length;
348
349 }
350 }
351
352 }
353 return (index);
354}
355
359 void setTimeout(long timeout_mS)
360 {
361 if (timeout_mS == 0)
362 {
363 timeout = 0x80000000;
364 }
365 else
366 {
367 timeout = timeout_mS;
368 }
369 }
370
371
379
380
382 uint8_t _pin = 255;
383protected:
384 uint32_t timeout = 1;
385};
386
387
395{
396public:
404 {
405 _keypad = kp;
406 _keyIndex = keyIndex;
407 }
408
418{
419 uint8_t tx[] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_MODE6,
420 _keypad._pin,
422 0,
423 _keyIndex,0x55,0x55,0x55 };
424 uint8_t rx[8];
425 int result = _keypad._sw.sendPacket(tx, rx);
426 if (result >= 0)
427 {
428 transitions = rx[4] + 256 * rx[5];
429
430 return (rx[3] > 0);
431 }
432 return(0);
433}
434
441{
442 uint8_t tx[] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_MODE6,
443 _keypad._pin,
445 0,
446 _keyIndex,0x55,0x55,0x55 };
447 uint8_t rx[8];
448 int result = _keypad._sw.sendPacket(tx, rx);
449 if (result >= 0)
450 {
451 transitions = rx[4] + 256 * rx[5];
452 uint16_t time = rx[6] + 256 * rx[7];
453 if (rx[3] == 0)
454 {
455 return(time);
456 }
457 }
458 return(0);
459}
460
467{
468 uint8_t tx[] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_MODE6,
469 _keypad._pin,
471 0,
472 _keyIndex,0x55,0x55,0x55 };
473 uint8_t rx[8];
474 int result = _keypad._sw.sendPacket(tx, rx);
475 if (result >= 0)
476 {
477 transitions = rx[4] + 256 * rx[5];
478 uint16_t time = rx[6] + 256 * rx[7];
479 if (rx[3] == 1)
480 {
481 return(time);
482 }
483 }
484 return(0);
485}
486
496 bool readTransitionsState(bool resetTransitionCount = true)
497{
498 return digitalRead();
499}
500
501private:
503 uint8_t _keyIndex;
504};
505
#define SW_LE16(_a)
Convert a uint16_t to two bytes in little endian format for array initialization.
@ PIN_MODE_MATRIX_KEYPAD
(15)
uint16_t transitions
Number of transitions returned by last call to readTransitionsState()
Class for a Serial Wombat chip. Each Serial Wombat chip on a project should have its own instance.
uint16_t readDurationInTrueState_mS()
return the number of mS that the button has been in true state
bool readTransitionsState(bool resetTransitionCount=true)
Queries the number of transistions that have occured on the button.
bool digitalRead()
Returns the state of the input.
uint16_t readDurationInFalseState_mS()
return the number of mS that the button has been in false state
SerialWombatMatrixButton(SerialWombatMatrixKeypad &kp, uint8_t keyIndex)
Instantiate a SerialWombatMatrixButton.
A class for the Serial Wombat SW18AB chips which scans matrix keypads up to 4x4.
int peek()
Query the SerialWombatMatrixKeypad queue for the next avaialble byte, but don't remove it from the qu...
int16_t begin(uint8_t controlPin, uint8_t row0pin, uint8_t row1pin, uint8_t row2pin, uint8_t row3pin, uint8_t column0pin, uint8_t column1pin, uint8_t column2pin, uint8_t column3pin, uint8_t bufferMode=0, uint8_t queueMode=1, uint8_t rowTiming=5)
Initalize the SerialWombatMatrixKeypad.
SerialWombatMatrixKeypad(SerialWombatChip &serialWombat)
Constructor for the SerialWombatMatrixKeypad class.
int16_t writeAsciiTable(uint8_t tableIndex, uint8_t asciiValue)
Change the default ASCII output for each key.
int availableForWrite()
Number of bytes avaialble to write to SerialWombatMatrixKeypad queue. Returns 0.
void setTimeout(long timeout_mS)
implemented to fulfill Stream requirement.
int available()
Queries the SerialWombatMatrixKeypad for number bytes available to read.
size_t write(const uint8_t *buffer, size_t size)
Write bytes to the SerialWombatMatrixKeypad queue (Does nothing)
int16_t writeQueueMask(uint16_t mask)
Set a binary mask for which keys are added to Queue.
SerialWombatMatrixKeypad operator=(SerialWombatMatrixKeypad &kp)
used to allow reference copy. Not for user use.
void flush()
Discard all bytes from the SerialWombatMatrixKeypad queue.
int read()
Reads a byte from the SerialWombatMatrixKeypad queue.
size_t readBytes(char *buffer, size_t length)
Reads a specified number of bytes from the SerialWombatMatrixKeypad queue queue.
size_t write(uint8_t data)
Write a byte to the SerialWombatMatrixKeypad queue (Does Nothing)