Skip to content

Commit 50c40d1

Browse files
committed
initial_commit
1 parent 557adf0 commit 50c40d1

7 files changed

+1007
-0
lines changed

README.md

+15
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,17 @@
11
# ESP8266
22
Code deployed to the ESP8266 Controller
3+
[Wiki](https://wiki.varnalab.org/index.php?title=Arduino_%D0%B8_%D0%BC%D0%B8%D0%BA%D1%80%D0%BE%D0%BA%D0%BE%D0%BD%D1%82%D1%80%D0%BE%D0%BB%D0%B5%D1%80%D0%B8#ESP8266)
4+
5+
Source:
6+
<li>
7+
<ul>test_ttn.py <- MQTT client implementation </ul>
8+
<ul>scroller.py <- scrolling API used to visualize text data to the screen</ul>
9+
</li>
10+
Needed libraries:
11+
<li>
12+
<ul>config.py</ul>
13+
<ul>mqtt_as.py</ul>
14+
<ul>lcd_api.py</ul>
15+
<ul>pyb_i2c_lcd.py</ul>
16+
</li>
17+

config.py

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from mqtt_as import config
2+
from sys import platform
3+
4+
# Include any cross-project settings.
5+
6+
if platform == 'esp32':
7+
config['ssid'] = 'my SSID' # EDIT if you're using ESP32
8+
config['wifi_pw'] = 'my WiFi password'

lcd_api.py

+195
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
"""Provides an API for talking to HD44780 compatible character LCDs."""
2+
3+
import time
4+
5+
class LcdApi:
6+
"""Implements the API for talking with HD44780 compatible character LCDs.
7+
This class only knows what commands to send to the LCD, and not how to get
8+
them to the LCD.
9+
10+
It is expected that a derived class will implement the hal_xxx functions.
11+
"""
12+
13+
# The following constant names were lifted from the avrlib lcd.h
14+
# header file, however, I changed the definitions from bit numbers
15+
# to bit masks.
16+
#
17+
# HD44780 LCD controller command set
18+
19+
LCD_CLR = 0x01 # DB0: clear display
20+
LCD_HOME = 0x02 # DB1: return to home position
21+
22+
LCD_ENTRY_MODE = 0x04 # DB2: set entry mode
23+
LCD_ENTRY_INC = 0x02 # --DB1: increment
24+
LCD_ENTRY_SHIFT = 0x01 # --DB0: shift
25+
26+
LCD_ON_CTRL = 0x08 # DB3: turn lcd/cursor on
27+
LCD_ON_DISPLAY = 0x04 # --DB2: turn display on
28+
LCD_ON_CURSOR = 0x02 # --DB1: turn cursor on
29+
LCD_ON_BLINK = 0x01 # --DB0: blinking cursor
30+
31+
LCD_MOVE = 0x10 # DB4: move cursor/display
32+
LCD_MOVE_DISP = 0x08 # --DB3: move display (0-> move cursor)
33+
LCD_MOVE_RIGHT = 0x04 # --DB2: move right (0-> left)
34+
35+
LCD_FUNCTION = 0x20 # DB5: function set
36+
LCD_FUNCTION_8BIT = 0x10 # --DB4: set 8BIT mode (0->4BIT mode)
37+
LCD_FUNCTION_2LINES = 0x08 # --DB3: two lines (0->one line)
38+
LCD_FUNCTION_10DOTS = 0x04 # --DB2: 5x10 font (0->5x7 font)
39+
LCD_FUNCTION_RESET = 0x30 # See "Initializing by Instruction" section
40+
41+
LCD_CGRAM = 0x40 # DB6: set CG RAM address
42+
LCD_DDRAM = 0x80 # DB7: set DD RAM address
43+
44+
LCD_RS_CMD = 0
45+
LCD_RS_DATA = 1
46+
47+
LCD_RW_WRITE = 0
48+
LCD_RW_READ = 1
49+
50+
def __init__(self, num_lines, num_columns):
51+
self.num_lines = num_lines
52+
if self.num_lines > 4:
53+
self.num_lines = 4
54+
self.num_columns = num_columns
55+
if self.num_columns > 40:
56+
self.num_columns = 40
57+
self.cursor_x = 0
58+
self.cursor_y = 0
59+
self.backlight = True
60+
self.display_off()
61+
self.backlight_on()
62+
self.clear()
63+
self.hal_write_command(self.LCD_ENTRY_MODE | self.LCD_ENTRY_INC)
64+
self.hide_cursor()
65+
self.display_on()
66+
67+
def clear(self):
68+
"""Clears the LCD display and moves the cursor to the top left
69+
corner.
70+
"""
71+
self.hal_write_command(self.LCD_CLR)
72+
self.hal_write_command(self.LCD_HOME)
73+
self.cursor_x = 0
74+
self.cursor_y = 0
75+
76+
def show_cursor(self):
77+
"""Causes the cursor to be made visible."""
78+
self.hal_write_command(self.LCD_ON_CTRL | self.LCD_ON_DISPLAY |
79+
self.LCD_ON_CURSOR)
80+
81+
def hide_cursor(self):
82+
"""Causes the cursor to be hidden."""
83+
self.hal_write_command(self.LCD_ON_CTRL | self.LCD_ON_DISPLAY)
84+
85+
def blink_cursor_on(self):
86+
"""Turns on the cursor, and makes it blink."""
87+
self.hal_write_command(self.LCD_ON_CTRL | self.LCD_ON_DISPLAY |
88+
self.LCD_ON_CURSOR | self.LCD_ON_BLINK)
89+
90+
def blink_cursor_off(self):
91+
"""Turns on the cursor, and makes it no blink (i.e. be solid)."""
92+
self.hal_write_command(self.LCD_ON_CTRL | self.LCD_ON_DISPLAY |
93+
self.LCD_ON_CURSOR)
94+
95+
def display_on(self):
96+
"""Turns on (i.e. unblanks) the LCD."""
97+
self.hal_write_command(self.LCD_ON_CTRL | self.LCD_ON_DISPLAY)
98+
99+
def display_off(self):
100+
"""Turns off (i.e. blanks) the LCD."""
101+
self.hal_write_command(self.LCD_ON_CTRL)
102+
103+
def backlight_on(self):
104+
"""Turns the backlight on.
105+
106+
This isn't really an LCD command, but some modules have backlight
107+
controls, so this allows the hal to pass through the command.
108+
"""
109+
self.backlight = True
110+
self.hal_backlight_on()
111+
112+
def backlight_off(self):
113+
"""Turns the backlight off.
114+
115+
This isn't really an LCD command, but some modules have backlight
116+
controls, so this allows the hal to pass through the command.
117+
"""
118+
self.backlight = False
119+
self.hal_backlight_off()
120+
121+
def move_to(self, cursor_x, cursor_y):
122+
"""Moves the cursor position to the indicated position. The cursor
123+
position is zero based (i.e. cursor_x == 0 indicates first column).
124+
"""
125+
self.cursor_x = cursor_x
126+
self.cursor_y = cursor_y
127+
addr = cursor_x & 0x3f
128+
if cursor_y & 1:
129+
addr += 0x40 # Lines 1 & 3 add 0x40
130+
if cursor_y & 2:
131+
addr += 0x14 # Lines 2 & 3 add 0x14
132+
self.hal_write_command(self.LCD_DDRAM | addr)
133+
134+
def putchar(self, char):
135+
"""Writes the indicated character to the LCD at the current cursor
136+
position, and advances the cursor by one position.
137+
"""
138+
if char != '\n':
139+
self.hal_write_data(ord(char))
140+
self.cursor_x += 1
141+
if self.cursor_x >= self.num_columns or char == '\n':
142+
self.cursor_x = 0
143+
self.cursor_y += 1
144+
if self.cursor_y >= self.num_lines:
145+
self.cursor_y = 0
146+
self.move_to(self.cursor_x, self.cursor_y)
147+
148+
def putstr(self, string):
149+
"""Write the indicated string to the LCD at the current cursor
150+
position and advances the cursor position appropriately.
151+
"""
152+
for char in string:
153+
self.putchar(char)
154+
155+
def custom_char(self, location, charmap):
156+
"""Write a character to one of the 8 CGRAM locations, available
157+
as chr(0) through chr(7).
158+
"""
159+
location &= 0x7
160+
self.hal_write_command(self.LCD_CGRAM | (location << 3))
161+
time.sleep_us(40)
162+
for i in range(8):
163+
self.hal_write_data(charmap[i])
164+
time.sleep_us(40)
165+
self.move_to(self.cursor_x, self.cursor_y)
166+
167+
def hal_backlight_on(self):
168+
"""Allows the hal layer to turn the backlight on.
169+
170+
If desired, a derived HAL class will implement this function.
171+
"""
172+
pass
173+
174+
def hal_backlight_off(self):
175+
"""Allows the hal layer to turn the backlight off.
176+
177+
If desired, a derived HAL class will implement this function.
178+
"""
179+
pass
180+
181+
def hal_write_command(self, cmd):
182+
"""Write a command to the LCD.
183+
184+
It is expected that a derived HAL class will implement this
185+
function.
186+
"""
187+
raise NotImplementedError
188+
189+
def hal_write_data(self, data):
190+
"""Write data to the LCD.
191+
192+
It is expected that a derived HAL class will implement this
193+
function.
194+
"""
195+
raise NotImplementedError

0 commit comments

Comments
 (0)