-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsharp.py
More file actions
136 lines (110 loc) · 3.8 KB
/
Copy pathsharp.py
File metadata and controls
136 lines (110 loc) · 3.8 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# driver for SHARP memory LCD displays
# based on CircuitPython module: `adafruit_sharpmemorydisplay`
# copyright: 2018 ladyada for Adafruit Industries
#
# this module copyright: 2026 Peter Lumb
# licence: MIT
"""
`adafruit_sharpmemorydisplay`
====================================================
A display control library for Sharp 'memory' displays
* Author(s): ladyada
Implementation Notes
--------------------
**Hardware:**
* `Adafruit SHARP Memory Display Breakout - 2.7 inch 240x400 Monochrome <https://www.adafruit.com/product/4694>`_
* `Adafruit SHARP Memory Display Breakout - 1.3 inch 144x168 Monochrome <https://www.adafruit.com/product/3502>`_
* `Adafruit SHARP Memory Display Breakout - 1.3 inch 96x96 Monochrome <https://www.adafruit.com/product/1393>`_
**Software and Dependencies:**
* Adafruit CircuitPython firmware for the supported boards:
https://github.com/adafruit/circuitpython/releases
"""
from machine import Pin
import gc
import sys
import time
# import array
# import extended framebuffer if available)
try:
import framebuf2 as framebuf
_fb_variant = 2
except:
import framebuf
_fb_variant = 1
print("sharp_memory_display: framebuf is ", ("standard" if _fb_variant ==1 else "extended") )
# from adafruit_bus_device.spi_device import SPIDevice #amend to micropython SPI driver
from micropython import const
# try:
# import numpy
# except ImportError:
# numpy = None
__version__ = "0.2.016"
__repo__ = "https://github.com/peter-l5/MicroPython-Sharp-Memory-Display"
# constants
_SHARPMEM_BIT_WRITECMD = const(0x80)
_SHARPMEM_BIT_VCOM = const(0x40)
_SHARPMEM_BIT_CLEAR = const(0x20)
def reverse_bit(num: int) -> int:
"""Turn an LSB byte to an MSB byte, and vice versa. Used for SPI as
it is LSB for the SHARP, but 99% of SPI implementations are MSB only!"""
result = 0
for _ in range(8):
result <<= 1
result += num & 1
num >>= 1
return result
class SharpMemoryDisplay(framebuf.FrameBuffer):
"""A driver for sharp memory displays, you can use any size but the
full display must be buffered in memory!"""
def __init__(
self,
width: int,
height: int,
spi: SPI,
cs: Pin,
*
):
#from sh1107, no dc pin needed, no res/ reset pin needed
self.spi = spi
self.cs = cs
self.cs.low()
self.width = width
self.height= height
self._buf = bytearray(2)
self.displaybuffer = bytearray((width // 8) * height)
self.displaybuffer_mv = memoryview(self.displaybuffer)
super().__init__(self.displaybuffer, self.width, self.height,
framebuf.MONO_HLSB)
# Set the vcom bit to a defined state
self._vcom = True
def show(self) -> None:
(w,h, displaybuffer_mv) = (self.width, self.height, self.displaybuffer_mv)
buffer=displaybuffer_mv
image_buffer = bytearray()
# toggle the VCOM bit
self._buf[0] = _SHARPMEM_BIT_WRITECMD
if self._vcom:
self._buf[0] |= _SHARPMEM_BIT_VCOM
self._vcom = not self._vcom
slice_from = 0
line_len = w // 8
for line in range(h):
if line == 1:
self._buf[0]=0
self._buf[1] = reverse_bit(line + 1) # reverse_bit
image_buffer.extend(self._buf)
image_buffer.extend(self.displaybuffer[slice_from : slice_from + line_len])
slice_from += line_len
image_buffer.extend(b'\x00\x00')
self.cs.high()
time.sleep_us(3)
self.spi.write(image_buffer)
self.cs.low()
def clear(self):
_bufc=bytearray(2)
_bufc[0]=_SHARPMEM_BIT_CLEAR
_bufc[1]=0
self.cs.high()
time.sleep_us(3)
self.spi.write(_bufc)
self.cs.low()