Skip to content

Commit 4602d71

Browse files
authored
Merge pull request #33 from makermelissa/master
General improvements to help with init, touch, and comments
2 parents 1b8377c + 354fc17 commit 4602d71

File tree

3 files changed

+59
-33
lines changed

3 files changed

+59
-33
lines changed

adafruit_ra8875/ra8875.py

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import struct
3131
import time
3232

33-
from digitalio import Direction
3433
from adafruit_bus_device import spi_device
3534
import adafruit_ra8875.registers as reg
3635

@@ -45,7 +44,7 @@
4544
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_RA8875.git"
4645

4746

48-
# pylint: disable-msg=invalid-name
47+
# pylint: disable-msg=invalid-name, too-many-statements
4948
def color565(r: int, g: int = 0, b: int = 0) -> int:
5049
"""Convert red, green and blue values (0-255) into a 16-bit 565 encoding."""
5150
try:
@@ -102,7 +101,7 @@ def __init__(
102101
self.reset()
103102
if self._read_reg(0) == 0x75:
104103
return
105-
self._adc_clk = reg.TPCR0_ADCCLK_DIV16
104+
self._adc_clk = reg.TPCR0_ADCCLK_DIV4
106105

107106
# pylint: enable-msg=invalid-name,too-many-arguments
108107

@@ -112,6 +111,7 @@ def init(self, start_on: bool = True) -> None:
112111
113112
:param bool start_on: (optional) If the display should start in an On State (default=True)
114113
"""
114+
hsync_finetune = 0
115115
if self.width == 480 and self.height == 82:
116116
self.vert_offset = 190
117117

@@ -123,6 +123,7 @@ def init(self, start_on: bool = True) -> None:
123123
vsync_nondisp = 32
124124
vsync_start = 23
125125
vsync_pw = 2
126+
self._adc_clk = reg.TPCR0_ADCCLK_DIV16
126127
elif self.width == 480 and self.height in (272, 128, 82):
127128
pixclk = reg.PCSR_PDATL | reg.PCSR_4CLK
128129
hsync_nondisp = 10
@@ -131,36 +132,41 @@ def init(self, start_on: bool = True) -> None:
131132
vsync_nondisp = 3
132133
vsync_start = 8
133134
vsync_pw = 10
134-
self._adc_clk = reg.TPCR0_ADCCLK_DIV4
135135
else:
136136
raise ValueError("An invalid display size was specified.")
137137

138138
self.pllinit()
139-
140139
self._write_reg(reg.SYSR, reg.SYSR_16BPP | reg.SYSR_MCU8)
141140
self._write_reg(reg.PCSR, pixclk)
142141
time.sleep(0.001)
143142

144143
# Horizontal settings registers
145144
self._write_reg(reg.HDWR, self.width // 8 - 1)
146-
self._write_reg(reg.HNDFTR, reg.HNDFTR_DE_HIGH)
147-
self._write_reg(reg.HNDR, (hsync_nondisp - 2) // 8)
145+
self._write_reg(reg.HNDFTR, reg.HNDFTR_DE_HIGH + hsync_finetune)
146+
self._write_reg(reg.HNDR, (hsync_nondisp - hsync_finetune - 2) // 8)
148147
self._write_reg(reg.HSTR, hsync_start // 8 - 1)
149-
self._write_reg(reg.HPWR, reg.HPWR_LOW + hsync_pw // 8 - 1)
148+
self._write_reg(reg.HPWR, reg.HPWR_LOW + (hsync_pw // 8 - 1))
150149

151150
# Vertical settings registers
152-
self._write_reg16(reg.VDHR0, self.height - 1 + self.vert_offset)
151+
self._write_reg16(reg.VDHR0, (self.height - 1 + self.vert_offset) & 0xFF)
152+
self._write_reg16(reg.VDHR1, (self.height - 1 + self.vert_offset) >> 8)
153153
self._write_reg16(reg.VNDR0, vsync_nondisp - 1)
154+
self._write_reg16(reg.VNDR1, vsync_nondisp >> 8)
154155
self._write_reg16(reg.VSTR0, vsync_start - 1)
156+
self._write_reg16(reg.VSTR1, vsync_start >> 8)
155157
self._write_reg(reg.VPWR, reg.VPWR_LOW + vsync_pw - 1)
156158

157159
# Set active window X
158160
self._write_reg16(reg.HSAW0, 0)
159-
self._write_reg16(reg.HEAW0, self.width - 1)
161+
self._write_reg16(reg.HSAW1, 0)
162+
self._write_reg16(reg.HEAW0, (self.width - 1) & 0xFF)
163+
self._write_reg16(reg.HEAW1, (self.width - 1) >> 8)
160164

161165
# Set active window Y
162166
self._write_reg16(reg.VSAW0, self.vert_offset)
163-
self._write_reg16(reg.VEAW0, self.height - 1 + self.vert_offset)
167+
self._write_reg16(reg.VSAW1, self.vert_offset)
168+
self._write_reg16(reg.VEAW0, (self.height - 1 + self.vert_offset) & 0xFF)
169+
self._write_reg16(reg.VEAW1, (self.height - 1 + self.vert_offset) >> 8)
164170

165171
# Clear the entire window
166172
self._write_reg(reg.MCLR, reg.MCLR_START | reg.MCLR_FULL)
@@ -216,7 +222,7 @@ def _write_cmd(self, cmd: int) -> None:
216222

217223
def _write_data(self, data: int, raw: bool = False) -> None:
218224
"""
219-
Write a byte or push raw data out
225+
Write a byte or push raw data out using the previously selected register
220226
221227
:param data: The byte to write to the register
222228
:type data: byte or bytearray
@@ -341,9 +347,8 @@ def touch_init(
341347
:param bool enable: Enable the Touch Functionality as well
342348
"""
343349
if tpin is not None:
344-
tpin.direction = Direction.INPUT
350+
tpin.switch_to_input()
345351
self._tpin = tpin
346-
self._write_reg(reg.INTC2, reg.INTC2_TP)
347352
self.touch_enable(enable)
348353

349354
def touch_enable(self, touch_on: bool) -> None:
@@ -356,12 +361,13 @@ def touch_enable(self, touch_on: bool) -> None:
356361
self._write_reg(
357362
reg.TPCR0,
358363
reg.TPCR0_ENABLE
359-
| reg.TPCR0_WAIT_4096CLK
364+
| reg.WAITTIME_LUT[self._adc_clk]
360365
| reg.TPCR0_WAKEENABLE
361366
| self._adc_clk,
362367
)
363368
self._write_reg(reg.TPCR1, reg.TPCR1_AUTO | reg.TPCR1_DEBOUNCE)
364369
self._write_data(self._read_reg(reg.INTC1) | reg.INTC1_TP)
370+
self._gfx_mode()
365371
else:
366372
self._write_data(self._read_reg(reg.INTC1) & ~reg.INTC1_TP)
367373
self._write_reg(reg.TPCR0, reg.TPCR0_DISABLE)
@@ -375,9 +381,12 @@ def touched(self) -> bool:
375381
:rtype: bool
376382
"""
377383
if self._tpin is not None:
378-
self._gfx_mode() # Hack that seems to work
384+
# Hardware interrupt only works in graphics mode
385+
self._gfx_mode()
379386
if self._tpin.value:
380387
return False
388+
389+
# Read the Interrupt Flag
381390
istouched = self._read_reg(reg.INTC2) & reg.INTC2_TP
382391
return istouched
383392

@@ -388,13 +397,13 @@ def touch_read(self) -> Tuple[int, int]:
388397
:return: The coordinate of the detected touch
389398
:rtype: tuple[int, int]
390399
"""
391-
touch_x = self._read_reg(reg.TPXH)
392-
touch_y = self._read_reg(reg.TPYH)
393-
temp = self._read_reg(reg.TPXYL)
394-
touch_x = touch_x << 2
395-
touch_y = touch_y << 2
396-
touch_x |= temp & 0x03
397-
touch_y |= (temp >> 2) & 0x03
400+
# Read the Touch Coordinates
401+
touch_x_high_bits = self._read_reg(reg.TPXH)
402+
touch_y_high_bits = self._read_reg(reg.TPYH)
403+
touch_xy_low_bits = self._read_reg(reg.TPXYL)
404+
touch_x = touch_x_high_bits << 2 | touch_xy_low_bits & 0x03
405+
touch_y = touch_y_high_bits << 2 | (touch_xy_low_bits >> 2) & 0x03
406+
# Clear the Interrupt Flag
398407
self._write_reg(reg.INTC2, reg.INTC2_TP)
399408
return [touch_x, touch_y]
400409

adafruit_ra8875/registers.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,11 @@
6060
HPWR_HIGH = 0x80
6161

6262
VDHR0 = 0x19
63+
VDHR1 = 0x1A
6364
VNDR0 = 0x1B
65+
VNDR1 = 0x1C
6466
VSTR0 = 0x1D
67+
VSTR1 = 0x1E
6568
VPWR = 0x1F
6669
VPWR_LOW = 0x00
6770
VPWR_HIGH = 0x80
@@ -70,10 +73,14 @@
7073
FNCR1 = 0x22
7174

7275
HSAW0 = 0x30
76+
HSAW1 = 0x31
7377
VSAW0 = 0x32
78+
VSAW1 = 0x33
7479

7580
HEAW0 = 0x34
81+
HEAW1 = 0x35
7682
VEAW0 = 0x36
83+
VEAW1 = 0x37
7784

7885
MCLR = 0x8E
7986
MCLR_START = 0x80
@@ -125,11 +132,22 @@
125132
TPCR0 = 0x70
126133
TPCR0_ENABLE = 0x80
127134
TPCR0_DISABLE = 0x00
135+
TPCR0_WAIT_512CLK = 0x00
136+
TPCR0_WAIT_1024CLK = 0x10
137+
TPCR0_WAIT_2048CLK = 0x20
128138
TPCR0_WAIT_4096CLK = 0x30
139+
TPCR0_WAIT_8192CLK = 0x40
140+
TPCR0_WAIT_16384CLK = 0x50
141+
TPCR0_WAIT_32768CLK = 0x60
142+
TPCR0_WAIT_65536CLK = 0x70
129143
TPCR0_WAKEENABLE = 0x08
130144
TPCR0_WAKEDISABLE = 0x00
131145
TPCR0_ADCCLK_DIV4 = 0x02
146+
TPCR0_ADCCLK_DIV8 = 0x03
132147
TPCR0_ADCCLK_DIV16 = 0x04
148+
TPCR0_ADCCLK_DIV32 = 0x05
149+
TPCR0_ADCCLK_DIV64 = 0x06
150+
TPCR0_ADCCLK_DIV128 = 0x07
133151

134152
TPCR1 = 0x71
135153
TPCR1_AUTO = 0x00
@@ -152,3 +170,12 @@
152170
INTC2_DMA = 0x08
153171
INTC2_TP = 0x04
154172
INTC2_BTE = 0x02
173+
174+
WAITTIME_LUT = {
175+
TPCR0_ADCCLK_DIV4: TPCR0_WAIT_512CLK,
176+
TPCR0_ADCCLK_DIV8: TPCR0_WAIT_1024CLK,
177+
TPCR0_ADCCLK_DIV16: TPCR0_WAIT_2048CLK,
178+
TPCR0_ADCCLK_DIV32: TPCR0_WAIT_4096CLK,
179+
TPCR0_ADCCLK_DIV64: TPCR0_WAIT_8192CLK,
180+
TPCR0_ADCCLK_DIV128: TPCR0_WAIT_16384CLK,
181+
}

examples/ra8875_simpletest.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,3 @@
8181
display.fill_circle(
8282
int(coords[0] / x_scale), int(coords[1] / y_scale), 4, MAGENTA
8383
)
84-
display.txt_color(WHITE, BLACK)
85-
display.txt_set_cursor(display.width // 2 - 220, display.height // 2 - 20)
86-
display.txt_size(2)
87-
display.txt_write(
88-
"Position ("
89-
+ str(int(coords[0] / x_scale))
90-
+ ", "
91-
+ str(int(coords[1] / y_scale))
92-
+ ")"
93-
)

0 commit comments

Comments
 (0)