Exemple #1
0
class SPI:
    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 transfer(self, wr, rd = 0):
        self.d.sel()
        self.d.write(wr)
        r = self.d.read(rd)
        self.d.unsel()
        return r
class GameduinoSPIDriver(gameduino.Gameduino):
    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 transfer(self, wr, rd=0):
        self.d.sel()
        self.d.write(wr)
        r = self.d.read(rd)
        self.d.unsel()
        return r
Exemple #3
0
class GD(gameduino2.base.GD2):
    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 coldstart(self):
        self.host_cmd(0x00)   # Wake up
        self.host_cmd(0x48)   # int clock
        self.host_cmd(0x68)   # Core reset
        time.sleep(.25)

    def host_cmd(self, a, b = 0, c = 0):
        self.spi.sel()
        self.spi.write(bytes([a, b, c]))
        self.spi.unsel()

    def start(self, a):
        self.spi.sel()
        self.spi.write(bytes([
            0xff & (a >> 16),
            0xff & (a >> 8),
            0xff & a]))

    def _rd(self, a, n):
        self.start(a)
        r = self.spi.read(1 + n)
        self.spi.unsel()
        return r[1:]

    def _rd32(self, a):
        return struct.unpack("<I", self._rd(a, 4))[0]

    def _wr32(self, a, v):
        self.start(0x800000 | a)
        self.spi.write(struct.pack("I", v))
        self.spi.unsel()

    def _wr(self, a, v):
        self.start(0x800000 | a)
        self.spi.write(v)
        self.spi.unsel()

    def getspace(self):
        self.space = self._rd32(gd3.REG_CMDB_SPACE)
        if self.space & 1:
            raise CoprocessorException

    def stream(self):
        self.start(0x800000 | gd3.REG_CMDB_WRITE)

    def unstream(self):
        self.spi.unsel()

    def reserve(self, n):
        if self.space < n:
            self.unstream()
            while self.space < n:
                self.getspace()
            self.stream()
            
    def c4(self, v):
        '''
        Write 32-bit value v to the command FIFO
        '''
        self.reserve(4)
        self.spi.write(struct.pack("I", v))
        self.space -= 4

    def c(self, ss):
        '''
        Write s to the command FIFO
        '''
        for i in range(0, len(ss), 64):
            s = ss[i:i + 64]
            self.reserve(len(s))
            self.spi.write(s)
            self.space -= len(s)

    def flush(self):
        pass

    def finish(self):
        self.reserve(4092)

    def is_idle(self):
        self.unstream()
        self.getspace()
        self.stream()
        return self.space == 4092

    def rd32(self, a):
        self.unstream()
        r = self._rd32(a)
        self.stream()
        return r

    def rd(self, a, n):
        self.unstream()
        r = self._rd(a, n)
        self.stream()
        return r

    def wr(self, a, v):
        self.unstream()
        r = self._wr(a, v)
        self.stream()

    def result(self, n=1):
        # Return the result field of the preceding command
        self.finish()
        self.unstream()
        wp = self._rd32(gd3.REG_CMD_READ)
        r = self._rd32(gd3.RAM_CMD + (4095 & (wp - 4 * n)))
        self.stream()
        return r

    def setup_480x272(self):
        b = 6
        setup = [
            (gd3.REG_OUTBITS, b * 73),
            (gd3.REG_DITHER, 1),
            (gd3.REG_GPIO, 0x83),
            (gd3.REG_PCLK_POL, 1),
            (gd3.REG_ROTATE, 0),
            (gd3.REG_SWIZZLE, 3),
        ]

        self.Clear()
        self.swap()
        for (a, v) in setup:
            self.cmd_regwrite(a, v)

        self.cmd_regwrite(gd3.REG_PCLK, 5)  # Enable display
        self.finish()

        self.w = 480
        self.h = 272

    def setup_800x480(self):
        b = 6
        setup = [
            (gd3.REG_OUTBITS, b * 73),
            (gd3.REG_DITHER, 1),
            (gd3.REG_GPIO, 0x83),
            (gd3.REG_ROTATE, 0),
            (gd3.REG_SWIZZLE, 3),
            (gd3.REG_HCYCLE, 928),
            (gd3.REG_HOFFSET, 88),
            (gd3.REG_HSIZE, 800),
            (gd3.REG_HSYNC0, 0),
            (gd3.REG_HSYNC1, 48),
            (gd3.REG_VCYCLE, 525),
            (gd3.REG_VOFFSET, 32),
            (gd3.REG_VSIZE, 480),
            (gd3.REG_VSYNC0, 0),
            (gd3.REG_VSYNC1, 3),
            (gd3.REG_CSPREAD, 0),
            (gd3.REG_PCLK_POL, 0),
        ]
        for (a, v) in setup:
            self.cmd_regwrite(a, v)

        self.Clear()
        self.swap()
        self.finish()

        self.cmd_regwrite(gd3.REG_PCLK, 2)  # Enable display

        self.w = 800
        self.h = 480


    def calibrate(self):
        self.Clear()
        self.cmd_text(240, 135, 29, gd3.OPT_CENTER, "Tap the dot")
        self.cmd_calibrate(0)
        self.cmd_dlstart()

    def screenshot(self, dest):
        REG_SCREENSHOT_EN    = 0x302010 # Set to enable screenshot mode
        REG_SCREENSHOT_Y     = 0x302014 # Y line register
        REG_SCREENSHOT_START = 0x302018 # Screenshot start trigger
        REG_SCREENSHOT_BUSY  = 0x3020e8 # Screenshot ready flags
        REG_SCREENSHOT_READ  = 0x302174 # Set to enable readout
        RAM_SCREENSHOT       = 0x3c2000 # Screenshot readout buffer

        self.finish()
        self.unstream()

        self._wr32(REG_SCREENSHOT_EN, 1)
        self._wr32(0x0030201c, 32)
        
        self._wr32(REG_SCREENSHOT_READ, 1)

        for ly in range(self.h):
            self._wr32(REG_SCREENSHOT_Y, ly)
            self._wr32(REG_SCREENSHOT_START, 1)
            time.sleep(.002)
            # while (self.raw_read(REG_SCREENSHOT_BUSY) | self.raw_read(REG_SCREENSHOT_BUSY + 4)): pass
            while self._rd(REG_SCREENSHOT_BUSY, 8) != bytes(8):
                pass
            self._wr32(REG_SCREENSHOT_READ, 1)
            bgra = self._rd(RAM_SCREENSHOT, 4 * self.w)
            (b,g,r,a) = [bgra[i::4] for i in range(4)]
            line = bytes(sum(zip(r,g,b), ()))
            dest(line)
            self._wr32(REG_SCREENSHOT_READ, 0)
        self._wr32(REG_SCREENSHOT_EN, 0)
        self.stream()

    def screenshot_im(self):
        self.ssbytes = b""
        def appender(s):
            self.ssbytes += s
        self.screenshot(appender)
        from PIL import Image
        return Image.frombytes("RGB", (self.w, self.h), self.ssbytes)
