Serial Wombat Arduino Library

Loading...
Searching...
No Matches
SerialWombatAbstractProcessedInput.h
Go to the documentation of this file.
1#pragma once
2/*
3 Copyright 2021-2024 Broadwell Consulting Inc.
4
5"Serial Wombat" is a registered trademark of Broadwell Consulting Inc. in
6 the United States. See SerialWombat.com for usage guidance.
7
8 Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
14
15 The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
22 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
23 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24 * OTHER DEALINGS IN THE SOFTWARE.
25*/
26
27#include "SerialWombat.h"
28#include "limits.h"
29
32
33
79 class SerialWombatAbstractProcessedInput
80{
81 public:
87 SerialWombatAbstractProcessedInput(SerialWombatChip& sw):_pisw(sw){}
88
89
90 enum Period {
91 PERIOD_1mS = 0,
92 PERIOD_2mS = 1,
93 PERIOD_4mS = 2,
94 PERIOD_8mS = 3,
95 PERIOD_16mS = 4,
96 PERIOD_32mS = 5,
97 PERIOD_64mS = 6,
98 PERIOD_128mS = 7,
99 PERIOD_256mS = 8,
100 PERIOD_512mS = 9,
101 PERIOD_1024mS = 10,
102
103 };
104
105 enum OutputValue {
106 RAW = 0,
107 FIRST_ORDER_FILTERED = 1,
108 AVERAGE = 2,
109 };
110
111 enum Transform {
112 NONE = 0,
113 SCALE_RANGE = 1,
114 LINEAR_MXB = 2
115 };
116
124 int16_t writeInverted(bool inverted)
125 {
126 uint8_t tx[] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_INPUTPROCESS,
127 pin(),
128 swPinModeNumber(),
129 3,
130 (uint8_t) inverted,
131 0x55,0x55,0x55
132 };
133
134 int16_t result = _pisw.sendPacket(tx);
135
136 return(result);
137 }
138
159 int16_t writeFirstOrderFilteringConstant(uint16_t constant)
160 {
161 uint8_t tx[] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_INPUTPROCESS,
162 pin(),
163 swPinModeNumber(),
164 11,
165 SW_LE16(constant),
166 0x55,0x55
167 };
168 return (_pisw.sendPacket(tx));
169 }
170
181 int16_t writeAveragingNumberOfSamples(uint16_t numberOfSamples)
182 {
183 uint8_t tx[] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_INPUTPROCESS,
184 pin(),
185 swPinModeNumber(),
186 1,
187 SW_LE16(numberOfSamples),
188 0x55,0x55
189 };
190
191 int16_t result = _pisw.sendPacket(tx);
192
193 return(result);
194 }
195
207 int16_t writeExcludeBelowAbove(uint16_t low, uint16_t high)
208 {
209 uint8_t tx[] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_INPUTPROCESS,
210 pin(),
211 swPinModeNumber(),
212 2,
213 SW_LE16(low),
214 SW_LE16(high)
215 };
216
217 int16_t result = _pisw.sendPacket(tx);
218
219 return(result);
220 }
221
222
238 int16_t configureQueue(SerialWombatQueue* queue, Period period, bool queueHighByte = true, bool queueLowByte = true)
239 {
240 uint8_t tx[] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_INPUTPROCESS,
241 pin(),
242 swPinModeNumber(),
243 5,
244 SW_LE16(queue->startIndex)
245 ,(uint8_t)period,
246 (uint8_t)((((uint8_t) queueHighByte) << (uint8_t)1) | (uint8_t)queueLowByte)
247 };
248 int16_t result = _pisw.sendPacket(tx);
249 return(result);
250 }
251
257 int16_t configureOutputValue(OutputValue outputValue)
258 {
259 uint8_t tx[] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_INPUTPROCESS,
260 pin(),
261 swPinModeNumber(),
262 4,
263 (uint8_t)outputValue,
264 0x55,0x55,0x55
265 };
266
267 int16_t result = _pisw.sendPacket(tx);
268
269 return(result);
270 }
271
287 int16_t writeTransformScaleRange(uint16_t min, uint16_t max)
288 {
289 uint8_t tx[] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_INPUTPROCESS,
290 pin(),
291 swPinModeNumber(),
292 6,
293 SW_LE16(min),
294 SW_LE16(max)
295 };
296
297 int16_t result = _pisw.sendPacket(tx);
298
299 return(result);
300 }
301
315 int16_t writeTransformLinearMXB(int32_t m, int32_t b)
316 {
317 uint8_t tx[] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_INPUTPROCESS,
318 pin(),
319 swPinModeNumber(),
320 7,
321 SW_LE32(((uint32_t)m)),
322 };
323
324 int16_t result = _pisw.sendPacket(tx);
325 if (result < 0)
326 {
327 return(result);
328 }
329
330 uint8_t tx2[] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_INPUTPROCESS,
331 pin(),
332 swPinModeNumber(),
333 8,
334 SW_LE32(((uint32_t)b)),
335 };
336 result = _pisw.sendPacket(tx2);
337 return(result);
338 }
339
347
348 int16_t configureIntegrator(
349 uint16_t negativeMaxIndex,
350 uint16_t negativeMidIndex,
351 uint16_t negativeDeadZone,
352 uint16_t positiveDeadZone,
353 uint16_t positiveMidIndex,
354 uint16_t positiveMaxIndex,
355 uint16_t midIncrement,
356 uint16_t maxIncrement,
357 uint16_t initialValue,
358 uint8_t updateFrequencyMask = 0
359 )
360 {
361 {
362 uint8_t tx[] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_INPUTPROCESS,
363 pin(),
364 swPinModeNumber(),
365 12,
366 SW_LE16(negativeMaxIndex),
367 SW_LE16(negativeMidIndex)
368 };
369 int16_t result = _pisw.sendPacket(tx);
370
371 if (result < 0) return (result);
372 }
373 {
374 uint8_t tx[] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_INPUTPROCESS,
375 pin(),
376 swPinModeNumber(),
377 13,
378 SW_LE16(negativeDeadZone),
379 SW_LE16(positiveDeadZone),
380 };
381 int16_t result = _pisw.sendPacket(tx);
382
383 if (result < 0) return (result);
384 }
385 {
386 uint8_t tx[] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_INPUTPROCESS,
387 pin(),
388 swPinModeNumber(),
389 14,
390 SW_LE16(positiveMidIndex),
391 SW_LE16(positiveMaxIndex),
392 };
393 int16_t result = _pisw.sendPacket(tx);
394
395 if (result < 0) return (result);
396 }
397 {
398 uint8_t tx[] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_INPUTPROCESS,
399 pin(),
400 swPinModeNumber(),
401 15,
402 SW_LE16(initialValue),
403 0,0
404 };
405 int16_t result = _pisw.sendPacket(tx);
406
407 if (result < 0) return (result);
408 }
409 {
410 uint8_t tx[] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_INPUTPROCESS,
411 pin(),
412 swPinModeNumber(),
413 16,
414 SW_LE16(midIncrement),
415 SW_LE16(maxIncrement),
416 };
417 int16_t result = _pisw.sendPacket(tx);
418
419 return (result);
420 }
421 {
422 uint8_t tx[] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_INPUTPROCESS,
423 pin(),
424 swPinModeNumber(),
425 17,
426 updateFrequencyMask,
427 0,0,0
428 };
429 int16_t result = _pisw.sendPacket(tx);
430
431 return (result);
432 }
433 }
434
438 int16_t writeProcessedInputEnable(bool enabled)
439 {
440 uint8_t tx[] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_INPUTPROCESS,
441 pin(),
442 swPinModeNumber(),
443 0,
444 (uint8_t)enabled,
445 0x55,0x55,0x55
446 };
447
448 int16_t result = _pisw.sendPacket(tx);
449
450 return(result);
451 }
452
460 uint16_t readMinimum(bool resetAfterRead = false)
461 {
462 uint8_t tx[] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_INPUTPROCESS,
463 pin(),
464 swPinModeNumber(),
465 9,
466 (uint8_t)resetAfterRead,
467 0x55,0x55,0x55
468 };
469 uint8_t rx[8];
470 int16_t result = _pisw.sendPacket(tx,rx);
471 if (result < 0)
472 {
473 return (65535);
474 }
475 return(rx[4] + 256*rx[5]);
476 }
477
485 uint16_t readMaximum(bool resetAfterRead = false)
486 {
487 uint8_t tx[] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_INPUTPROCESS,
488 pin(),
489 swPinModeNumber(),
490 10,
491 (uint8_t)resetAfterRead,
492 0x55,0x55,0x55
493 };
494 uint8_t rx[8];
495 int16_t result = _pisw.sendPacket(tx, rx);
496 if (result < 0)
497 {
498 return (65535);
499 }
500 return(rx[4] + 256 * rx[5]);
501 }
502
509 uint16_t readAverage()
510 {
511 uint8_t tx[] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_INPUTPROCESS,
512 pin(),
513 swPinModeNumber(),
514 11,
515 0x55,0x55,0x55,0x55
516 };
517 uint8_t rx[8];
518 int16_t result = _pisw.sendPacket(tx, rx);
519 if (result < 0)
520 {
521 return (0);
522 }
523 return(rx[4] + 256 * rx[5]);
524 }
525
530 uint16_t readFiltered()
531 {
532 uint8_t tx[] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_INPUTPROCESS,
533 pin(),
534 swPinModeNumber(),
535 11,
536 0x55,0x55,0x55,0x55
537 };
538 uint8_t rx[8];
539 int16_t result = _pisw.sendPacket(tx, rx);
540 if (result < 0)
541 {
542 return (0);
543 }
544 return(rx[6] + 256 * rx[7]);
545 }
546
549 virtual uint8_t pin() = 0;
550
554 virtual uint8_t swPinModeNumber() = 0;
555
556 private:
557 SerialWombatChip& _pisw;
558};
559
560
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_INPUTPROCESS
@ CONFIGURE_PIN_INPUTPROCESS
(211)
Definition SerialWombat.h:250
SW_LE32
#define SW_LE32(_a)
Convert a uint32_t to four bytes in little endian format for array initialization.
Definition SerialWombat.h:49
SerialWombatAbstractProcessedInput::writeTransformScaleRange
int16_t writeTransformScaleRange(uint16_t min, uint16_t max)
Scale incoming values to a range of 0 to 65535.
Definition SerialWombatAbstractProcessedInput.h:287
SerialWombatAbstractProcessedInput::SerialWombatAbstractProcessedInput
SerialWombatAbstractProcessedInput(SerialWombatChip &sw)
Constructor for the SerialWombatAbstractScaledOutput Class.
Definition SerialWombatAbstractProcessedInput.h:87
SerialWombatAbstractProcessedInput::writeTransformLinearMXB
int16_t writeTransformLinearMXB(int32_t m, int32_t b)
Scale incoming values based on an mx+b linear equation.
Definition SerialWombatAbstractProcessedInput.h:315
SerialWombatAbstractProcessedInput::writeFirstOrderFilteringConstant
int16_t writeFirstOrderFilteringConstant(uint16_t constant)
Set a first order filtering constant to be applied to the signal Higher is heavier filtering.
Definition SerialWombatAbstractProcessedInput.h:159
SerialWombatAbstractProcessedInput::configureOutputValue
int16_t configureOutputValue(OutputValue outputValue)
Configures whether the pin's public data value is averaged, filtered, or neither.
Definition SerialWombatAbstractProcessedInput.h:257
SerialWombatAbstractProcessedInput::readMinimum
uint16_t readMinimum(bool resetAfterRead=false)
Retreive the maximum public data output value since the last call with reset= true.
Definition SerialWombatAbstractProcessedInput.h:460
SerialWombatAbstractProcessedInput::readAverage
uint16_t readAverage()
Retreive the last completed averaged value.
Definition SerialWombatAbstractProcessedInput.h:509
SerialWombatAbstractProcessedInput::readFiltered
uint16_t readFiltered()
Retreive the filtered value.
Definition SerialWombatAbstractProcessedInput.h:530
SerialWombatAbstractProcessedInput::Transform
Transform
Definition SerialWombatAbstractProcessedInput.h:111
SerialWombatAbstractProcessedInput::SCALE_RANGE
@ SCALE_RANGE
Scale the input signal to a 0-65535 value based on input high and low limits.
Definition SerialWombatAbstractProcessedInput.h:113
SerialWombatAbstractProcessedInput::LINEAR_MXB
@ LINEAR_MXB
Scale the input signal based on a linear mx+b equation.
Definition SerialWombatAbstractProcessedInput.h:114
SerialWombatAbstractProcessedInput::NONE
@ NONE
Don't transform the input signal.
Definition SerialWombatAbstractProcessedInput.h:112
SerialWombatAbstractProcessedInput::configureIntegrator
int16_t configureIntegrator(uint16_t negativeMaxIndex, uint16_t negativeMidIndex, uint16_t negativeDeadZone, uint16_t positiveDeadZone, uint16_t positiveMidIndex, uint16_t positiveMaxIndex, uint16_t midIncrement, uint16_t maxIncrement, uint16_t initialValue, uint8_t updateFrequencyMask=0)
Sort incoming data into one of 5 ranges, and integrate based on linear interpolation in those ranges.
Definition SerialWombatAbstractProcessedInput.h:348
SerialWombatAbstractProcessedInput::writeExcludeBelowAbove
int16_t writeExcludeBelowAbove(uint16_t low, uint16_t high)
Sets input value ranges which are discarded rather than processed.
Definition SerialWombatAbstractProcessedInput.h:207
SerialWombatAbstractProcessedInput::OutputValue
OutputValue
Definition SerialWombatAbstractProcessedInput.h:105
SerialWombatAbstractProcessedInput::AVERAGE
@ AVERAGE
Use an averaged signal for the pin's public data (updates less often)
Definition SerialWombatAbstractProcessedInput.h:108
SerialWombatAbstractProcessedInput::RAW
@ RAW
Use the unfiltered signal for the pin's public data.
Definition SerialWombatAbstractProcessedInput.h:106
SerialWombatAbstractProcessedInput::FIRST_ORDER_FILTERED
@ FIRST_ORDER_FILTERED
Use a first order filtered signal for the pin's public data.
Definition SerialWombatAbstractProcessedInput.h:107
SerialWombatAbstractProcessedInput::pin
virtual uint8_t pin()=0
Used for inheritance.
SerialWombatAbstractProcessedInput::Period
Period
Definition SerialWombatAbstractProcessedInput.h:90
SerialWombatAbstractProcessedInput::PERIOD_32mS
@ PERIOD_32mS
Definition SerialWombatAbstractProcessedInput.h:96
SerialWombatAbstractProcessedInput::PERIOD_1024mS
@ PERIOD_1024mS
Definition SerialWombatAbstractProcessedInput.h:101
SerialWombatAbstractProcessedInput::PERIOD_256mS
@ PERIOD_256mS
Definition SerialWombatAbstractProcessedInput.h:99
SerialWombatAbstractProcessedInput::PERIOD_8mS
@ PERIOD_8mS
Definition SerialWombatAbstractProcessedInput.h:94
SerialWombatAbstractProcessedInput::PERIOD_2mS
@ PERIOD_2mS
Definition SerialWombatAbstractProcessedInput.h:92
SerialWombatAbstractProcessedInput::PERIOD_1mS
@ PERIOD_1mS
Definition SerialWombatAbstractProcessedInput.h:91
SerialWombatAbstractProcessedInput::PERIOD_512mS
@ PERIOD_512mS
Definition SerialWombatAbstractProcessedInput.h:100
SerialWombatAbstractProcessedInput::PERIOD_4mS
@ PERIOD_4mS
Definition SerialWombatAbstractProcessedInput.h:93
SerialWombatAbstractProcessedInput::PERIOD_128mS
@ PERIOD_128mS
Definition SerialWombatAbstractProcessedInput.h:98
SerialWombatAbstractProcessedInput::PERIOD_64mS
@ PERIOD_64mS
Definition SerialWombatAbstractProcessedInput.h:97
SerialWombatAbstractProcessedInput::PERIOD_16mS
@ PERIOD_16mS
Definition SerialWombatAbstractProcessedInput.h:95
SerialWombatAbstractProcessedInput::swPinModeNumber
virtual uint8_t swPinModeNumber()=0
Used for ineheritance.
SerialWombatAbstractProcessedInput::readMaximum
uint16_t readMaximum(bool resetAfterRead=false)
Retreive the minimum public data output value since the last call with reset= true.
Definition SerialWombatAbstractProcessedInput.h:485
SerialWombatAbstractProcessedInput::configureQueue
int16_t configureQueue(SerialWombatQueue *queue, Period period, bool queueHighByte=true, bool queueLowByte=true)
Sets up the queueing feature for this pin mode. Queue must have been initialized prior to this queue.
Definition SerialWombatAbstractProcessedInput.h:238
SerialWombatAbstractProcessedInput::writeAveragingNumberOfSamples
int16_t writeAveragingNumberOfSamples(uint16_t numberOfSamples)
Set a number of samples to average for each update of the downstream signal.
Definition SerialWombatAbstractProcessedInput.h:181
SerialWombatAbstractProcessedInput::writeInverted
int16_t writeInverted(bool inverted)
if enabled subtract the input value from 65535 before doing any other processing.
Definition SerialWombatAbstractProcessedInput.h:124
SerialWombatAbstractProcessedInput::writeProcessedInputEnable
int16_t writeProcessedInputEnable(bool enabled)
Enables or disables all input processing functions If disabled, the raw input value is placed directl...
Definition SerialWombatAbstractProcessedInput.h:438
SerialWombatChip
Class for a Serial Wombat chip. Each Serial Wombat chip on a project should have its own instance.
Definition SerialWombat.h:315
SerialWombatQueue
A Class representing a Queue in the User Ram area on the Serial Wombat Chip.
Definition SerialWombatQueue.h:42
SerialWombatQueue::startIndex
uint16_t startIndex
Definition SerialWombatQueue.h:391