-
Notifications
You must be signed in to change notification settings - Fork 0
/
display_waveshareepaper154bw_sx1509ioexpander.cpp
766 lines (633 loc) · 22.5 KB
/
display_waveshareepaper154bw_sx1509ioexpander.cpp
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
/*
////////////////////////////////////////////////////////////////////////////////////
//
// This file is part of the firmware of Lightning Detector, an experimental open
// hardware project for early notification about approaching thunderstorm activity.
// Copyright (C) 2024 M. Frohne
//
// Lightning Detector's firmware is free software: you can redistribute it
// and/or modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation, either version 3
// of the License, or (at your option) any later version.
//
// Lightning Detector's firmware is distributed in the hope that it
// will be useful, but WITHOUT ANY WARRANTY; without even the implied
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Lightning Detector. If not, see <https://www.gnu.org/licenses/>.
//
////////////////////////////////////////////////////////////////////////////////////
*/
#include "display_waveshareepaper154bw_sx1509ioexpander.h"
#include "fonts.h"
#include "spiconfig.h"
#include <SPI.h>
#include <Wire.h>
/*!
* \brief Constructor.
*
* Configures the Graphics instance to work with this display driver instance and
* sets background and stroke colors to obtain black strokes on a white background.
* See also setPixel().
*
* \param pSX1509Addr I2C address of the SX1509 IO expander chip.
* \param pPinNumDspCS SX1509 pin number to connect display's \p CS pin to.
* \param pPinNumDspDC SX1509 pin number to connect display's \p DC pin to.
* \param pPinNumDspRST SX1509 pin number to connect display's \p RST pin to.
* \param pPinNumDspBUSY SX1509 pin number to connect display's \p BUSY pin to.
*/
DISPLAY_TYPE::DISPLAY_TYPE(uint8_t pSX1509Addr, uint8_t pPinNumDspCS, uint8_t pPinNumDspDC, uint8_t pPinNumDspRST, uint8_t pPinNumDspBUSY) :
AbstractSPIDisplay(200, 200, SPISettings(SPIConfig::getSPISpeed(), MSBFIRST, SPI_MODE0), pSX1509Addr),
pinNumDspCS(pPinNumDspCS),
pinNumDspDC(pPinNumDspDC),
pinNumDspRST(pPinNumDspRST),
pinNumDspBUSY(pPinNumDspBUSY),
bankAMaskDspCS(sx1509PinToBankAMask(pinNumDspCS)),
bankBMaskDspCS(sx1509PinToBankBMask(pinNumDspCS)),
bankAMaskDspDC(sx1509PinToBankAMask(pinNumDspDC)),
bankBMaskDspDC(sx1509PinToBankBMask(pinNumDspDC)),
bankAMaskDspRST(sx1509PinToBankAMask(pinNumDspRST)),
bankBMaskDspRST(sx1509PinToBankBMask(pinNumDspRST)),
bankAMaskDspBUSY(sx1509PinToBankAMask(pinNumDspBUSY)),
bankBMaskDspBUSY(sx1509PinToBankBMask(pinNumDspBUSY))
{
graphics.setDisplay(this);
graphics.begin();
graphics.background(1, 1, 1);
graphics.stroke(0, 0, 0);
}
//Public
/*!
* \copybrief AbstractSPIDisplay::setup()
*
* Enables SPI and I2C.
*
* Resets SX1509 and configures its IOs:
* - \p CS, \p DC and \p RST as outputs; inputs otherwise
* - Set high level for active-low \p CS and \p RST
* - Enable pull-down and disable input buffer for every unused pin
*
* Finally issues an initial display hardware reset.
*/
void DISPLAY_TYPE::setup() const
{
//Enable SPI and I2C
AbstractSPIDisplay::setup();
//Reset SX1509 and configure its IOs
//Reset sequence
writeRegSX1509(SX1509RegisterAddress::RegReset, 0x12);
writeRegSX1509(SX1509RegisterAddress::RegReset, 0x34);
//Only CS, DC and RST are outputs
writeRegSX1509(SX1509RegisterAddress::RegDirB, ~(bankBMaskDspCS | bankBMaskDspDC | bankBMaskDspRST));
writeRegSX1509(SX1509RegisterAddress::RegDirA, ~(bankAMaskDspCS | bankAMaskDspDC | bankAMaskDspRST));
//Start with high output for active-low CS and RST
writeRegSX1509(SX1509RegisterAddress::RegDataB, (bankBMaskDspCS | bankBMaskDspRST));
writeRegSX1509(SX1509RegisterAddress::RegDataA, (bankAMaskDspCS | bankAMaskDspRST));
//Use pull-down on every unused pin
writeRegSX1509(SX1509RegisterAddress::RegPullUpB, 0b0);
writeRegSX1509(SX1509RegisterAddress::RegPullUpA, 0b0);
writeRegSX1509(SX1509RegisterAddress::RegPullDownB, ~(bankBMaskDspCS | bankBMaskDspDC | bankBMaskDspRST | bankBMaskDspBUSY));
writeRegSX1509(SX1509RegisterAddress::RegPullDownA, ~(bankAMaskDspCS | bankAMaskDspDC | bankAMaskDspRST | bankAMaskDspBUSY));
//Disable input buffers of unused pins
writeRegSX1509(SX1509RegisterAddress::RegInputDisableB, ~(bankBMaskDspCS | bankBMaskDspDC | bankBMaskDspRST | bankBMaskDspBUSY));
writeRegSX1509(SX1509RegisterAddress::RegInputDisableA, ~(bankBMaskDspCS | bankBMaskDspDC | bankBMaskDspRST | bankBMaskDspBUSY));
//Make sure again that CS is high
deselectChip();
//Initial display hardware reset
hwResetDisplay();
}
//
/*!
* \copybrief AbstractSPIDisplay::init()
*
* Display hardware reset plus following init sequence:
* - \p 0x12
* - \p 0x01, \p 0XC7, \p 0x00, \p 0x00
* - \p 0x11, \p 0x03
* - \p 0x44, \p 0x00, \p 0x18
* - \p 0x45, \p 0x00, \p 0x00, \p 0xC7, \p 0x00
*/
void DISPLAY_TYPE::init() const
{
hwResetDisplay();
writeCommand(0x12);
waitDisplayIdle();
writeCommand(0x01, 0XC7, 0x00, 0x00);
writeCommand(0x11, 0x03); //Incrementing X, incrementing Y, auto update address along x-axis
writeCommand(0x44, 0x00, 0x18);
writeCommand(0x45, 0x00, 0x00, 0xC7, 0x00);
}
/*!
* \copybrief AbstractSPIDisplay::sleep()
*
* Sends sleep command and then fully turns off display power via turnOffDisplay().
*/
void DISPLAY_TYPE::sleep() const
{
waitDisplayIdle();
writeCommand(0x10, 0x01);
turnOffDisplay();
}
//
/*!
* \copybrief AbstractSPIDisplay::clear()
*
* Resets the buffer to all white and calls updateDisplay().
*/
void DISPLAY_TYPE::clear()
{
for (size_t i = 0; i < 5000; ++i)
displayBuffer[i] = 0b11111111;
updateDisplay();
}
//
/*!
* \copybrief AbstractSPIDisplay::update()
*
* Shows all of the passed information on the display.
* The buffer is written and then updateDisplay() is called to write the buffer out.
*
* The displayed information is formatted in the following way (~ from top-left to bottom-right):
* - \p pNumLightnings
* - \p pLightningRate
* - Battery state as both \p pBatteryVoltage and \p pBatteryPercentage
* - \p pStormDist ("???" if AS3935 reports out of range)
* - Short status information (from left to right):
* - 'D' for \p pMode = Auxil::RunMode::UnmaskDisturbers, 'T' for Auxil::RunMode::TuneAntenna, ' ' (space) else
* - 'S' if \p pSerialEnabled and ' ' (space) else
* - \p pLastInterrupt encoded as follows:
* - "" for AS3935::InterruptType::Lightning
* - "dDst" for AS3935::InterruptType::DistanceChanged
* - "Dist" for AS3935::InterruptType::Disturber
* - "NOIS" for AS3935::InterruptType::Noise
* - "INV" for AS3935::InterruptType::Invalid
* - \p pRunTimeHours as raw hours if below 24h or as days+hours otherwise.
*
* \param pNumLightnings Accumulated number of lightnings.
* \param pLightningRate Current lightning rate as number of lightnings per minute.
* \param pStormDist AS3935-internal estimate of storm distance in kilometers.
* \param pBatteryPercentage Fraction of available battery charge as percentage from 0 to 100.
* \param pBatteryVoltage Measured battery voltage.
* \param pRunTimeHours Estimated run time in hours since last reset.
* \param pMode Run mode.
* \param pSerialEnabled Serial connection enabled.
* \param pLastInterrupt Last interrupt request type from AS3935.
*/
void DISPLAY_TYPE::update(size_t pNumLightnings, float pLightningRate, uint8_t pStormDist, float pBatteryPercentage, float pBatteryVoltage,
float pRunTimeHours, Auxil::RunMode pMode, bool pSerialEnabled, AS3935::InterruptType pLastInterrupt)
{
//Prepare formatted strings
auto rightAlignString = [](String& pStr, size_t pLength) -> void
{
if (pLength < pStr.length())
return;
size_t diff = pLength - pStr.length();
for (size_t i = 0; i < diff; ++i)
pStr = String(" ") + pStr;
};
size_t numLghtStrMaxLen = 10;
String numLghtStr(pNumLightnings);
if (numLghtStr.length() > numLghtStrMaxLen)
numLghtStr = "NaN";
size_t rateStrMaxLen = 5;
size_t rateStrDecPlaces = 0;
if (static_cast<size_t>(pLightningRate) < 10)
rateStrDecPlaces = 1;
String rateStr(pLightningRate, rateStrDecPlaces);
rateStr += "/m";
if (rateStr.length() > rateStrMaxLen)
rateStr = "NaN";
rightAlignString(rateStr, rateStrMaxLen);
size_t voltageStrMaxLen = 7;
String voltageStr(pBatteryVoltage, 3);
voltageStr += " V";
if (voltageStr.length() > voltageStrMaxLen)
voltageStr = "NaN";
size_t distStrMaxLen = 8;
String distStr;
if (pStormDist == AS3935::stormDistanceOutOfRange)
distStr = "???";
else
distStr = String(pStormDist);
distStr += " km";
if (distStr.length() > distStrMaxLen)
distStr = "NaN";
rightAlignString(distStr, distStrMaxLen);
size_t batFracStrMaxLen = 7;
String batFracStr(static_cast<size_t>(pBatteryPercentage));
batFracStr += " %";
if (batFracStr.length() > batFracStrMaxLen)
batFracStr = "NaN";
size_t statusStrMaxLen = 7;
String statusStr;
if (pMode == Auxil::RunMode::Normal)
statusStr += " ";
else if (pMode == Auxil::RunMode::UnmaskDisturbers)
statusStr += "D";
else if (pMode == Auxil::RunMode::TuneAntenna)
statusStr += "T";
else
statusStr += "?";
if (pSerialEnabled)
statusStr += "S";
else
statusStr += " ";
statusStr += " ";
if (pLastInterrupt == AS3935::InterruptType::DistanceChanged)
statusStr += "dDst";
else if (pLastInterrupt == AS3935::InterruptType::Noise)
statusStr += "NOIS";
else if (pLastInterrupt == AS3935::InterruptType::Disturber)
statusStr += "Dist";
else if (pLastInterrupt == AS3935::InterruptType::Lightning)
statusStr += "";
else if (pLastInterrupt == AS3935::InterruptType::Invalid)
statusStr += "INV";
else
statusStr += "?";
if (statusStr.length() > statusStrMaxLen)
statusStr = "ERROR";
size_t pRunTimeFullDays = static_cast<size_t>(pRunTimeHours) / 24;
float pRunTimeRemainderHours = pRunTimeHours - pRunTimeFullDays*24;
size_t runtimeStrMaxLen = 8;
String runtimeStr = String(pRunTimeFullDays) + "d ";
if (pRunTimeFullDays == 0)
runtimeStr = String(pRunTimeRemainderHours, 1) + " h";
else
runtimeStr += String(static_cast<size_t>(pRunTimeRemainderHours)) + "h";
if (runtimeStr.length() > runtimeStrMaxLen)
runtimeStr = "NaN";
rightAlignString(runtimeStr, runtimeStrMaxLen);
//Clear buffer
for (size_t i = 0; i < 5000; ++i)
displayBuffer[i] = 0b11111111;
//Draw Frame
const size_t frameMargin = 1;
graphics.rect(frameMargin, frameMargin, graphics.width() - 2*frameMargin, graphics.height() - 2*frameMargin);
//Write header/caption text
graphics.textFont(&Font_TerminASCII24Bold);
const size_t textHeight = graphics.textFontHeight();
const size_t textWidth = graphics.textFontWidth();
const size_t textOfsX = 4;
const size_t textOfsY = 4;
const size_t textPadding = 3;
const size_t captionTextExtraPadding = 3;
size_t yPos = textOfsY+captionTextExtraPadding;
graphics.text(" Lightning Det. ", textOfsX, yPos);
//Write text containing the interesting information
graphics.textFont(&Font_TerminASCII24);
yPos += textHeight+captionTextExtraPadding+textPadding;
graphics.text("Lightnings Rate", textOfsX, yPos);
yPos += textHeight+textPadding;
graphics.text(numLghtStr, textOfsX, yPos);
graphics.text(rateStr, textOfsX + 11*textWidth, yPos);
yPos += textHeight+textPadding;
graphics.text("Battery Distance", textOfsX, yPos);
yPos += textHeight+textPadding;
graphics.text(voltageStr, textOfsX, yPos);
graphics.text(distStr, textOfsX + 8*textWidth, yPos);
yPos += textHeight+textPadding;
graphics.text(batFracStr, textOfsX, yPos);
graphics.text("Run Time", textOfsX + 8*textWidth, yPos);
yPos += textHeight+textPadding;
graphics.text(statusStr, textOfsX, yPos);
graphics.text(runtimeStr, textOfsX + 8*textWidth, yPos);
//Add decoration lines
const size_t yPos0 = textOfsY+captionTextExtraPadding+textHeight+captionTextExtraPadding+1;
yPos = yPos0;
graphics.line(frameMargin, yPos, graphics.width()-1-frameMargin, yPos);
yPos += textHeight+textPadding;
yPos += textHeight+textPadding;
graphics.line(frameMargin, yPos, graphics.width()-1-frameMargin, yPos);
graphics.line(textOfsX + static_cast<size_t>(10.5*textWidth), yPos0, textOfsX + static_cast<size_t>(10.5*textWidth), yPos);
graphics.line(textOfsX + static_cast<size_t>(7.5*textWidth), yPos, textOfsX + static_cast<size_t>(7.5*textWidth),
graphics.height()-1-frameMargin);
yPos += textHeight+textPadding;
yPos += textHeight+textPadding;
graphics.line(textOfsX + static_cast<size_t>(7.5*textWidth), yPos, graphics.width()-1-frameMargin, yPos);
yPos += textHeight+textPadding;
graphics.line(frameMargin, yPos, textOfsX + static_cast<size_t>(7.5*textWidth), yPos);
updateDisplay();
}
//Private
/*!
* \copybrief AbstractSPIDisplay::setPixel()
*
* Sets the pixel value for pixel (\p pX, \p pY), ignoring the specified color value (monochrome display).
* If any of \p pR, \p pG or \p pB are larger than zero the pixel value is set to 1 and it is set to zero otherwise.
*
* \param pX Display's x-coordinate.
* \param pY Display's y-coordinate.
* \param pR Red pixel value from 0 to 255.
* \param pG Green pixel value from 0 to 255.
* \param pB Blue pixel value from 0 to 255.
*/
void DISPLAY_TYPE::setPixel(size_t pX, size_t pY, uint8_t pR, uint8_t pG, uint8_t pB)
{
size_t pixIdx = graphics.width()*pY + pX;
size_t byteIdx = pixIdx / 8;
size_t bitIdx = pixIdx % 8;
uint8_t byteMask = 0b10000000 >> bitIdx;
bool set = ((pR > 0) || (pG > 0) || (pB > 0));
if (set)
displayBuffer[byteIdx] |= byteMask;
else
displayBuffer[byteIdx] &= ~byteMask;
}
//
/*!
* \brief Update the display according to the current buffer content.
*
* Does a full display update sequence using the current state of the bitmap buffer.
*/
void DISPLAY_TYPE::updateDisplay() const
{
waitDisplayIdle();
uint16_t tval12bit = 0x140; //TODO: This is ~20 degrees; how to use internal sensor???
writeCommand(0x1A, (tval12bit >> 4), ((tval12bit & 0b000000001111) << 4));
writeCommand(0x22, 0xB1);
writeCommand(0x20);
waitDisplayIdle();
writeCommand(0x4E, 0x00);
writeCommand(0x4F, 0X00, 0x00);
writeCommandRaw(0x24, &displayBuffer[0], 5000);
writeCommand(0x22, 0xC7);
writeCommand(0x20);
waitDisplayIdle();
}
//
/*!
* \brief Check if display controller is busy.
*
* \return True if \p BUSY pin reports being busy and false otherwise.
*/
bool DISPLAY_TYPE::displayBusy() const
{
bool inputHighB = (readRegSX1509(SX1509RegisterAddress::RegDataB) & bankBMaskDspBUSY) != 0b0;
bool inputHighA = (readRegSX1509(SX1509RegisterAddress::RegDataA) & bankAMaskDspBUSY) != 0b0;
return inputHighB || inputHighA;
}
/*!
* \brief Wait until display controller is not busy.
*
* See also displayBusy().
*/
void DISPLAY_TYPE::waitDisplayIdle() const
{
while (displayBusy())
delay(10);
}
//
/*!
* \brief Reset display controller via hardware reset pin.
*/
void DISPLAY_TYPE::hwResetDisplay() const
{
setPinsHighSX1509(bankBMaskDspRST, bankAMaskDspRST); //First set RST high, as constant low RST eventually turns off power
delay(100);
setPinsLowSX1509(bankBMaskDspRST, bankAMaskDspRST);
delay(10);
setPinsHighSX1509(bankBMaskDspRST, bankAMaskDspRST);
delay(10);
}
/*!
* \brief Fully turn off the display.
*
* Sets \p RST pin to low, which eventually makes the display controller to turn off power by itself.
*/
void DISPLAY_TYPE::turnOffDisplay() const
{
//Constant high RST draws ~2mA current, so turn off power completely, which is eventually triggered by constant low RST
setPinsLowSX1509(bankBMaskDspRST, bankAMaskDspRST);
}
//
/*!
* \copybrief AbstractSPIDisplay::selectChip()
*/
void DISPLAY_TYPE::selectChip() const
{
setPinsLowSX1509(bankBMaskDspCS, bankAMaskDspCS);
}
/*!
* \copybrief AbstractSPIDisplay::deselectChip()
*/
void DISPLAY_TYPE::deselectChip() const
{
setPinsHighSX1509(bankBMaskDspCS, bankAMaskDspCS);
}
//
/*!
* \brief Configure display DC pin for "command" transaction.
*/
void DISPLAY_TYPE::setSPIDCCommand() const
{
setPinsLowSX1509(bankBMaskDspDC, bankAMaskDspDC);
}
/*!
* \brief Configure display DC pin for "data" transaction.
*/
void DISPLAY_TYPE::setSPIDCData() const
{
setPinsHighSX1509(bankBMaskDspDC, bankAMaskDspDC);
}
//
/*!
* \brief Write a display command.
*
* \param pCmd Command byte.
*/
void DISPLAY_TYPE::writeCommand(uint8_t pCmd) const
{
writeCommandRaw(pCmd, nullptr, 0);
}
/*!
* \brief Write a display command with one data byte.
*
* \param pCmd Command byte.
* \param pData1 Data byte 1.
*/
void DISPLAY_TYPE::writeCommand(uint8_t pCmd, uint8_t pData1) const
{
uint8_t data[1] = {pData1};
writeCommandRaw(pCmd, &data[0], 1);
}
/*!
* \brief Write a display command with two data bytes.
*
* Order: writes \p pCmd, then \p pData1, then \p pData2.
*
* \param pCmd Command byte.
* \param pData1 Data byte 1.
* \param pData2 Data byte 2.
*/
void DISPLAY_TYPE::writeCommand(uint8_t pCmd, uint8_t pData1, uint8_t pData2) const
{
uint8_t data[2] = {pData1, pData2};
writeCommandRaw(pCmd, &data[0], 2);
}
/*!
* \brief Write a display command with three data bytes.
*
* Order: writes \p pCmd, then \p pData1, then \p pData2, etc.
*
* \param pCmd Command byte.
* \param pData1 Data byte 1.
* \param pData2 Data byte 2.
* \param pData3 Data byte 3.
*/
void DISPLAY_TYPE::writeCommand(uint8_t pCmd, uint8_t pData1, uint8_t pData2, uint8_t pData3) const
{
uint8_t data[3] = {pData1, pData2, pData3};
writeCommandRaw(pCmd, &data[0], 3);
}
/*!
* \brief Write a display command with four data bytes.
*
* Order: writes \p pCmd, then \p pData1, then \p pData2, etc.
*
* \param pCmd Command byte.
* \param pData1 Data byte 1.
* \param pData2 Data byte 2.
* \param pData3 Data byte 3.
* \param pData4 Data byte 4.
*/
void DISPLAY_TYPE::writeCommand(uint8_t pCmd, uint8_t pData1, uint8_t pData2, uint8_t pData3, uint8_t pData4) const
{
uint8_t data[4] = {pData1, pData2, pData3, pData4};
writeCommandRaw(pCmd, &data[0], 4);
}
/*!
* \brief Write a display command with many data bytes.
*
* Order: writes \p pCmd, then \p pData[0], then \p pData[1], etc.
*
* \param pCmd Command byte.
* \param pData Data bytes.
* \param pLength Number of data bytes.
*/
void DISPLAY_TYPE::writeCommandRaw(uint8_t pCmd, const uint8_t* pData, uint16_t pLength) const
{
setSPIDCCommand();
SPI.beginTransaction(spiSettings);
selectChip();
delayMicroseconds(20);
SPI.transfer(pCmd);
if (pLength > 0)
{
setSPIDCData();
delayMicroseconds(20);
}
for (uint16_t i = 0; i < pLength; ++i)
SPI.transfer(*(pData+i));
deselectChip();
SPI.endTransaction();
setSPIDCCommand();
}
//
/*!
* \brief Write to SX1509 register.
*
* \param pAddr Register address.
* \param pVal New value.
*/
void DISPLAY_TYPE::writeRegSX1509(SX1509RegisterAddress pAddr, uint8_t pVal) const
{
uint8_t regAddr = static_cast<uint8_t>(pAddr);
Wire.beginTransmission(ioExpanderI2CAddr);
Wire.write(regAddr);
Wire.write(pVal);
Wire.endTransmission(true);
}
/*!
* \brief Read from SX1509 register.
*
* \param pAddr Register address.
* \return Read value.
*/
uint8_t DISPLAY_TYPE::readRegSX1509(SX1509RegisterAddress pAddr) const
{
uint8_t regAddr = static_cast<uint8_t>(pAddr);
Wire.beginTransmission(ioExpanderI2CAddr);
Wire.write(regAddr);
Wire.endTransmission(false);
Wire.requestFrom(ioExpanderI2CAddr, 1, true);
uint8_t retVal = Wire.read();
return retVal;
}
//
/*!
* \brief Set some SX1509 output pins to high state.
*
* Output pins corresponding to a '1' bit in the bit masks \p pBankBMask and
* \p pBankAMask are set to \e high state while keeping the state of all other pins.
*
* \param pBankBMask Bit mask for SX1509's IO bank B.
* \param pBankAMask Bit mask for SX1509's IO bank A.
*/
void DISPLAY_TYPE::setPinsHighSX1509(uint8_t pBankBMask, uint8_t pBankAMask) const
{
uint8_t dataB = readRegSX1509(SX1509RegisterAddress::RegDataB);
uint8_t dataA = readRegSX1509(SX1509RegisterAddress::RegDataA);
dataB |= pBankBMask;
dataA |= pBankAMask;
writeRegSX1509(SX1509RegisterAddress::RegDataB, dataB);
writeRegSX1509(SX1509RegisterAddress::RegDataA, dataA);
}
/*!
* \brief Set some SX1509 output pins to low state.
*
* Output pins corresponding to a '1' bit in the bit masks \p pBankBMask and
* \p pBankAMask are set to \e low state while keeping the state of all other pins.
*
* \param pBankBMask Bit mask for SX1509's IO bank B.
* \param pBankAMask Bit mask for SX1509's IO bank A.
*/
void DISPLAY_TYPE::setPinsLowSX1509(uint8_t pBankBMask, uint8_t pBankAMask) const
{
uint8_t dataB = readRegSX1509(SX1509RegisterAddress::RegDataB);
uint8_t dataA = readRegSX1509(SX1509RegisterAddress::RegDataA);
dataB &= ~pBankBMask;
dataA &= ~pBankAMask;
writeRegSX1509(SX1509RegisterAddress::RegDataB, dataB);
writeRegSX1509(SX1509RegisterAddress::RegDataA, dataA);
}
//
/*!
* \brief Convert SX1509 pin number to a combined bit mask for both IO banks.
*
* Generates a bit pattern {\p p15, ..., \p p0} with all zeroes except a one at \p pi = \p pPin.
*
* \param pPin SX1509 pin number.
* \return 16 bit long bit mask with 1 for \p pPin and 0 else.
*/
uint16_t DISPLAY_TYPE::sx1509PinToBankMask(uint8_t pPin)
{
uint16_t pinMask = (0b1 << pPin);
return pinMask;
}
/*!
* \brief Convert SX1509 pin number to a bit mask for IO bank A.
*
* Generates a bit pattern {\p p7, ..., \p p0} with all zeroes except a one at \p pi = \p pPin.
*
* \param pPin SX1509 pin number.
* \return 8 bit long bit mask (just bank A) with 1 for \p pPin and 0 else.
*/
uint8_t DISPLAY_TYPE::sx1509PinToBankAMask(uint8_t pPin)
{
return static_cast<uint8_t>(sx1509PinToBankMask(pPin) & 0b0000000011111111);
}
/*!
* \brief Convert SX1509 pin number to a bit mask for IO bank B.
*
* Generates a bit pattern {\p p15, ..., \p p8} with all zeroes except a one at \p pi = \p pPin.
*
* \param pPin SX1509 pin number.
* \return 8 bit long bit mask (just bank B) with 1 for \p pPin and 0 else.
*/
uint8_t DISPLAY_TYPE::sx1509PinToBankBMask(uint8_t pPin)
{
return static_cast<uint8_t>((sx1509PinToBankMask(pPin) >> 8) & 0b0000000011111111);
}