if __name__ == '__main__':
    s = SPIDriver(sys.argv[1])
    t1 = time.time() + float(sys.argv[2])
    i = 0
    random.seed(7)
    while time.time() < t1:
        expected = s.ccitt_crc
        s.unsel()
        l = 1 + rnd(100)
        db = [rnd(256) for j in range(l)]
        s.write(db)
        expected = crc16xmodem(db, expected)
        s.unsel()

        db = [rnd(256) for j in range(64)]
        r = list(array.array('B', s.writeread(db)))
        expected = crc16xmodem(db, expected)
        expected = crc16xmodem(r, expected)

        s.getstatus()
        print("expected=%04x actual=%04x" % (expected, s.ccitt_crc))
        assert expected == s.ccitt_crc, "pass %d with %d bytes %s, expected=%04x actual=%04x" % (
            i, len(db), list(db), expected, s.ccitt_crc)
        i += 1
    for i in range(20):
        s.seta(0)
        s.setb(0)
        s.seta(1)
        s.setb(1)
Exemple #5
0
class ButtonWindow(Gtk.Window):

    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 refresh(self):
        self.sd.getstatus()
        self.label_voltage.set_text("%.2f V" % self.sd.voltage)
        self.label_current.set_text("%d mA" % self.sd.current)
        self.label_temp.set_text("%.1f C" % self.sd.temp)
        return True

    def click_cs(self, button):
        print('CS state', button.get_state(), Gtk.StateType.ACTIVE)
        [self.sd.unsel, self.sd.sel][ison(button)]()

    def click_a(self, button):
        self.sd.seta(int(button.get_state()))

    def click_b(self, button):
        self.sd.setb(int(button.get_state()))

    def on_click_me_clicked(self, button):
        print("\"Click me\" button was clicked")

    def send(self, _):
        b = self.tx.get_buffer()
        print(b.get_text())

        self.sd.write(struct.pack("B", int(b.get_text(), 16)))
        b.delete_text(0, -1)

    def on_open_clicked(self, button):
        print("\"Open\" button was clicked")

    def on_close_clicked(self, button):
        print("Closing application")
        Gtk.main_quit()
