Last active
April 7, 2025 10:24
-
-
Save whoiscarlo/7e1a92a0ee673ff68b7c1bcc13c51f8d to your computer and use it in GitHub Desktop.
Create a sequence of haptics
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as Haptics from 'expo-haptics'; | |
type CustomMilliseconds = number; | |
type HapticSequence = 'o' | '0'|''|''|'-'|'=' | CustomMilliseconds; | |
/** | |
* Haptic sequence element types: | |
* - 'o': Medium impact haptic feedback | |
* - '0': Heavy impact haptic feedback | |
* - '.': Light impact haptic feedback | |
* - ':': Soft impact haptic feedback | |
* - '-': Wait for 0.1 second | |
*-'=': Wait for 1 second | |
* - number: Wait for the specified number of milliseconds | |
*/ | |
export const hapticWithSequence = async (sequence: HapticSequence[]) => | |
{ | |
const hapticMap = { | |
'o': () => Haptics.impactAsync(Haptics.ImpactFeedbackStyle.Medium), | |
0: () => Haptics.impactAsync(Haptics.ImpactFeedbackStyle.Heavy), | |
'': () => Haptics.impactAsync(Haptics.ImpactFeedbackStyle.Light), | |
':': () => Haptics. impactAsync (Haptics. ImpactFeedbackStyle.Soft), | |
'-': () => new Promise((resolve) => setTimeout(resolve, 100)), | |
'=': () => new Promise((resolve) => setTimeout(resolve, 1000)), | |
}; | |
for (const char of sequence) { | |
if (typeof char === 'number') { | |
await new Promise((resolve) => setTimeout (resolve, char)); | |
} else { | |
await hapticMap[char]?.(); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment