[Pyto] Operate iPhone Taptic Engine with Python

Introduction

Some iPhones are equipped with a device called Taptic Engine. This is a device that creates the feel of pressing the home button instead of being a physical button. A click feeling occurs when you press and hold something other than the home button or when payment is completed.

I tried to operate this Taptic Engine using an application called Pyto.

environment

iPhone SE (2020)(iOS13.4.1) Pyto(11.9)

Taptic Engine and Haptic Feedback

I understand that Taptic Engine is a device that generates a click feeling, and that click feeling is called Haptic Feedback.

Types of Haptic Feedback

There are three types of Haptic Feedback on the iPhone. Here I referred to the site of [1]. Here It is also described in detail on the [2] site.

type function
UIImpactFeedbackGenerator React once. 3 levels of strength can be selected
UISelectionFeedbackGenerator For selection UI. The weakest reaction
UINotificationFeedbackGenerator For notification of success or failure. There are three types

How to write in Pyto

You can use ʻUIKit in Pyto. This is a function that allows you to touch Objective-C, like ʻobjc_util in Pythonista. Actually, it is a library called rubicon-objc, and it seems that Objective-C is operated using this. Pyto UIKit source code

It seems that you can minimize the delay by putting prepare () before calling, but it is optional. The feeling I tried didn't make a difference with or without it. prepare documentation

UIImpactFeedbackGenerator

You can adjust the strength in 3 steps. 0 is the weakest.

import UIKit

generator = UIKit.UIImpactFeedbackGenerator.alloc().init()
generator.prepare()

power = 0  # 0~3 stages of 2
generator.initWithStyle(power)  #Set strength
generator.impactOccurred()  #Activate

UISelectionFeedbackGenerator The strength cannot be adjusted.

import UIKit

generator = UIKit.UISelectionFeedbackGenerator.alloc().init()
generator.prepare()
generator.selectionChanged()  #Activate

UINotificationFeedbackGenerator You can choose from 3 types of reactions. Success (ton ** ton ) = 0, Warning ( ton ton ) = 1, Error ( ton ton **) = 2.

import UIKit

generator = UIKit.UINotificationFeedbackGenerator.alloc().init()
generator.prepare()
mode = 0  # 0~3 types of 2.
generator.notificationOccurred(mode)  #Activate

A program that tries three at once

This is a code that tests all the strengths of the above three types at once.

import UIKit
import time

generator = UIKit.UIImpactFeedbackGenerator.alloc().init()
generator2 = UIKit.UISelectionFeedbackGenerator.alloc().init()
generator3 = UIKit.UINotificationFeedbackGenerator.alloc().init()

print("impact start")
generator.prepare()
for i in range(3):
    print(f"-{i}")
    for j in range(3):
        generator.initWithStyle(i)
        generator.impactOccurred()
        time.sleep(0.3)
print("finished")
time.sleep(1)

print("selection start")
generator2.prepare()
for i in range(10):
    generator2.selectionChanged()
    time.sleep(0.1)
print("finished")
time.sleep(1)

print("notification start")
generator3.prepare()
for i in range(3):
    print(f"-{i}")
    generator3.notificationOccurred(i)
    time.sleep(1)
print("finished")

in conclusion

It took me some time to realize that I should write .alloc (). Init ().

Reference site

[1] https://qiita.com/WorldDownTown/items/2b5a72e41a95763727bb (How to use UIFeedbackGenerator and a convenient library) [2] https://note.com/tdksk/n/nb4498e59dcad (Effective micro-interaction with Haptic Feedback in iPhone app) [3] https://qiita.com/griffin_stewie/items/298f57ca3f1714ebe45c (I got the iPhone 7 so I immediately tried the Taptic Engine API) [4] https://developer.apple.com/documentation/uikit/uifeedbackgenerator?language=objc#2555399 (UIFeedbackGenerator) [5] https://rubicon-objc.readthedocs.io/en/latest/tutorial/tutorial-2.html (Rubicon Tutorial 2)

Recommended Posts

[Pyto] Operate iPhone Taptic Engine with Python
Operate Kinesis with Python
Operate Blender with Python
Operate Excel with Python (1)
Operate Excel with Python (2)
Operate Excel with Python openpyxl
Operate TwitterBot with Lambda, Python
[Note] Operate MongoDB with Python
[Python] [SQLite3] Operate SQLite with Python (Basic)
Search engine work with python
Operate a receipt printer with python
Try to operate Facebook with Python
Operate ECHONET Lite appliances with Python
Operate smartlife power supply with python (de-IFTTT)
[GCP] Operate Google Cloud Storage with Python
[Python] Automatically operate the browser with Selenium
Operate home appliances with Python and IRKit
FizzBuzz with Python3
Scraping with Python
Statistics with python
Scraping with Python
Python with Go
Twilio with Python
Play with 2016-Python
Tested with Python
with syntax (Python)
Operate the schedule app using python from iphone
Bingo with python
Zundokokiyoshi with python
Excel with Python
Microcomputer with Python
Cast with python
Try to operate DB with Python and visualize with d3
PIL with Python on Windows 8 (for Google App Engine)
Getting Started with Google App Engine for Python & PHP
How to operate Discord API with Python (bot registration)
[Automation] Operate GitLab with Python to facilitate inquiry management
Let's operate GPIO of Raspberry Pi with Python CGI
Run iphone safari from mac with python + selenium + safari-webdriver
Serial communication with Python
Zip, unzip with python
Django 1.11 started with Python3.6
Primality test with Python
Python with eclipse + PyDev.
Socket communication with Python
Data analysis with python 2
Scraping with Python (preparation)
Try scraping with Python.
Learning Python with ChemTHEATER 03
Sequential search with Python
"Object-oriented" learning with python
Operate Redmine using Python Redmine
Run Python with VBA
Handling yaml with python
Solve AtCoder 167 with python
Serial communication with python
Operate Filemaker from Python
[Python] Use JSON with Python
Learning Python with ChemTHEATER 05-1
Learn Python with ChemTHEATER
1.1 Getting Started with Python