Serial Wombat Arduino Library

Loading...
Searching...
No Matches
SerialWombatMatrixKeypad.h
Go to the documentation of this file.
1#pragma once
2
3/*
4 Copyright 2021-2025 Broadwell Consulting Inc.
5
6"Serial Wombat" is a registered trademark of Broadwell Consulting Inc. in
7 the United States. See SerialWombat.com for usage guidance.
8
9 Permission 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
16 The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
18
19 THE 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
87 class SerialWombatMatrixKeypad :
88 public Stream
89{
90 public:
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,
177 _pin, (uint8_t)PIN_MODE_MATRIX_KEYPAD,
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,
201 _pin, (uint8_t)PIN_MODE_MATRIX_KEYPAD,
202 tableIndex,asciiValue,0x55,0x55,0x55};
203 return _sw.sendPacket(tx);
204 }
205
209 int available()
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
295 int availableForWrite()
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
375 SerialWombatMatrixKeypad& operator=(SerialWombatMatrixKeypad& kp)
376 {
377 return kp;
378 }
379
380
381 SerialWombatChip& _sw;
382 uint8_t _pin = 255;
383 protected:
384 uint32_t timeout = 1;
385};
386
387
394 class SerialWombatMatrixButton : public SerialWombatAbstractButton
395{
396 public:
403 SerialWombatMatrixButton(SerialWombatMatrixKeypad& kp, uint8_t keyIndex):SerialWombatAbstractButton(),_keypad(kp)
404 {
405 _keypad = kp;
406 _keyIndex = keyIndex;
407 }
408
417 bool digitalRead()
418{
419 uint8_t tx[] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_MODE6,
420 _keypad._pin,
421 PIN_MODE_MATRIX_KEYPAD,
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
440 uint16_t readDurationInFalseState_mS()
441{
442 uint8_t tx[] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_MODE6,
443 _keypad._pin,
444 PIN_MODE_MATRIX_KEYPAD,
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
466 uint16_t readDurationInTrueState_mS()
467{
468 uint8_t tx[] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_MODE6,
469 _keypad._pin,
470 PIN_MODE_MATRIX_KEYPAD,
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 (void) resetTransitionCount; //Get rid of unused warning
499 return digitalRead();
500}
501
502 private:
503 SerialWombatMatrixKeypad& _keypad;
504 uint8_t _keyIndex;
505};
506
SerialWombat.h
SW_LE16
#define SW_LE16(_a)
Convert a uint16_t to two bytes in little endian format for array initialization.
Definition SerialWombat.h:46
SerialWombatCommands::CONFIGURE_PIN_MODE0
@ CONFIGURE_PIN_MODE0
(200)
Definition SerialWombat.h:237
SerialWombatCommands::CONFIGURE_PIN_MODE5
@ CONFIGURE_PIN_MODE5
(205)
Definition SerialWombat.h:242
SerialWombatCommands::CONFIGURE_PIN_MODE8
@ CONFIGURE_PIN_MODE8
(208)
Definition SerialWombat.h:245
SerialWombatCommands::CONFIGURE_PIN_MODE6
@ CONFIGURE_PIN_MODE6
(206)
Definition SerialWombat.h:243
SerialWombatCommands::CONFIGURE_PIN_MODE9
@ CONFIGURE_PIN_MODE9
(209)
Definition SerialWombat.h:246
SerialWombatCommands::CONFIGURE_PIN_MODE7
@ CONFIGURE_PIN_MODE7
(207)
Definition SerialWombat.h:244
PIN_MODE_MATRIX_KEYPAD
@ PIN_MODE_MATRIX_KEYPAD
(15)
Definition SerialWombat.h:275
SerialWombatAbstractButton::transitions
uint16_t transitions
Number of transitions returned by last call to readTransitionsState()
Definition SerialWombatAbstractButton.h:72
SerialWombatAbstractButton::SerialWombatAbstractButton
SerialWombatAbstractButton()
Definition SerialWombatAbstractButton.h:44
SerialWombatChip
Class for a Serial Wombat chip. Each Serial Wombat chip on a project should have its own instance.
Definition SerialWombat.h:313
SerialWombatMatrixButton::readDurationInTrueState_mS
uint16_t readDurationInTrueState_mS()
return the number of mS that the button has been in true state
Definition SerialWombatMatrixKeypad.h:466
SerialWombatMatrixButton::readTransitionsState
bool readTransitionsState(bool resetTransitionCount=true)
Queries the number of transistions that have occured on the button.
Definition SerialWombatMatrixKeypad.h:496
SerialWombatMatrixButton::digitalRead
bool digitalRead()
Returns the state of the input.
Definition SerialWombatMatrixKeypad.h:417
SerialWombatMatrixButton::readDurationInFalseState_mS
uint16_t readDurationInFalseState_mS()
return the number of mS that the button has been in false state
Definition SerialWombatMatrixKeypad.h:440
SerialWombatMatrixButton::SerialWombatMatrixButton
SerialWombatMatrixButton(SerialWombatMatrixKeypad &kp, uint8_t keyIndex)
Instantiate a SerialWombatMatrixButton.
Definition SerialWombatMatrixKeypad.h:403
SerialWombatMatrixKeypad
A class for the Serial Wombat SW18AB chips which scans matrix keypads up to 4x4.
Definition SerialWombatMatrixKeypad.h:89
SerialWombatMatrixKeypad::_pin
uint8_t _pin
Definition SerialWombatMatrixKeypad.h:382
SerialWombatMatrixKeypad::peek
int peek()
Query the SerialWombatMatrixKeypad queue for the next avaialble byte, but don't remove it from the qu...
Definition SerialWombatMatrixKeypad.h:248
SerialWombatMatrixKeypad::begin
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.
Definition SerialWombatMatrixKeypad.h:114
SerialWombatMatrixKeypad::SerialWombatMatrixKeypad
SerialWombatMatrixKeypad(SerialWombatChip &serialWombat)
Constructor for the SerialWombatMatrixKeypad class.
Definition SerialWombatMatrixKeypad.h:95
SerialWombatMatrixKeypad::writeAsciiTable
int16_t writeAsciiTable(uint8_t tableIndex, uint8_t asciiValue)
Change the default ASCII output for each key.
Definition SerialWombatMatrixKeypad.h:198
SerialWombatMatrixKeypad::availableForWrite
int availableForWrite()
Number of bytes avaialble to write to SerialWombatMatrixKeypad queue. Returns 0.
Definition SerialWombatMatrixKeypad.h:295
SerialWombatMatrixKeypad::setTimeout
void setTimeout(long timeout_mS)
implemented to fulfill Stream requirement.
Definition SerialWombatMatrixKeypad.h:359
SerialWombatMatrixKeypad::_sw
SerialWombatChip & _sw
Definition SerialWombatMatrixKeypad.h:381
SerialWombatMatrixKeypad::available
int available()
Queries the SerialWombatMatrixKeypad for number bytes available to read.
Definition SerialWombatMatrixKeypad.h:209
SerialWombatMatrixKeypad::write
size_t write(const uint8_t *buffer, size_t size)
Write bytes to the SerialWombatMatrixKeypad queue (Does nothing)
Definition SerialWombatMatrixKeypad.h:285
SerialWombatMatrixKeypad::writeQueueMask
int16_t writeQueueMask(uint16_t mask)
Set a binary mask for which keys are added to Queue.
Definition SerialWombatMatrixKeypad.h:174
SerialWombatMatrixKeypad::flush
void flush()
Discard all bytes from the SerialWombatMatrixKeypad queue.
Definition SerialWombatMatrixKeypad.h:240
SerialWombatMatrixKeypad::read
int read()
Reads a byte from the SerialWombatMatrixKeypad queue.
Definition SerialWombatMatrixKeypad.h:221
SerialWombatMatrixKeypad::operator=
SerialWombatMatrixKeypad & operator=(SerialWombatMatrixKeypad &kp)
used to allow reference copy. Not for user use.
Definition SerialWombatMatrixKeypad.h:375
SerialWombatMatrixKeypad::readBytes
size_t readBytes(char *buffer, size_t length)
Reads a specified number of bytes from the SerialWombatMatrixKeypad queue queue.
Definition SerialWombatMatrixKeypad.h:310
SerialWombatMatrixKeypad::timeout
uint32_t timeout
Definition SerialWombatMatrixKeypad.h:384
SerialWombatMatrixKeypad::write
size_t write(uint8_t data)
Write a byte to the SerialWombatMatrixKeypad queue (Does Nothing)
Definition SerialWombatMatrixKeypad.h:270