-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLcdScreen.cs
More file actions
117 lines (101 loc) · 3.95 KB
/
LcdScreen.cs
File metadata and controls
117 lines (101 loc) · 3.95 KB
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
using System;
using System.Drawing;
using Unosquare.RaspberryIO.Abstractions;
using Unosquare.WiringPi;
namespace Celones.DisplayManager
{
class LcdScreen : ICanvas
{
private Device.Pcd8544 _ctl;
private GpioPin _bl;
private double _brightness, _contrast;
private Bitmap _img;
public LcdScreen(Device.Pcd8544 controller, GpioPin backlight = null)
{
_ctl = controller;
_bl = backlight;
_img = new Bitmap(Width, Height);
Graphics = Graphics.FromImage(_img);
}
public void Init()
{
_ctl.Init();
if (_bl != null)
{
_bl.PinMode = GpioPinDriveMode.PwmOutput;
_bl.PwmMode = PwmMode.Balanced;
_bl.PwmClockDivisor = 2;
}
Brightness = 1.0;
Contrast = 1.0;
}
public void Clear()
{
for (int index = 0; index < _ctl.DramSizeX * _ctl.DramSizeY; index++)
{
_ctl.Write(0x00);
}
for (int x = 0; x < Width; x++)
{
for (int y = 0; y < Height; y++)
{
_img.SetPixel(x, y, Color.White);
}
}
}
public Graphics Graphics { get; }
public int Width { get => _ctl.DramSizeX; }
public int Height { get => _ctl.DramSizeY * 8; }
public double Brightness
{
get { return _brightness; }
set
{
if (_bl != null)
{
_brightness = Math.Min(Math.Max(value, 0.0), 1.0);
_bl.PwmRegister = (int)(_brightness * _bl.PwmRange);
}
}
}
public double Contrast
{
get { return _contrast; }
set
{
_contrast = Math.Min(Math.Max(value, 0.0), 1.0);
_ctl.Write(Device.Pcd8544.Instruction.SetOperationMode(instructionSet: Device.Pcd8544.InstructionSet.Extended));
_ctl.Write(Device.Pcd8544.Instruction.SetOperationVoltage((int)(60 * _contrast)));
_ctl.Write(Device.Pcd8544.Instruction.SetOperationMode(instructionSet: Device.Pcd8544.InstructionSet.Basic));
}
}
public void Update()
{
Update(new Rectangle(0, 0, Width, Height));
}
public void Update(Rectangle rect)
{
rect.Intersect(new Rectangle(0, 0, Width, Height));
int yFirst = rect.Top / 8;
int yLast = (int)Math.Ceiling((double)(rect.Top + rect.Height) / 8.0);
for (int y = yFirst; y < yLast; y++)
{
_ctl.Write(Device.Pcd8544.Instruction.SetYAddress(y));
_ctl.Write(Device.Pcd8544.Instruction.SetXAddress(rect.Left));
for (int i = 0; i < rect.Width; i++)
{
int data = 0;
data |= _img.GetPixel(rect.Left + i, y * 8 + 0).GetBrightness() < 0.5 ? 1 : 0;
data |= _img.GetPixel(rect.Left + i, y * 8 + 1).GetBrightness() < 0.5 ? 2 : 0;
data |= _img.GetPixel(rect.Left + i, y * 8 + 2).GetBrightness() < 0.5 ? 4 : 0;
data |= _img.GetPixel(rect.Left + i, y * 8 + 3).GetBrightness() < 0.5 ? 8 : 0;
data |= _img.GetPixel(rect.Left + i, y * 8 + 4).GetBrightness() < 0.5 ? 16 : 0;
data |= _img.GetPixel(rect.Left + i, y * 8 + 5).GetBrightness() < 0.5 ? 32 : 0;
data |= _img.GetPixel(rect.Left + i, y * 8 + 6).GetBrightness() < 0.5 ? 64 : 0;
data |= _img.GetPixel(rect.Left + i, y * 8 + 7).GetBrightness() < 0.5 ? 128 : 0;
_ctl.Write((byte)data);
}
}
}
}
}