-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMe7SegmentDisplay.cpp
826 lines (793 loc) · 18.7 KB
/
Me7SegmentDisplay.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
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
/**
* \par Copyright (C), 2012-2016, MakeBlock
* \class Me7SegmentDisplay
* \brief Driver for Me 7-Segment Serial Display module.
* @file Me7SegmentDisplay.cpp
* @author Frankie.Chu, MakeBlock
* @version V1.0.5
* @date 2016/07/27
* @brief Driver for Me 7 Segment Serial Display module.
*
* \par Copyright
* This software is Copyright (C), 2012-2018, MakeBlock. Use is subject to license \n
* conditions. The main licensing options available are GPL V2 or Commercial: \n
*
* \par Open Source Licensing GPL V2
* This is the appropriate option if you want to share the source code of your \n
* application with everyone you distribute it to, and you also want to give them \n
* the right to share who uses it. If you wish to use this software under Open \n
* Source Licensing, you must contribute all your source code to the open source \n
* community in accordance with the GPL Version 2 when your application is \n
* distributed. See http://www.gnu.org/copyleft/gpl.html
*
* \par Description
* Driver for Me 7 Segment Serial Display module.
* \par Method List:
*
* 1. void Me7SegmentDisplay::init(void);
* 2. void Me7SegmentDisplay::set(uint8_t brightness, uint8_t SetData, uint8_t SetAddr);
* 3. void Me7SegmentDisplay::reset(uint8_t port);
* 4. void Me7SegmentDisplay::setpin(uint8_t dataPin, uint8_t clkPin);
* 5. void Me7SegmentDisplay::write(uint8_t SegData[]);
* 6. void Me7SegmentDisplay::write(uint8_t BitAddr, uint8_t SegData);
* 7. void Me7SegmentDisplay::display(uint16_t value);
* 8. void Me7SegmentDisplay::display(int16_t value);
* 9. void Me7SegmentDisplay::display(float value);
* 10. void Me7SegmentDisplay::display(long value);
* 11. int16_t Me7SegmentDisplay::checkNum(float v,int16_t b);
* 12. void Me7SegmentDisplay::display(double value, uint8_t digits);
* 13. void Me7SegmentDisplay::display(uint8_t DispData[]);
* 14. void Me7SegmentDisplay::display(uint8_t BitAddr, uint8_t DispData);
* 15. void Me7SegmentDisplay::display(uint8_t BitAddr, uint8_t DispData, uint8_t point_on);
* 16. void Me7SegmentDisplay::clearDisplay(void);
* 17. void Me7SegmentDisplay::setBrightness(uint8_t brightness);
* 18. void Me7SegmentDisplay::coding(uint8_t DispData[]);
* 19. uint8_t Me7SegmentDisplay::coding(uint8_t DispData);
*
* \par History:
* <pre>
* `<Author>` `<Time>` `<Version>` `<Descr>`
* makeblock 2013/08/08 0.0.1 According to hardware differences, initial revision
* Mark Yan 2015/07/24 1.0.0 Rebuild the old lib.
* Rafael Lee 2015/09/02 1.0.1 Added some comments and macros. Some bug fixed in coding function.
* Mark Yan 2015/10/29 1.0.2 fix issue when display negative data.
* Mark Yan 2015/11/09 1.0.3 fix some comments error.
* Mark Yan 2015/11/12 1.0.4 fix driver API.
* Mark Yan 2016/07/27 1.0.5 add display to support long type.
* </pre>
*
* @example NumberDisplay.ino
* @example NumberFlow.ino
* @example TimeDisplay.ino
*
* DERIVED FROM
* // Author:Frankie.Chu
* // Date:9 April,2012
* //
* // This library is free software; you can redistribute it and/or
* // modify it under the terms of the GNU Lesser General Public
* // License as published by the Free Software Foundation; either
* // version 2.1 of the License, or (at your option) any later version.
* //
* // This library 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
* // Lesser General Public License for more details.
* //
* // You should have received a copy of the GNU Lesser General Public
* // License along with this library; if not, write to the Free Software
* // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/* Includes ------------------------------------------------------------------*/
#include "Me7SegmentDisplay.h"
/* Private variables ---------------------------------------------------------*/
const uint8_t TubeTab[] PROGMEM =
{
0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f, //0-9
0x77, 0x7c, 0x39, 0x5e, 0x79, 0x71, //'A', 'B', 'C', 'D', 'E', 'F',
0xbf, 0x86, 0xdb, 0xcf, 0xe6, 0xed, 0xfd, 0x87, 0xff, 0xef, //0.-9.
0xf7, 0xfc, 0xb9, 0xde, 0xf9, 0xf1, //'A.', 'B.', 'C.', 'D.', 'E.', 'F.',
0, 0x40 //' ','-'
};
#ifdef ME_PORT_DEFINED
/**
* Alternate Constructor which can call your own function to map the 7-Segment display to arduino port,
* no pins are used or initialized here.
* \param[in]
* None
*/
Me7SegmentDisplay::Me7SegmentDisplay(void) : MePort()
{
}
/**
* Alternate Constructor which can call your own function to map the 7-Segment display to arduino port,
* the slot1 will be used for data pin and slot2 will used for clk pin.
* \param[in]
* port - RJ25 port from PORT_1 to M2
*/
Me7SegmentDisplay::Me7SegmentDisplay(uint8_t port) : MePort(port)
{
_dataPin = s1;
_clkPin = s2;
pinMode(_clkPin, OUTPUT);
pinMode(_dataPin, OUTPUT);
set();
clearDisplay();
}
#else // ME_PORT_DEFINED
/**
* Alternate Constructor which can call your own function to map the 7-Segment display to arduino port.
* \param[in]
* dataPin - The DATA pin for Seven-Segment LED module(arduino port).
* \param[in]
* clkPin - The CLK pin for Seven-Segment LED module(arduino port).
*/
Me7SegmentDisplay::Me7SegmentDisplay(uint8_t dataPin, uint8_t clkPin)
{
_dataPin = dataPin;
_clkPin = clkPin;
pinMode(_clkPin, OUTPUT);
pinMode(_dataPin, OUTPUT);
set();
clearDisplay();
}
#endif // ME_PORT_DEFINED
#ifdef ME_PORT_DEFINED
/**
* \par Function
* reset
* \par Description
* Reset the port for Seven-Segment LED module, the prot here is RJ25 port from PORT_1 to M2.
* \param[in]
* port - RJ25 port from PORT_1 to M2
* \par Output
* None
* \par Return
* None
* \par Others
* Set global variable _dataPin and _clkPin
*/
void Me7SegmentDisplay::reset(uint8_t port)
{
_port = port;
s2 = mePort[port].s2;
s1 = mePort[port].s1;
_clkPin = s2;
_dataPin = s1;
pinMode(_clkPin, OUTPUT);
pinMode(_dataPin, OUTPUT);
set();
clearDisplay();
}
#endif // ME_PORT_DEFINED
/**
* \par Function
* setpin
* \par Description
* Reset Seven-Segment LED module's data pin and its clock pin then clear display.
* \param[in]
* dataPin - The DATA pin for Seven-Segment LED module(arduino port).
* \param[in]
* clkPin - The CLK pin for Seven-Segment LED module(arduino port).
* \par Output
* None
* \par Return
* None
* \par Others
* Set global variable _KeyPin and _clkPin
*/
void Me7SegmentDisplay::setpin(uint8_t dataPin, uint8_t clkPin)
{
_dataPin = dataPin;
_clkPin = clkPin;
pinMode(_clkPin, OUTPUT);
pinMode(_dataPin, OUTPUT);
#ifdef ME_PORT_DEFINED
s1 = dataPin;
s2 = clkPin;
#endif // ME_PORT_DEFINED
}
/**
* \Function
* clearDisplay
* \Description
* Clear display.
* \par Output
* None
* \par Return
* None
* \par Others
*/
void Me7SegmentDisplay::clearDisplay(void)
{
uint8_t buf[4] = { ' ', ' ', ' ', ' ' };
display(buf);
}
/**
* \par Function
* init
* \par Description
* Initialization the display, here just call function clearDisplay.
* \par Output
* None
* \return
* None
* \others
* None
*/
void Me7SegmentDisplay::init(void)
{
clearDisplay();
}
/**
* \par Function
* writeByte
* \par Description
* Write one byte to TM1637.
* \param[in]
* wr_data - Data to write to module.
* \par Output
* None
* \return
* None
* \others
* None
*/
void Me7SegmentDisplay::writeByte(uint8_t wr_data)
{
uint8_t i;
uint8_t cnt0;
for (i = 0; i < 8; i++) //sent 8bit data
{
digitalWrite(_clkPin, LOW);
if (wr_data & 0x01)
{
digitalWrite(_dataPin, HIGH); //LSB first
}
else
{
digitalWrite(_dataPin, LOW);
}
wr_data >>= 1;
digitalWrite(_clkPin, HIGH);
}
digitalWrite(_clkPin, LOW); //wait for ACK
digitalWrite(_dataPin, HIGH);
digitalWrite(_clkPin, HIGH);
pinMode(_dataPin, INPUT);
while (digitalRead(_dataPin))
{
cnt0 += 1;
if (cnt0 == 200)
{
pinMode(_dataPin, OUTPUT);
digitalWrite(_dataPin, LOW);
cnt0 = 0;
}
//pinMode(_dataPin,INPUT);
}
pinMode(_dataPin, OUTPUT);
}
/**
* \par Function
* start
* \par Description
* Send start signal to TM1637
* \param[in]
* None
* \par Output
* None
* \return
* None
* \others
* None
*/
void Me7SegmentDisplay::start(void)
{
digitalWrite(_clkPin, HIGH); //send start signal to TM1637
digitalWrite(_dataPin, HIGH);
digitalWrite(_dataPin, LOW);
digitalWrite(_clkPin, LOW);
}
/**
* \par Function
* stop
* \par Description
* Send the stop signal to TM1637.
* \param[in]
* None
* \par Output
* None
* \return
* None
* \others
* None
*/
void Me7SegmentDisplay::stop(void)
{
digitalWrite(_clkPin, LOW);
digitalWrite(_dataPin, LOW);
digitalWrite(_clkPin, HIGH);
digitalWrite(_dataPin, HIGH);
}
/**
* \par Function
* write
* \par Description
* White data array to certain address.
* \param[in]
* SegData[] - Data array to write to module.
* \par Output
* None
* \return
* None
* \par Others
* None
*/
void Me7SegmentDisplay::write(uint8_t SegData[])
{
uint8_t i;
start(); // Start signal sent to TM1637 from MCU.
writeByte(ADDR_AUTO);
stop();
start();
writeByte(Cmd_SetAddr);
for (i = 0; i < 4; i++)
{
writeByte(SegData[i]);
}
stop();
start();
writeByte(Cmd_DispCtrl);
stop();
}
/**
* \par Function
* write
* \par Description
* Write data to certain address.
* \param[in]
* BitAddr - Bit address of data.
* \param[in]
* SegData - Data to display.
* \par Output
* None
* \return
* None
* \others
* None
*/
void Me7SegmentDisplay::write(uint8_t BitAddr, uint8_t SegData)
{
start(); // start signal sent to TM1637 from MCU
writeByte(ADDR_FIXED);
stop();
start();
writeByte(BitAddr | STARTADDR);
writeByte(SegData);
stop();
start();
writeByte(Cmd_DispCtrl);
stop();
}
/**
* \par Function
* display
* \par Description
* Display certain value, and this value type is uint16_t
* \param[in]
* value - Value to display.
* \par Output
* None
* \return
* None
* \par Others
* None
*/
void Me7SegmentDisplay::display(uint16_t value)
{
display((int16_t)value);
}
/**
* \par Function
* display
* \par Description
* Display certain value, and this value type is int16_t
* \param[in]
* value - Value to display.
* \par Output
* None
* \return
* None
* \par Others
* None
*/
void Me7SegmentDisplay::display(int16_t value)
{
display((double)value, 0);
}
/**
* \par Function
* display
* \par Description
* Display certain value, and this value type is float
* \param[in]
* value - Value to display.
* \par Output
* None
* \return
* None
* \par Others
* None
*/
void Me7SegmentDisplay::display(float value)
{
uint8_t i=0;
bool isStart = false;
uint8_t index = 0;
uint8_t disp[]={0,0,0,0};
bool isNeg = false;
if((float)value<0)
{
isNeg = true;
value = -value;
disp[0] = 0x21;
index++;
}
for(i=0;i<7;i++)
{
int n = checkNum(value,3-i);
if(n>=1||i==3)
{
isStart=true;
}
if(isStart)
{
if(i==3)
{
disp[index]=n+0x10;
}
else
{
disp[index]=n;
}
index++;
}
if(index>3)
{
break;
}
}
display(disp);
}
/**
* \par Function
* display
* \par Description
* Display certain value, and this value type is long
* \param[in]
* value - Value to display.
* \par Output
* None
* \return
* None
* \par Others
* None
*/
void Me7SegmentDisplay::display(long value)
{
display((double)value, 0);
}
/**
* \par Function
* checkNum
* \par Description
* Extraction values to be displayed of float data
* \param[in]
* v - Value to display.
* \param[in]
* b - Value to display.
* \par Output
* None
* \return
* The data removal of the decimal point
* \par Others
* None
*/
int16_t Me7SegmentDisplay::checkNum(float v,int16_t b)
{
if(b>=0)
{
return floor((v-floor(v/pow(10,b+1))*(pow(10,b+1)))/pow(10,b));
}
else
{
b=-b;
int i=0;
for(i=0;i<b;i++)
{
v = v*10;
}
return ((int)(v)%10);
}
}
/**
* \par Function
* display
* \par Description
* Display double number.
* \param[in]
* value - Value to display.
* \param[in]
* digits - Number of digits to display.
* \par Output
* None
* \return
* None
* \par Others
* None
*/
void Me7SegmentDisplay::display(double value, uint8_t digits)
{
Posotion_1:
uint8_t buf[4] = { ' ', ' ', ' ', ' ' };
uint8_t tempBuf[4];
uint8_t b = 0;
uint8_t bit_num = 0;
uint8_t int_num = 0;
uint8_t isNeg = 0;
double number = value;
if (number >= 9999.5 || number <= -999.5)
{
buf[0] = ' ';
buf[1] = ' ';
buf[2] = ' ';
buf[3] = 0x0e;
}
else
{
// Handle negative numbers
if (number < 0.0)
{
number = -number;
isNeg = 1;
}
// Round correctly so that print(1.999, 2) prints as "2.00"
double rounding = 0.5;
for (uint8_t i = 0; i < digits; ++i)
{
rounding /= 10.0;
}
number += rounding;
// Extract the integer part of the number and print it
uint16_t int_part = (uint16_t)number;
double remainder = number - (double)int_part;
do
{
uint16_t m = int_part;
int_part /= 10;
int8_t c = m - 10 * int_part;
tempBuf[int_num] = c;
int_num++;
}
while (int_part);
bit_num = isNeg + int_num + digits;
if (bit_num > 4)
{
bit_num = 4;
digits = 4 - (isNeg + int_num);
goto Posotion_1;
}
b = 4 - bit_num;
if (isNeg)
{
buf[b++] = 0x21; // '-' display minus sign
}
for (uint8_t i = int_num; i > 0; i--)
{
buf[b++] = tempBuf[i - 1];
}
// Print the decimal point, but only if there are digits beyond
if (digits > 0)
{
buf[b - 1] += 0x10; // display '.'
// Extract digits from the remainder one at a time
while (digits-- > 0)
{
remainder *= 10.0;
int16_t toPrint = int16_t(remainder);
buf[b++] = toPrint;
remainder -= toPrint;
}
}
}
display(buf);
}
/**
* \par Function
* display
* \par Description
* Display 8 bit number array.
* \param[in]
* DispData[] - The data that needs to be displayed store in this array.
* \par Output
* None
* \return
* None
* \par Others
* None
*/
void Me7SegmentDisplay::display(uint8_t DispData[])
{
uint8_t SegData[4];
uint8_t i;
for (i = 0; i < 4; i++)
{
SegData[i] = DispData[i];
}
coding(SegData);
write(SegData);
}
/**
* \par Function
* display
* \par Description
* Display data to certain digit.
* \param[in]
* BitAddr - Address to display.
* \param[in]
* DispData - Data to display.
* \par Output
* None
* \return
* None
* \par Others
* None
*/
void Me7SegmentDisplay::display(uint8_t BitAddr, uint8_t DispData)
{
uint8_t SegData;
if ((DispData >= 'A' && DispData <= 'F'))
{
DispData = DispData - 'A' + 10;
}
else if ((DispData >= 'a' && DispData <= 'f'))
{
DispData = DispData - 'a' + 10;
}
SegData = coding(DispData);
write(BitAddr, SegData);
}
/**
* \par Function
* display
* \par Description
* Display data to certain digit.
* \param[in]
* BitAddr - Address to display.
* \param[in]
* DispData - Data to display.
* \param[in]
* point_on - Display the point or not.
* \par Output
* None
* \return
* None
* \par Others
* None
*/
void Me7SegmentDisplay::display(uint8_t BitAddr, uint8_t DispData, uint8_t point_on)
{
uint8_t SegData;
if ((DispData >= 'A' && DispData <= 'F'))
{
DispData = DispData - 'A' + 10;
}
else if ((DispData >= 'a' && DispData <= 'f'))
{
DispData = DispData - 'a' + 10;
}
if(point_on == POINT_ON )
{
SegData = coding(DispData+0x10);
}
else
{
SegData = coding(DispData);
}
write(BitAddr, SegData);
}
/**
* \par Function
* set
* \par Description
* Set brightness, data and address.
* \param[in]
* brightness - Brightness, defined in Me7SegmentDisplay.h from BRIGHTNESS_0 to BRIGHTNESS_7.
* \param[in]
* SetData - First address for display data
* \param[in]
* SetAddr - First address for display
* \par Output
* Cmd_SetData - Private variable Cmd_SetData of class Me7SegmentDisplay.
* Cmd_SetAddr - Private variable Cmd_SetAddr of class Me7SegmentDisplay.
* Cmd_DispCtrl - Control command for Me 7 Segment Serial Display module.
* \return
* None
* \par Others
* None
*/
void Me7SegmentDisplay::set(uint8_t brightness, uint8_t SetData, uint8_t SetAddr)
{
Cmd_SetData = SetData;
Cmd_SetAddr = SetAddr;
Cmd_DispCtrl = SEGDIS_ON + brightness;//Set brightness, take effect next display cycle.
}
/**
* \par Function
* setBrightness
* \par Description
* Set brightness.
* \param[in]
* brightness - Brightness, defined in Me7SegmentDisplay.h from BRIGHTNESS_0 to BRIGHTNESS_7.
* \par Output
* Cmd_DispCtrl - Control command for Me 7 Segment Serial Display module.
* \return
* None
* \par Others
* None
*/
void Me7SegmentDisplay::setBrightness(uint8_t brightness)
{
Cmd_DispCtrl = SEGDIS_ON + brightness;
}
/**
* \par Function
* coding
* \par Description
* Set display data using look up table.
* \param[in]
* DispData[] - DataArray to display.
* \par Output
* DispData[] - DataArray be transcoded.
* \return
* None
* \par Others
* None
*/
void Me7SegmentDisplay::coding(uint8_t DispData[])
{
for (uint8_t i = 0; i < 4; i++)
{
if (DispData[i] >= sizeof(TubeTab) / sizeof(*TubeTab))
{
DispData[i] = 32; // Change to ' '(space)
}
//DispData[i] = TubeTab[DispData[i]];
DispData[i] = pgm_read_byte(&TubeTab[DispData[i]]);//+ PointData;
}
}
/**
* \par Function
* coding
* \par Description
* Return display data from look up table.
* \param[in]
* DispData - Data need be transcoded.
* \par Output
* None
* \return
* Return the value of transcoding
* \par Others
* None
*/
uint8_t Me7SegmentDisplay::coding(uint8_t DispData)
{
if (DispData >= sizeof(TubeTab) / sizeof(*TubeTab))
{
DispData = 32; // Change to ' '(space)
}
//DispData = TubeTab[DispData];//+ PointData;
DispData = pgm_read_byte(&TubeTab[DispData]);//+ PointData;
return DispData;
}