1
+ // Licensed to the .NET Foundation under one or more agreements.
2
+ // The .NET Foundation licenses this file to you under the MIT license.
3
+ // See the LICENSE file in the project root for more information.
4
+
5
+ using System ;
6
+ using System . Collections . Generic ;
7
+ using System . Linq ;
8
+ using System . Threading ;
9
+
10
+ namespace Iot . Device . Max7219
11
+ {
12
+
13
+ /// <summary>
14
+ /// Graphical functions for a MAX7219 device
15
+ /// </summary>
16
+ public class MatrixGraphics
17
+ {
18
+ readonly Max7219 _device ;
19
+
20
+ public MatrixGraphics ( Max7219 device , IFont font )
21
+ {
22
+ if ( device == null )
23
+ throw new ArgumentNullException ( nameof ( device ) ) ;
24
+ if ( font == null )
25
+ throw new ArgumentNullException ( nameof ( font ) ) ;
26
+ _device = device ;
27
+ Font = font ;
28
+ }
29
+
30
+ public IFont Font { get ; set ; }
31
+
32
+ /// <summary>
33
+ /// Writes a char to the given device with the specified font.
34
+ /// </summary>
35
+ public void WriteLetter ( int deviceId , char chr , bool flush = true )
36
+ {
37
+ var charBytes = Font [ chr ] ;
38
+ var end = Math . Min ( charBytes . Count , Max7219 . NumDigits ) ;
39
+ for ( int col = 0 ; col < end ; col ++ )
40
+ {
41
+ _device [ deviceId , col ] = charBytes [ col ] ;
42
+ }
43
+ if ( flush )
44
+ {
45
+ _device . Flush ( ) ;
46
+ }
47
+ }
48
+
49
+ /// <summary>
50
+ /// Scrolls the underlying buffer (for all cascaded devices) up one pixel
51
+ /// </summary>
52
+ public void ScrollUp ( bool flush = true )
53
+ {
54
+ for ( var i = 0 ; i < _device . Length ; i ++ )
55
+ _device [ i ] = ( byte ) ( _device [ i ] >> 1 ) ;
56
+ if ( flush )
57
+ {
58
+ _device . Flush ( ) ;
59
+ }
60
+ }
61
+
62
+ /// <summary>
63
+ /// Scrolls the underlying buffer (for all cascaded devices) down one pixel
64
+ /// </summary>
65
+ public void ScrollDown ( bool flush = true )
66
+ {
67
+ for ( var i = 0 ; i < _device . Length ; i ++ )
68
+ {
69
+ _device [ i ] = ( byte ) ( ( _device [ i ] << 1 ) & 0xff ) ;
70
+ }
71
+ if ( flush )
72
+ {
73
+ _device . Flush ( ) ;
74
+ }
75
+ }
76
+
77
+ /// <summary>
78
+ /// Scrolls the underlying buffer (for all cascaded devices) to the left
79
+ /// </summary>
80
+ public void ScrollLeft ( byte value , bool flush = true )
81
+ {
82
+ for ( var i = 1 ; i < _device . Length ; i ++ )
83
+ {
84
+ _device [ i - 1 ] = _device [ i ] ;
85
+ }
86
+ _device [ _device . Length - 1 ] = value ;
87
+ if ( flush )
88
+ {
89
+ _device . Flush ( ) ;
90
+ }
91
+ }
92
+
93
+ /// <summary>
94
+ /// Scrolls the underlying buffer (for all cascaded devices) to the right
95
+ /// </summary>
96
+ public void ScrollRight ( byte value , bool flush = true )
97
+ {
98
+ for ( var i = _device . Length - 1 ; i > 0 ; i -- )
99
+ {
100
+ _device [ i ] = _device [ i - 1 ] ;
101
+ }
102
+ _device [ 0 ] = value ;
103
+ if ( flush )
104
+ {
105
+ _device . Flush ( ) ;
106
+ }
107
+ }
108
+
109
+ /// <summary>
110
+ /// Shows a message on the device.
111
+ /// If it's longer then the total width (or <see paramref="alwaysScroll"/> == true),
112
+ /// it transitions the text message across the devices from right-to-left.
113
+ /// </summary>
114
+ public void ShowMessage ( string text , int delayInMilliseconds = 50 , bool alwaysScroll = false )
115
+ {
116
+ IEnumerable < IReadOnlyList < byte > > textCharBytes = text . Select ( chr => Font [ chr ] ) ;
117
+ int textBytesLength = textCharBytes . Sum ( x => x . Count ) + text . Length - 1 ;
118
+
119
+ bool scroll = alwaysScroll || textBytesLength > _device . Length ;
120
+ if ( scroll )
121
+ {
122
+ var pos = _device . Length - 1 ;
123
+ _device . ClearAll ( false ) ;
124
+ foreach ( var arr in textCharBytes )
125
+ {
126
+ foreach ( byte b in arr )
127
+ {
128
+ ScrollLeft ( b , true ) ;
129
+ Thread . Sleep ( delayInMilliseconds ) ;
130
+
131
+ }
132
+ ScrollLeft ( 0 , true ) ;
133
+ Thread . Sleep ( delayInMilliseconds ) ;
134
+
135
+ }
136
+ for ( ; pos > 0 ; pos -- )
137
+ {
138
+ ScrollLeft ( 0 , true ) ;
139
+ Thread . Sleep ( delayInMilliseconds ) ;
140
+ }
141
+ }
142
+ else
143
+ {
144
+ //calculate margin to display text centered
145
+ var margin = ( _device . Length - textBytesLength ) / 2 ;
146
+ _device . ClearAll ( false ) ;
147
+ var pos = margin ;
148
+ foreach ( var arr in textCharBytes )
149
+ {
150
+ foreach ( byte b in arr )
151
+ {
152
+ _device [ pos ++ ] = b ;
153
+ }
154
+ pos ++ ;
155
+ }
156
+ _device . Flush ( ) ;
157
+ }
158
+ }
159
+ }
160
+ }
0 commit comments