Example #1
0
def update_app(filename):
    print(f"Updating application firmware from {filename}")

    # Create the elements for the mboot filesystem-load operation.
    elems = fwupdate.update_app_elements(filename, _FILESYSTEM_ADDR,
                                         _FILESYSTEM_LEN)
    if not elems:
        return

    # Create the update key.
    key = struct.pack("<I", _SPIFLASH_UPDATE_KEY_VALUE)

    # Create a SPI flash object.
    spi = machine.SoftSPI(sck="B13", mosi="C3", miso="C2", baudrate=50_000_000)
    cs = machine.Pin("B12", machine.Pin.OUT, value=1)
    flash = spiflash.SPIFlash(spi, cs)

    # Write the update key and elements to the SPI flash.
    flash.erase_block(_SPIFLASH_UPDATE_KEY_ADDR)
    flash.write(_SPIFLASH_UPDATE_KEY_ADDR, key + elems)

    # Enter mboot with a request to do a filesystem-load update.
    # If there is a power failure during the update (eg battery removed) then
    # mboot will read the SPI flash update key and elements and retry.
    machine.bootloader(elems)
Example #2
0
    def __init__(self,
                 SPI_type: str,
                 reset: str = None,
                 CS: str = None,
                 MI: str = None,
                 MO: str = None,
                 SCK: str = None):

        # SPI_type = 'SPI1' or 'SPI2' or 'softSPI'
        SPIparams = {
            'baudrate': 1000000,
            'polarity': 1,
            'phase': 1,
            'bits': 8,
            'firstbit': machine.SPI.MSB
        }
        if '1' in SPI_type:
            self.SPI = machine.SPI(1, **SPIparams)

        elif '2' in SPI_type:
            self.SPI = machine.SPI(2, **SPIparams)

        elif 'soft' in SPI_type.lower(
        ):  # Works for newer versions of micropython
            self.SPI = machine.SoftSPI(
                sck=machine.Pin(SCK,
                                mode=machine.Pin.OUT,
                                pull=machine.Pin.PULL_DOWN),
                mosi=machine.Pin(MO,
                                 mode=machine.Pin.OUT,
                                 pull=machine.Pin.PULL_DOWN),
                miso=machine.Pin(MI, mode=machine.Pin.IN),
                **SPIparams)

        self.select = Digital_output(pin=CS, inverted=True)
        self.reset = Digital_output(pin=reset, inverted=True)

        self.reset.off()
        self.select.off()
Example #3
0
# Vbus(40)  Vin (10)
# Gnd(18)   Gnd (11)
# 15(20)    DC (3 DC)
# 13(17)    CS (5 OC OLEDCS)
# 14(19)    Rst (4 R RESET)
# 10(14)    CLK (2 CL SCK)
# 11(15)    DATA (1 SI MOSI)

import machine
import gc

# *** Choose your color display driver here ***
# Driver supporting non-STM platforms
# from drivers.ssd1351.ssd1351_generic import SSD1351 as SSD

# STM specific driver
from drivers.ssd1351.ssd1351 import SSD1351 as SSD

#height = 96  # 1.27 inch 96*128 (rows*cols) display
height = 128  # 1.5 inch 128*128 display

pdc = machine.Pin(15, machine.Pin.OUT, value=0)
pcs = machine.Pin(13, machine.Pin.OUT, value=1)
prst = machine.Pin(14, machine.Pin.OUT, value=1)
#spi = machine.SPI(1, baudrate=1_000_000)
spi = machine.SoftSPI(sck=machine.Pin(10, machine.Pin.OUT),
                      mosi=machine.Pin(11, machine.Pin.OUT),
                      miso=machine.Pin(8, machine.Pin.OUT))
