touchscreen (touch screen)

    The touchscreen module contains basic reading touch screen operations

    Currently supported touch screens:

    • ns2009 (default)

    If you need to modify the driver model, you need to recompile the MaixPy source code to modify the pre-compiled supported model

    Global functions

    init(i2c=None, cal=None)

    Initialize the touch screen

    API may be changed later (mainly for changes to parameters of multiple drivers)


    • i2c: Currently supports the touch screen of I2C communication. Pass in the I2C instance object, this parameter may be renamed or cancelled later
    • cal: Calibration data, a tuple of 7 integer values, which can be obtained by the touchscreen.calibrate() function


    Calibrate the screen so that the screen display and touch screen pixels can correspond

    return value

    Return a tuple of 7 integer values, which can be saved to the file system or flash and passed in during initialization, so that there is no need to calibrate every time


    Read the current screen state and the coordinate value of the pressed point

    return value

    A tuple (status, x, y) composed of 3 integer values, note that this value will always keep the previous state

    • status: status, the values ​​are touchscreen.STATUS_PRESS, touchscreen.STATUS_MOVE, touchscreen.STATUS_RELEASE
    • x: x axis coordinate
    • y: y axis coordinate



    The screen is pressed, the first value of the tuple returned by the read() function


    The screen is pressed and moved, that is, press to move, the first value of the tuple returned by the read() function


    The screen is no longer held down, that is, there is no click, the first value of the tuple returned by the read() function


    Routine 1: Drawing board

    Black background and white pen drawing board, use the boot button to clear the content

    Cancel the comment of ts.calibrate() to start the touch screen calibration program

    board_info is related to the board, and different board configurations are different. Manual configuration is required before use.

    import touchscreen as ts
    from machine import I2C
    import lcd, image
    from board import board_info
    from fpioa_manager import *
    fm.register(board_info.BOOT_KEY, fm.fpioa.GPIO1)
    btn_clear = GPIO(GPIO.GPIO1, GPIO.IN)
    i2c = I2C(I2C.I2C0, freq=400000, scl=30, sda=31)
    img = image.Image()
    status_last = ts.STATUS_IDLE
    x_last = 0
    y_last = 0
    draw = False
    while True:
        (status,x,y) =
        print(status, x, y)
        if draw:
            img.draw_line((x_last, y_last, x, y))
        if status_last!=status:
            if (status==ts.STATUS_PRESS or status == ts.STATUS_MOVE):
                draw = True
                draw = False
            status_last = status
        x_last = x
        y_last = y
        if btn_clear.value() == 0: