-
Notifications
You must be signed in to change notification settings - Fork 0
/
vfd.py
75 lines (58 loc) · 2.15 KB
/
vfd.py
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
"""VFD Lib"""
from time import sleep
import spidev
from .const import *
class VFD:
def __init__(self, spi_num, spi_ce):
self.spi = spidev.SpiDev()
self.spi.open(spi_num, spi_ce)
self.spi.mode = 3
self.spi.max_speed_hz = 500000
def write(self, data, rs):
"""Send data."""
if rs:
self.spi.writebytes([VFD_SPIDATA, data])
else:
self.spi.writebytes([VFD_SPICOMMAND, data])
sleep(0.00001)
def command(self, char):
self.write(char, False)
def text(self, strings):
"""Write text."""
for char in strings:
self.write(ord(char), True)
def home(self):
"""Home the cursor."""
self.command(VFD_RETURNHOME)
sleep(VFD_SLEEPTIME)
def clear(self):
"""Clear the display."""
self.command(VFD_CLEARDISPLAY)
sleep(VFD_SLEEPTIME)
def setCursor(self, col, row):
"""Set the cursor to specific cell."""
_numlines = ROWS
row_offsets = [0x00, 0x40, 0x14, 0x54]
if row > _numlines:
row = _numlines - 1
self.command(VFD_SETDDRAMADDR | (col + row_offsets[row]))
sleep(VFD_SLEEPTIME)
def display(self, _displaycontrol):
_displaycontrol |= self.VFD_DISPLAYON
self.command(VFD_DISPLAYCONTROL | _displaycontrol)
def blink_on(self):
_displaycontrol = VFD_DISPLAYON | VFD_CURSORON | VFD_BLINKON
self.display(_displaycontrol)
def blink_off(self):
_displaycontrol = VFD_DISPLAYON | VFD_CURSOROFF | VFD_BLINKOFF
self.display(_displaycontrol)
def scrollDisplayLeft(self):
self.command(VFD_CURSORSHIFT | VFD_DISPLAYMOVE | VFD_MOVELEFT)
def scrollDisplayRight(self):
self.command(VFD_CURSORSHIFT | VFD_DISPLAYMOVE | VFD_MOVERIGHT)
def autoscroll(self):
self._displaymode |= VFD_ENTRYSHIFTINCREMENT
self.command(VFD_ENTRYMODESET | self._displaymode)
def noAutoscroll(self):
self._displaymode &= ~VFD_ENTRYSHIFTINCREMENT
self.command(VFD_ENTRYMODESET | self._displaymode)