gc.collect()  # Precaution before instantiating framebuf
ssd = SSD(spi, pcs, pdc, prst, height)  # Create a display instance
Example #4
0
def main():
    '''
    Draw on screen using focaltouch sensor
    '''
    try:
        # Turn on display backlight
        axp = axp202c.PMU()
        axp.enablePower(axp202c.AXP202_LDO2)

        # initialize display spi port
        spi = machine.SoftSPI(2,
                              baudrate=32000000,
                              polarity=1,
                              phase=0,
                              bits=8,
                              firstbit=0,
                              sck=machine.Pin(18, machine.Pin.OUT),
                              mosi=machine.Pin(19, machine.Pin.OUT))

        # configure display
        tft = st7789.ST7789(spi,
                            240,
                            240,
                            cs=machine.Pin(5, machine.Pin.OUT),
                            dc=machine.Pin(27, machine.Pin.OUT),
                            backlight=machine.Pin(12, machine.Pin.OUT),
                            rotation=2)

        # enable display and clear screen
        tft.init()
        tft.fill(st7789.BLACK)
        tft.text(font, "Draw", 104, 1, st7789.WHITE)

        # enable focaltouch touchscreen
        touch_i2c = machine.I2C(scl=machine.Pin(32), sda=machine.Pin(23))
        touch = focaltouch.FocalTouch(touch_i2c)

        color_index = 0
        color = 0
        # draw color swatches used to select color to draw
        for color_index, color in enumerate(COLORS):
            tft.fill_rect(color_index * 30, 210, 30, 30, color)

        # draw box around currently selected color
        tft.rect(color_index * 30, 210, 30, 30, st7789.WHITE)
        tft.rect(color_index * 30 + 1, 211, 28, 28, st7789.BLACK)
        tft.rect(color_index * 30 + 2, 212, 26, 26, st7789.BLACK)

        while True:
            # can be up to two touches
            if touch.touched == 1:

                # get x and y points of the first touch
                p_x = touch.touches[0]['x']
                p_y = touch.touches[0]['y']

                # If point is in the lowest 30 rows of the screen
                # change color to swatch pressed.
                if p_y > 209:
                    # remove box from around previous color swatch
                    tft.rect(color_index * 30, 210, 30, 30, color)
                    tft.rect(color_index * 30 + 1, 211, 28, 28, color)
                    tft.rect(color_index * 30 + 2, 212, 26, 26, color)

                    # update new color
                    color_index = p_x // 30
                    color = COLORS[color_index]

                    # draw box around newly selected color swatch
                    tft.rect(color_index * 30, 210, 30, 30, st7789.WHITE)
                    tft.rect(color_index * 30 + 1, 211, 28, 28, st7789.BLACK)
                    tft.rect(color_index * 30 + 2, 212, 26, 26, st7789.BLACK)

                else:
                    # draw the pixel - would be better with lines
                    tft.pixel(p_x, p_y, color)

    finally:
        # shutdown i2c
        if 'touch_i2c' in locals():
            touch_i2c.deinit()

        # shutdown spi
        if 'spi' in locals():
            spi.deinit()

        # turn off display backlight
        axp.disablePower(axp202c.AXP202_LDO2)
Example #5
0
def main():
    '''
    Draw on screen using map_bitarray_to_rgb565
    '''
    try:
        # Turn on display backlight
        axp = axp202c.PMU()
        axp.enablePower(axp202c.AXP202_LDO2)

        # initialize display spi port
        spi = machine.SoftSPI(
            2,
            baudrate=32000000,
            polarity=1,
            phase=0,
            bits=8,
            firstbit=0,
            sck=machine.Pin(18, machine.Pin.OUT),
            mosi=machine.Pin(19, machine.Pin.OUT))

        # configure display
        tft = st7789.ST7789(
            spi,
            240,
            240,
            cs=machine.Pin(5, machine.Pin.OUT),
            dc=machine.Pin(27, machine.Pin.OUT),
            backlight=machine.Pin(12, machine.Pin.OUT),
            rotation=2)

        # enable display and clear screen
        tft.init()
        tft.fill(st7789.BLACK)
        sprite = bytearray(512)

        # create pacman spites in random positions
        sprites = []
        for man in range(SPRITES):
            sprites.append(
                pacman(
                    random.randint(0, tft.width()-SPRITE_WIDTH),
                    random.randint(0, tft.height()-SPRITE_HEIGHT),
                    random.randint(0, SPRITE_STEPS-1)
                )
            )

        # move and draw sprites
        while True:
            for man in sprites:
                # move the sprite
                man.move()

                # convert bitmap into rgb565 blitable buffer
                tft.map_bitarray_to_rgb565(
                    SPRITE_BITMAPS[man.step],
                    sprite,
                    SPRITE_WIDTH,
                    st7789.YELLOW,
                    st7789.BLACK)

                # blit the buffer to the display
                tft.blit_buffer(
                    sprite,
                    man.x,
                    man.y,
                    SPRITE_WIDTH,
                    SPRITE_HEIGHT)

            time.sleep(0.1)

    finally:
        # shutdown spi
        if 'spi' in locals():
            spi.deinit()

        # turn off display backlight
        axp.disablePower(axp202c.AXP202_LDO2)