Exemple #6
0
class Frame(wx.Frame):
    def __init__(self):

        self.sd = None

        def widepair(a, b):
            r = wx.BoxSizer(wx.HORIZONTAL)
            r.Add(a, 1, wx.LEFT)
            r.AddStretchSpacer(prop=1)
            r.Add(b, 1, wx.RIGHT)
            return r

        def pair(a, b):
            r = wx.BoxSizer(wx.HORIZONTAL)
            r.Add(a, 1, wx.LEFT)
            r.Add(b, 0, wx.RIGHT)
            return r

        def rpair(a, b):
            r = wx.BoxSizer(wx.HORIZONTAL)
            r.Add(a, 0, wx.LEFT)
            r.Add(b, 1, wx.RIGHT)
            return r

        def label(s):
            return wx.StaticText(self, label = s)

        def hbox(items):
            r = wx.BoxSizer(wx.HORIZONTAL)
            [r.Add(i, 0, wx.EXPAND) for i in items]
            return r

        def hcenter(i):
            r = wx.BoxSizer(wx.HORIZONTAL)
            r.AddStretchSpacer(prop=1)
            r.Add(i, 2, wx.CENTER)
            r.AddStretchSpacer(prop=1)
            return r

        def vbox(items):
            r = wx.BoxSizer(wx.VERTICAL)
            [r.Add(i, 0, wx.EXPAND) for i in items]
            return r

        wx.Frame.__init__(self, None, -1, "SPIDriver")

        self.label_serial = wx.StaticText(self, label = "-", style = wx.ALIGN_RIGHT)
        self.label_voltage = wx.StaticText(self, label = "-", style = wx.ALIGN_RIGHT)
        self.label_current = wx.StaticText(self, label = "-", style = wx.ALIGN_RIGHT)
        self.label_temp = wx.StaticText(self, label = "-", style = wx.ALIGN_RIGHT)
        self.label_uptime = wx.StaticText(self, label = "-", style = wx.ALIGN_RIGHT)

        self.Bind(EVT_PING, self.refresh)

        self.ckCS = wx.CheckBox(self, label = "CS")
        self.ckA = wx.CheckBox(self, label = "A")
        self.ckB = wx.CheckBox(self, label = "B")
        self.ckCS.Bind(wx.EVT_CHECKBOX, self.check_cs)
        self.ckA.Bind(wx.EVT_CHECKBOX, self.check_a)
        self.ckB.Bind(wx.EVT_CHECKBOX, self.check_b)

        ps = self.GetFont().GetPointSize()
        fmodern = wx.Font(ps, wx.MODERN, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL)
        def logger():
            r = wx.TextCtrl(self, style=wx.TE_READONLY | wx.TE_RIGHT | wx.TE_DONTWRAP)
            r.SetBackgroundColour(wx.Colour(224, 224, 224))
            r.SetFont(fmodern)
            return r
        self.txMISO = logger()
        self.txMOSI = logger()

        self.txVal = HexTextCtrl(self, size=wx.DefaultSize, style=0)
        self.txVal.SetMaxLength(2)
        self.txVal.SetFont(wx.Font(14 * ps // 10,
                              wx.MODERN,
                              wx.FONTSTYLE_NORMAL,
                              wx.FONTWEIGHT_BOLD))
        txButton = wx.Button(self, label = "Transfer")
        txButton.Bind(wx.EVT_BUTTON, partial(self.transfer, self.txVal))
        txButton.SetDefault()

        self.allw = [self.ckCS, self.ckA, self.ckB, self.txVal, txButton, self.txMISO, self.txMOSI]
        [w.Enable(False) for w in self.allw]
        self.devs = self.devices()
        cb = wx.ComboBox(self, choices = sorted(self.devs.keys()), style = wx.CB_READONLY)
        cb.Bind(wx.EVT_COMBOBOX, self.choose_device)
        vb = vbox([
            label(""),
            hcenter(cb),
            label(""),
            hcenter(pair(
                vbox([
                    label("Serial"),
                    label("Voltage"),
                    label("Current"),
                    label("Temp."),
                    label("Running"),
                ]),
                vbox([
                    self.label_serial,
                    self.label_voltage,
                    self.label_current,
                    self.label_temp,
                    self.label_uptime,
                ])
            )),

            label(""),
            rpair(label("MISO"), self.txMISO),
            rpair(label("MOSI"), self.txMOSI),
            label(""),
            hcenter(pair(self.ckCS, hbox([self.ckA, self.ckB]))),
            label(""),
            hcenter(pair(self.txVal, txButton)),
            label(""),
            ])
        self.SetSizerAndFit(vb)
        self.SetAutoLayout(True)

        if len(self.devs) > 0:
            d1 = min(self.devs)
            self.connect(self.devs[d1])
            cb.SetValue(d1)

        t = threading.Thread(target=ping_thr, args=(self, ))
        t.setDaemon(True)
        t.start()

    def devices(self):
        if sys.platform == 'darwin':
            devdir = "/dev/"
            pattern = "^tty.usbserial-(........)"
        else:
            devdir = "/dev/serial/by-id/"
            pattern = "^usb-FTDI_FT230X_Basic_UART_(........)-"

        if not os.access(devdir, os.R_OK):
            return {}
        devs = os.listdir(devdir)
        def filter(d):
            m = re.match(pattern, d)
            if m:
                return (m.group(1), devdir + d)
        seldev = [filter(d) for d in devs]
        return dict([d for d in seldev if d])

    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 refresh(self, e):
        if self.sd:
            self.sd.getstatus()
            self.label_serial.SetLabel(self.sd.serial)
            self.label_voltage.SetLabel("%.2f V" % self.sd.voltage)
            self.label_current.SetLabel("%d mA" % self.sd.current)
            self.label_temp.SetLabel("%.1f C" % self.sd.temp)
            days = self.sd.uptime // (24 * 3600)
            rem = self.sd.uptime % (24 * 3600)
            hh = rem // 3600
            mm = (rem / 60) % 60
            ss = rem % 60;
            self.label_uptime.SetLabel("%d:%02d:%02d:%02d" % (days, hh, mm, ss))

    def choose_device(self, e):
        self.connect(self.devs[e.EventObject.GetValue()])

    def check_cs(self, e):
        if e.EventObject.GetValue():
            self.sd.sel()
        else:
            self.sd.unsel()

    def check_a(self, e):
        self.sd.seta(e.EventObject.GetValue())

    def check_b(self, e):
        self.sd.setb(e.EventObject.GetValue())

    def transfer(self, htc, e):
        if htc.GetValue():
            txb = int(htc.GetValue(), 16)
            rxb = struct.unpack("B", self.sd.writeread(struct.pack("B", txb)))[0]
            self.txMOSI.AppendText(" %02X" % txb)
            self.txMISO.AppendText(" %02X" % rxb)
            htc.ChangeValue("")
