-
Notifications
You must be signed in to change notification settings - Fork 0
/
DeviceFamilyFF.cs
173 lines (132 loc) · 4.66 KB
/
DeviceFamilyFF.cs
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
namespace OneWireAPI
{
// ReSharper disable once InconsistentNaming
public class DeviceFamilyFF : Device
{
private int _width = 20;
private int _height = 4;
public DeviceFamilyFF(Session session, short[] id)
: base(session, id)
{
// Just call the base constructor
}
public void SetSize(int width, int height)
{
_width = width;
_height = height;
}
public void SetBackLight(bool state)
{
// Select the device
Adapter.Select(Id);
// Set the state of the backlight
Adapter.SendByte((short) (state ? 0x8 : 0x7));
}
public void SetText(string text)
{
// Line number
var lineNumber = 1;
// Replace any CRLF pairs with just a newline
text = text.Replace("\r\n", "\n");
// Split the input string at any newlines
var lines = text.Split("\n".ToCharArray(), _height);
// Loop over each line
foreach (var line in lines)
{
// Set the text of this line
SetText(line, lineNumber++);
}
}
public void SetText(string text, int line)
{
// Position at which to write the new string data
short memoryPosition = 0x00;
// String data to send
string sendData;
// Byte array of data to send
byte[] data;
// Amount of data to send
short dataCount = 0;
// Figure out the initial memory position based on the line number
switch (line)
{
case 1:
memoryPosition = 0x00;
break;
case 2:
memoryPosition = 0x40;
break;
case 3:
memoryPosition = 0x14;
break;
case 4:
memoryPosition = 0x54;
break;
}
// Pad the text to the right width
text = text.PadRight(_width);
// The scratchpad is only 16 bytes long so we need to split it up
if (_width > 16)
{
// Select the device
Adapter.Select(Id);
// Set the data block to just the first 16 characters
sendData = text.Substring(0, 16);
// Initialize the data array
data = new byte[18];
// Set the command to write to the scratchpad
data[dataCount++] = 0x4E;
// Set the memory position
data[dataCount++] = (byte) memoryPosition;
// Add the text data to the data
foreach (var bChar in System.Text.Encoding.Default.GetBytes(sendData))
data[dataCount++] = bChar;
// Set the block
Adapter.SendBlock(data, dataCount);
// Select the device
Adapter.Select(Id);
// Send the scratchpad data to the LCD
Adapter.SendByte(0x48);
// Reset the device
Adapter.Reset();
// Increment the memory position
memoryPosition += 16;
// Set the data to the rest of the line
sendData = text.Substring(16, _width - 16);
}
else
{
// Just set the data string to whatever was passed in
sendData = text;
}
// Select the device
Adapter.Select(Id);
// Initialize the data array
data = new byte[18];
// Reset the data count
dataCount = 0;
// Set the command to write to the scratchpad
data[dataCount++] = 0x4E;
// Set the memory position
data[dataCount++] = (byte) memoryPosition;
// Add the text data to the data
foreach (var bChar in System.Text.Encoding.Default.GetBytes(sendData))
data[dataCount++] = bChar;
// Set the block
Adapter.SendBlock(data, dataCount);
// Select the device
Adapter.Select(Id);
// Send the scratchpad data to the LCD
Adapter.SendByte(0x48);
// Reset the device
Adapter.Reset();
}
public void Clear()
{
// Select the device
Adapter.Select(Id);
// Clear the display
Adapter.SendByte(0x49);
}
}
}