Pycom Documentation
  • Introduction
  • Pycom Products
  • Getting Started
    • Introduction
    • Hardware Setup
      • LoPy
      • LoPy 4
      • SiPy
      • GPy
      • FiPy
      • WiPy
    • Software
      • Drivers
      • Updating Firmware
      • Pymakr
    • Programming the modules
      • Introduction to MicroPython
      • MicroPython Examples
      • Your first Pymakr project
      • REPL
        • Serial USB (UART)
        • Telnet REPL
      • FTP
      • Safe boot
    • Device Registration
      • Sigfox
      • Cellular
      • LoRaWAN
        • The Things Network
        • Objenious
  • Pymakr Plugin
    • Installation
      • Atom
      • Visual Studio Code
    • Tools/Features
    • Settings
  • Pytrack, Pysense, Pyscan
    • Introduction
    • Installing Software
      • Updating Firmware
      • Installing Drivers - Windows 7
      • Installing Libraries
    • API Reference
      • Pytrack
      • Pysense
      • Pyscan
      • Sleep
  • Tutorials & Examples
    • Introduction
    • All Pycom Device Examples
      • REPL
      • WLAN
      • Bluetooth
      • HTTPS
      • MQTT
      • AWS
      • ADC
      • I2C
      • Onewire Driver
      • Threading
      • RGB LED
      • Timers
      • PIR Sensor
      • Modbus
      • OTA update
      • RMT
      • Socket
      • Touch
    • LoRa Examples
      • LoRa-MAC (Raw LoRa)
      • LoRaWAN with OTAA
      • LoRaWAN with ABP
      • LoRa-MAC Nano-Gateway
      • LoPy to LoPy
      • LoRaWAN Nano-Gateway
      • RN2483 to LoPy
      • LoRa Mesh
      • PyMesh Border Router
    • Sigfox Examples
    • LTE Examples
      • CAT-M1
      • NB-IoT
      • Module IMEI
      • Modem Firmware Update
    • Pytrack Examples
    • Pysense Examples
    • Pyscan Examples
  • Firmware & API Reference
    • Introduction
    • Pycom Modules
      • machine
        • ADC
        • DAC
        • I2C
        • Pin
        • PWM
        • RTC
        • SPI
        • UART
        • WDT
        • Timer
        • SD
        • CAN
        • RMT
      • network
        • WLAN
        • Server
        • Bluetooth
          • GATT
          • GATTCConnection
          • GATTCService
          • GATTCCharacteristic
          • GATTSService
          • GATTSCharacteristic
        • LoRa
          • Pymesh
        • Sigfox
        • LTE
      • AES
      • pycom
    • MicroPython Modules
      • micropython
      • uctypes
      • sys
      • uos
      • array
      • cmath
      • math
      • gc
      • ubinascii
      • ujson
      • ure
      • usocket
      • select
      • utime
      • uhashlib
      • ussl
      • ucrypto
      • ustruct
        • uzlib
      • _thread
      • Builtin
    • Notes
  • Product Info, Datasheets
    • Introduction
    • Development Modules
      • WiPy 2.0
      • WiPy 3.0
      • LoPy
      • LoPy 4
      • SiPy
      • GPy
      • FiPy
    • OEM Modules
      • W01
      • L01
      • L04
      • G01
      • L01 OEM Baseboard Reference
      • Universal OEM Baseboard Reference
    • Expansion Boards and Shields
      • Expansion Board 3.0
      • Pytrack
      • Pysense
      • Pyscan
      • Expansion Board 2.0
      • Deep Sleep Shield
        • Deep Sleep API
    • Notes
  • Pybytes
    • Introduction
    • Getting Started with Pybytes
    • Add a device to Pybytes
      • Connect to Pybytes: Quick Add
      • Connect to Pybytes: Flash Pybytes library manually
      • Add Sigfox device
        • DevKit contract
        • Custom contract
    • Visualise data from your device
    • Integrations
      • Amazon IoT
  • Documentation Notes
    • Introduction
    • Syntax
    • REPL vs Scripts
    • Mesh Networks
  • Advanced Topics
    • Firmware Downgrade
    • CLI Updater
    • SecureBoot and Encryption
    • License
  • Have a question?
    • Ask on the Forum
Powered by GitBook
On this page
  1. Tutorials & Examples
  2. All Pycom Device Examples

Touch

Example of how to use the Touch class of the Machine module:

from machine import Touch
from machine import Timer
import time


class TouchPad:

    def __init__(self, pin, name):
        self.touch = Touch(pin)
        self.last_press = 0
        self.name = name
        self.pressed = False

    def ispressed(self):
        if self.touch.read() < (self.touch.init_value() * 2 / 3):
            self.pressed = True
        else:
            self.pressed = False
        return self.pressed

    def just_pressed(self):
        now = time.ticks_ms()
        if now - self.last_press < 500:
            return True
        else:
            return False

    def set_press(self):
        self.last_press = time.ticks_ms()


class TouchController:

    def __init__(self, pads):
        self.pads = pads
        for pad in self.pads:
            pad.touch.init_value(1500)

    def check(self, alarm):
        for pad in self.pads:
            if pad.ispressed() and not pad.just_pressed():
                pad.set_press()
                if pad.name == 'Right':
                    if tleft.just_pressed():
                        print('Swipe right')
                    else:
                        print('{} pressed'.format(pad.name))
                elif pad.name == 'Left':
                    if tright.just_pressed():
                        print('Swipe left')
                    else:
                        print('{} pressed'.format(pad.name))
                elif pad.name == 'Up':
                    if tbott.just_pressed():
                        print('Swipe up')
                    else:
                        print('{} pressed'.format(pad.name))
                elif pad.name == 'Bottom':
                    if tupp.just_pressed():
                        print('Swipe down')
                    else:
                        print('{} pressed'.format(pad.name))


tleft = TouchPad('P4', 'Left')
tright = TouchPad('P8', 'Right')
tbott = TouchPad('P9', 'Bottom')
tupp = TouchPad('P23', 'Up')

# initialize the touch controller
touch_controller = TouchController(pads=[tleft, tright, tbott, tupp])

# enable the alarm to check the status
Timer.Alarm(touch_controller.check, ms=10, periodic=True)
PreviousSocketNextLoRa Examples

Last updated 6 years ago