Exemple #7
0
class SPIDriverWindow(Gtk.Window):
    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)

    def refresh(self):
        self.sd.getstatus()
        self.label_voltage.set_text("%.2f V" % self.sd.voltage)
        self.label_current.set_text("%d mA" % self.sd.current)
        self.label_temp.set_text("%.1f C" % self.sd.temp)
        return True

    def click_cs(self, button):
        [self.sd.unsel, self.sd.sel][ison(button)]()

    def click_a(self, button):
        self.sd.seta(ison(button))

    def click_b(self, button):
        self.sd.setb(ison(button))

    def edit(self, _):
        b = self.tx.get_buffer()
        valid = all([ishex(w) for w in b.get_text().split()])
        self.button_send.set_sensitive(valid)

    def transfer(self, byte):
        byte = struct.unpack("B", self.sd.writeread(struct.pack("B", byte)))[0]
        txb = self.tx.get_buffer()
        txb.delete_text(0, -1)
        rxb = self.rx.get_buffer()
        rxb.set_text(rxb.get_text()[-17:] + " %02x" % byte, -1)

    def send(self, _):
        b = self.tx.get_buffer()
        for w in b.get_text().split():
            self.transfer(int(w, 16))

    def recv(self, _):
        self.transfer(0xff)
Exemple #8
0
def pattern(n):
    return [rnd(256) for i in range(n)]


if __name__ == '__main__':
    if len(sys.argv) > 1:
        s = SPIDriver(sys.argv[1])
    else:
        s = SPIDriver()
    # print(s)
    # t1 = time.time() + float(sys.argv[2])
    while True:  # time.time() < t1:
        for i in range(50):
            random.choice([
                lambda: s.seta(rnd(2)),
                lambda: s.setb(rnd(2)),
                lambda: s.sel(),
                lambda: s.unsel(),
                lambda: s.writeread(pattern(1 + rnd(1))),
                # lambda: s.read(1 + rnd(12)),
                # lambda: s.getstatus()
            ])()
        os.system("outlet.py 8 on ; outlet.py 8 off")
        time.sleep(3)
    print(hex(s.debug))

    while 0:
        s.sel()
        s.write(b'ABCDEF')
        s.unsel()
        time.sleep(.050)