def connect(self, dev): self.sd = SPIDriver(dev) [w.Enable(True) for w in self.allw] self.ckCS.SetValue(not self.sd.cs) self.ckA.SetValue(self.sd.a) self.ckB.SetValue(self.sd.b) self.refresh(None)
def __init__(self, dev): self.spi = SPIDriver(dev) self.spi.setb(1) if False: self.spi.setb(0) time.sleep(1) self.spi.setb(1) time.sleep(1) self.coldstart() t0 = time.time() while self._rd32(gd3.REG_ID) != 0x7c: assert (time.time() - t0) < 1.0, "No response - is GD attached?" if 0: time.sleep(1) print("ID %8x" % self._rd32(gd3.REG_ID)) print("CMD_READ %8x" % self._rd32(gd3.REG_CMD_READ)) print("CMD_WRITE %8x" % self._rd32(gd3.REG_CMD_WRITE)) print("CMD_SPACE %8x" % self._rd32(gd3.REG_CMDB_SPACE)) while self._rd32(gd3.REG_ID) != 0x7c: time.sleep(.1) self.getspace() self.stream()
def preview(cmdbuf): print('preview is', len(cmdbuf), 'bytes') d = SPIDriver( "/dev/serial/by-id/usb-FTDI_FT230X_Basic_UART_DO02C71A-if00-port0") from bteve.gameduino_spidriver import GameduinoSPIDriver gd = GameduinoSPIDriver(d) gd.init() gd.cc(cmdbuf) gd.finish()
def __init__(self, dev): self.d = SPIDriver(dev) self.d.unsel() self.d.seta(1) self.d.setb(1) print('reset') self.d.setb(0) time.sleep(.1) self.d.setb(1)
def __init__(self): self.d = SPIDriver( "/dev/serial/by-id/usb-FTDI_FT230X_Basic_UART_DO01HE8Q-if00-port0") self.d.unsel() self.d.seta(1) self.d.setb(1) if 0: print('reset') self.d.setb(0) time.sleep(.1) self.d.setb(1)
def on_refresh_gui(self): self.grid.after(500, self.on_refresh_gui) if self.port_prev != self.port_combo.get(): self.spi_driver = None self.port_prev = self.port_combo.get() if len(self.port_prev) > 0: try: self.spi_driver = SPIDriver(self.port_prev) except serial.serialutil.SerialException: pass if self.spi_driver is not None: self.spi_driver.getstatus() self.ser_num.var.set(self.spi_driver.serial) self.voltage.var.set("{0:.1f} V".format(self.spi_driver.voltage)) self.current.var.set("{0:.1f} mA".format(self.spi_driver.current)) self.deg_c.var.set("{0:.1f} °C".format(self.spi_driver.temp)) self.uptime.var.set(str(datetime.timedelta(seconds=self.spi_driver.uptime))) # https://stackoverflow.com/a/775095/101252 self.chk_ncs.var.set(not self.spi_driver.cs) self.chk_a.var.set(self.spi_driver.a) self.chk_b.var.set(self.spi_driver.b) return else: self.ser_num.var.set('') self.voltage.var.set('') self.current.var.set('') self.deg_c.var.set('') self.uptime.var.set('') self.chk_ncs.var.set(2) self.chk_a.var.set(2) self.chk_b.var.set(2) self.miso.var.set('') self.mosi.var.set('')
#!/usr/bin/env python3 # coding=utf-8 import sys from Eve import Eve from spidriver import SPIDriver def align4(s): return s + bytes(-len(s) & 3) if __name__ == '__main__': if len(sys.argv) > 1: s = SPIDriver(sys.argv[1]) else: s = SPIDriver() e = Eve(s) e.initialize() e.cmd_setbitmap(0, e.RGB565, 480, 272) e.Clear() e.Begin(Eve.BITMAPS) e.Vertex2f(0, 0) e.swap() e.finish() for a in sys.argv[2:]: e.cmd_loadimage(0, Eve.OPT_NODL) e.c(align4(open(a, "rb").read()))
def image_to_data(self, image, rotation=0): """Generator function to convert a PIL image to 16-bit 565 RGB bytes.""" # NumPy is much faster at doing this. NumPy code provided by: # Keith (https://www.blogger.com/profile/02555547344016007163) pb = np.rot90(np.array(image.convert('RGB')), rotation // 90).astype('uint8') result = np.zeros((self._width, self._height, 2), dtype=np.uint8) result[..., [0]] = np.add(np.bitwise_and(pb[..., [0]], 0xF8), np.right_shift(pb[..., [1]], 5)) result[..., [1]] = np.add(np.bitwise_and(np.left_shift(pb[..., [1]], 3), 0xE0), np.right_shift(pb[..., [2]], 3)) return result.flatten().tolist() if __name__ == '__main__': try: optlist, args = getopt.getopt(sys.argv[1:], "h:") except getopt.GetoptError as reason: print() print('usage: st7789 [ -h device ] image...') print() print() sys.exit(1) optdict = dict(optlist) st = ST7789(SPIDriver(optdict.get('-h', "/dev/ttyUSB0"))) st.start() st.clear() for a in args: image = Image.open(a) image = image.resize((240, 240)) st.display(image) time.sleep(3)
import sys import bteve as eve import board import busio if sys.implementation.name == "circuitpython": gd = eve.Gameduino() else: from spidriver import SPIDriver gd = eve.Gameduino(SPIDriver(sys.argv[1])) gd.init() i2c = busio.I2C(board.SCL, board.SDA) while not i2c.try_lock(): pass def pot(a): result = bytearray(1) i2c.writeto_then_readfrom(a, bytes([0xff]), result) return result[0] while True: gd.finish() (r, g, b) = (pot(0x2a), pot(0x28), pot(0x2c)) gd.ClearColorRGB(r, g, b) gd.Clear() def gauge(x, bg, c): gd.cmd_bgcolor(bg)
return "\n".join([hexline(s[i:i + 16]) for i in range(0, len(s), 16)]) if __name__ == '__main__': try: optlist, args = getopt.getopt(sys.argv[1:], "h:r:w:") except getopt.GetoptError as reason: print() print('usage: flash [ -h device ] [ -r file ] [ -w file ]') print() print() sys.exit(1) optdict = dict(optlist) s = SPIDriver(optdict.get('-h', "/dev/ttyUSB0")) s.seta(0) s.unsel() # Some primitives for generic flash def command(b): s.unsel() s.sel() s.write(b) def idcode(): command([0x9f]) return s.read(3) def write_enable(): command([0x06])
def __init__(self): Gtk.Window.__init__(self, title="SPIDriver") self.set_border_width(10) self.sd = SPIDriver() # help(self.sd) hbox = Gtk.Box(spacing=6) def pair(a, b): r = Gtk.HBox(spacing=6) r.pack_start(a, False, True, 0) r.pack_end(b, False, True, 0) return r def label(s): r = Gtk.Label() r.set_text(s) return r def vbox(items): r = Gtk.VBox(spacing=6) [r.pack_start(i, True, True, 0) for i in items] return r def hbox(items): r = Gtk.HBox(spacing=6) [r.pack_start(i, False, True, 0) for i in items] return r def checkbutton(name, state, click): r = Gtk.CheckButton(name) r.set_active(state) r.connect("clicked", click) return r def button(name, click): r = Gtk.Button(name) r.connect("clicked", click) return r self.label_voltage = Gtk.Label() self.label_current = Gtk.Label() self.label_temp = Gtk.Label() self.tx = Gtk.Entry() self.tx.set_max_length(2) self.tx.set_width_chars(2) self.rx = Gtk.Entry() self.rx.set_width_chars(20) self.add(vbox([ pair(label("Voltage"), self.label_voltage), pair(label("Current"), self.label_current), pair(label("Temp"), self.label_temp), hbox([ checkbutton("CS", 1 - self.sd.cs, self.click_cs), checkbutton("A", self.sd.a, self.click_a), checkbutton("B", self.sd.b, self.click_b), ]), pair( self.tx, button("Send", self.send) ), pair( self.rx, button("Recv", self.click_a) ), ])) self.refresh() """ self.add(hbox) button = Gtk.Button.new_with_label("Click Me") button.connect("clicked", self.on_click_me_clicked) hbox.pack_start(button, True, True, 0) button = Gtk.Button.new_with_mnemonic("_Open") button.connect("clicked", self.on_open_clicked) hbox.pack_start(button, True, True, 0) button = Gtk.Button.new_with_mnemonic("_Close") button.connect("clicked", self.on_close_clicked) hbox.pack_start(button, True, True, 0) hbox.pack_start(self.hbox([self.checkbutton("A"), self.checkbutton("b")]), True, True, 0) label = Gtk.Label() label.set_text("This is a left-justified label.\nWith multiple lines.") label.set_justify(Gtk.Justification.LEFT) hbox.pack_start(label, True, True, 0) self.label_voltage = label """ GLib.timeout_add(1000, self.refresh)
def __init__(self): Gtk.Window.__init__(self, title="SPIDriver") self.set_border_width(10) self.sd = SPIDriver() def pair(a, b): r = Gtk.HBox(spacing=6) r.pack_start(a, False, True, 0) r.pack_end(b, False, True, 0) return r def label(s): r = Gtk.Label() r.set_text(s) return r def vbox(items): r = Gtk.VBox(spacing=6) [r.pack_start(i, True, True, 0) for i in items] return r def hbox(items): r = Gtk.HBox(spacing=6) [r.pack_start(i, False, True, 0) for i in items] return r def checkbutton(name, state, click): r = Gtk.CheckButton(name) r.set_active(state) r.connect("clicked", click) return r def button(name, click): r = Gtk.Button(name) r.connect("clicked", click) return r self.label_voltage = Gtk.Label() self.label_current = Gtk.Label() self.label_temp = Gtk.Label() self.tx = Gtk.Entry() self.tx.set_width_chars(20) self.tx.connect('changed', self.edit) self.rx = Gtk.Entry() self.rx.set_width_chars(20) self.rx.connect('button-press-event', lambda a, b: True) self.rx.set_property('editable', False) self.button_send = button("Send", self.send) self.button_send.set_sensitive(False) self.add( vbox([ pair(label("Voltage"), self.label_voltage), pair(label("Current"), self.label_current), pair(label("Temp"), self.label_temp), Gtk.Separator(orientation=Gtk.Orientation.HORIZONTAL), hbox([ checkbutton("CS", 1 - self.sd.cs, self.click_cs), checkbutton("A", self.sd.a, self.click_a), checkbutton("B", self.sd.b, self.click_b), ]), pair(self.tx, self.button_send), pair(self.rx, button("Recv", self.recv)), ])) self.refresh() GLib.timeout_add(1000, self.refresh)
from spidriver import SPIDriver s = SPIDriver("/dev/ttyUSB0") # change for your port s.sel() # start command s.write([0x9f]) # command 9F is READ JEDEC ID print(list(s.read(3))) # read next 3 bytes s.unsel() # end command
#!/usr/bin/env python3 # coding=utf-8 import random import time from spidriver import SPIDriver # print(hex(crc16xmodem(bytes([0xaa, 0xbb, 0xcc]), 0xffff))) def rnd(n): return random.randrange(n) if __name__ == '__main__': s = SPIDriver() u = s.uptime while s.uptime == u: s.getstatus() t0 = time.time() d0 = s.uptime while 1: s.getstatus() du = s.uptime - d0 # device uptime tu = int(time.time() - t0) # true uptime fastness = du - tu print("%9d %.3f V %4d mA %.1f C %04x fast=%d" % (tu, s.voltage, s.current, s.temp, s.ccitt_crc, fastness)) time.sleep(10)