@a16n/web-sim 0.0.0-stage → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +13 -2
- package/bin/cli.ts +255 -0
- package/bin/web-sim.js +39 -0
- package/overrides/@backpackapp-io/react-native-toast/overrides.ts +15 -0
- package/overrides/@react-native-firebase/app/overrides.ts +34 -0
- package/overrides/@tiktok-for-business/react-native-sdk/NativeTiktokSdk.ts +29 -0
- package/overrides/@tiktok-for-business/react-native-sdk/overrides.ts +9 -0
- package/overrides/expo-application/ExpoApplication.ts +81 -0
- package/overrides/expo-application/overrides.ts +16 -0
- package/overrides/expo-background-task/ExpoBackgroundTaskModule.ts +30 -0
- package/overrides/expo-background-task/overrides.ts +9 -0
- package/overrides/expo-constants/ExponentConstants.ts +66 -0
- package/overrides/expo-constants/overrides.ts +9 -0
- package/overrides/expo-device/ExpoDevice.ts +104 -0
- package/overrides/expo-device/overrides.ts +9 -0
- package/overrides/expo-glass-effect/GlassContainer.tsx +9 -0
- package/overrides/expo-glass-effect/GlassView.tsx +95 -0
- package/overrides/expo-glass-effect/availability.ts +14 -0
- package/overrides/expo-glass-effect/overrides.ts +14 -0
- package/overrides/expo-haptics/Haptics.ts +67 -0
- package/overrides/expo-haptics/overrides.ts +5 -0
- package/overrides/expo-mail-composer/ExpoMailComposer.ts +40 -0
- package/overrides/expo-mail-composer/overrides.ts +9 -0
- package/overrides/expo-mesh-gradient/MeshGradientView.tsx +44 -0
- package/overrides/expo-mesh-gradient/overrides.ts +9 -0
- package/overrides/expo-modules-core/overrides.ts +12 -0
- package/overrides/expo-navigation-bar/ExpoNavigationBar.ts +29 -0
- package/overrides/expo-navigation-bar/NavigationBar.ts +43 -0
- package/overrides/expo-navigation-bar/overrides.ts +12 -0
- package/overrides/expo-router/NativeStackView.tsx +74 -0
- package/overrides/expo-router/NativeTabsView.tsx +263 -0
- package/overrides/expo-router/SafeAreaProvider.ts +6 -0
- package/overrides/expo-router/overrides.ts +23 -0
- package/overrides/expo-router/statusBarRequests.ts +51 -0
- package/overrides/expo-screen-orientation/ExpoScreenOrientation.ts +173 -0
- package/overrides/expo-screen-orientation/overrides.ts +21 -0
- package/overrides/expo-sharing/SharingNativeModule.ts +21 -0
- package/overrides/expo-sharing/overrides.ts +9 -0
- package/overrides/expo-sms/ExpoSMS.ts +24 -0
- package/overrides/expo-sms/overrides.ts +9 -0
- package/overrides/expo-splash-screen/SplashScreen.ts +25 -0
- package/overrides/expo-splash-screen/overrides.ts +5 -0
- package/overrides/expo-status-bar/StatusBar.tsx +44 -0
- package/overrides/expo-status-bar/overrides.ts +5 -0
- package/overrides/expo-store-review/ExpoStoreReview.ts +18 -0
- package/overrides/expo-store-review/overrides.ts +9 -0
- package/overrides/expo-symbols/SymbolView.tsx +192 -0
- package/overrides/expo-symbols/overrides.ts +9 -0
- package/overrides/expo-system-ui/ExpoSystemUI.ts +34 -0
- package/overrides/expo-system-ui/overrides.ts +9 -0
- package/overrides/expo-task-manager/ExpoTaskManager.ts +39 -0
- package/overrides/expo-task-manager/overrides.ts +9 -0
- package/overrides/expo-tracking-transparency/ExpoTrackingTransparency.ts +30 -0
- package/overrides/expo-tracking-transparency/overrides.ts +11 -0
- package/overrides/expo-video/ExpoVideo.ts +16 -0
- package/overrides/expo-video/overrides.ts +9 -0
- package/overrides/lottie-react-native/LottieView.tsx +87 -0
- package/overrides/lottie-react-native/overrides.ts +9 -0
- package/overrides/react-native-pager-view/PagerViewNativeComponent.tsx +254 -0
- package/overrides/react-native-pager-view/overrides.ts +9 -0
- package/overrides/react-native-reanimated/overrides.ts +28 -0
- package/overrides/react-native-safe-area-context/NativeSafeAreaProvider.tsx +40 -0
- package/overrides/react-native-safe-area-context/overrides.ts +12 -0
- package/overrides/react-native-share/NativeRNShare.ts +22 -0
- package/overrides/react-native-share/overrides.ts +12 -0
- package/overrides/react-native-share/platform.ts +14 -0
- package/overrides/react-native-turbo-image/TurboImage.tsx +101 -0
- package/overrides/react-native-turbo-image/overrides.ts +9 -0
- package/overrides/react-native-web/RefreshControl.tsx +57 -0
- package/overrides/react-native-web/overrides.ts +157 -0
- package/overrides/react-native-web/useElementLayout.ts +152 -0
- package/package.json +80 -4
- package/plugins/battery/overrides/expo-battery/ExpoBattery.ts +57 -0
- package/plugins/battery/overrides/expo-battery/overrides.ts +9 -0
- package/plugins/battery/plugin.ts +8 -0
- package/plugins/battery/shell.tsx +56 -0
- package/plugins/battery/state.ts +11 -0
- package/plugins/biometrics/overrides/expo-local-authentication/ExpoLocalAuthentication.ts +90 -0
- package/plugins/biometrics/overrides/expo-local-authentication/overrides.ts +11 -0
- package/plugins/biometrics/plugin.ts +8 -0
- package/plugins/biometrics/shell.tsx +88 -0
- package/plugins/biometrics/state.ts +41 -0
- package/plugins/brightness/app.ts +5 -0
- package/plugins/brightness/overrides/expo-brightness/Brightness.ts +98 -0
- package/plugins/brightness/overrides/expo-brightness/overrides.ts +10 -0
- package/plugins/brightness/plugin.ts +9 -0
- package/plugins/brightness/shell.tsx +55 -0
- package/plugins/brightness/state.ts +19 -0
- package/plugins/calendar/calendar.ts +299 -0
- package/plugins/calendar/overrides/expo-calendar/ExpoCalendar.ts +358 -0
- package/plugins/calendar/overrides/expo-calendar/LegacyExpoCalendar.ts +163 -0
- package/plugins/calendar/overrides/expo-calendar/overrides.ts +14 -0
- package/plugins/calendar/plugin.ts +8 -0
- package/plugins/calendar/shell.tsx +123 -0
- package/plugins/camera/overrides/expo-camera/ExpoCamera.tsx +269 -0
- package/plugins/camera/overrides/expo-camera/ExpoCameraManager.ts +56 -0
- package/plugins/camera/overrides/expo-camera/overrides.ts +12 -0
- package/plugins/camera/plugin.ts +9 -0
- package/plugins/camera/shell.tsx +107 -0
- package/plugins/camera/state.ts +61 -0
- package/plugins/clipboard/overrides/expo-clipboard/ExpoClipboard.ts +138 -0
- package/plugins/clipboard/overrides/expo-clipboard/overrides.ts +9 -0
- package/plugins/clipboard/plugin.ts +8 -0
- package/plugins/clipboard/shell.tsx +122 -0
- package/plugins/clipboard/state.ts +93 -0
- package/plugins/clock/app.ts +25 -0
- package/plugins/clock/plugin.ts +8 -0
- package/plugins/clock/shell.tsx +225 -0
- package/plugins/clock/state.ts +12 -0
- package/plugins/contacts/contacts.ts +125 -0
- package/plugins/contacts/overrides/expo-contacts/ExpoContacts.ts +153 -0
- package/plugins/contacts/overrides/expo-contacts/ExpoContactsNext.ts +232 -0
- package/plugins/contacts/overrides/expo-contacts/overrides.ts +12 -0
- package/plugins/contacts/plugin.ts +8 -0
- package/plugins/contacts/shell.tsx +109 -0
- package/plugins/home-indicator/app.ts +5 -0
- package/plugins/home-indicator/plugin.ts +9 -0
- package/plugins/home-indicator/requests.ts +48 -0
- package/plugins/home-indicator/shell.tsx +68 -0
- package/plugins/home-indicator/state.ts +31 -0
- package/plugins/keep-awake/app.ts +5 -0
- package/plugins/keep-awake/overrides/expo-keep-awake/ExpoKeepAwake.ts +27 -0
- package/plugins/keep-awake/overrides/expo-keep-awake/overrides.ts +9 -0
- package/plugins/keep-awake/plugin.ts +9 -0
- package/plugins/keep-awake/shell.tsx +80 -0
- package/plugins/keep-awake/state.ts +12 -0
- package/plugins/keyboard/Keyboard.tsx +354 -0
- package/plugins/keyboard/app.ts +90 -0
- package/plugins/keyboard/controller.ts +384 -0
- package/plugins/keyboard/fields.ts +244 -0
- package/plugins/keyboard/icons.tsx +283 -0
- package/plugins/keyboard/keyboard.css +362 -0
- package/plugins/keyboard/layouts/android.ts +175 -0
- package/plugins/keyboard/layouts/index.ts +10 -0
- package/plugins/keyboard/layouts/ios.ts +219 -0
- package/plugins/keyboard/layouts/types.ts +80 -0
- package/plugins/keyboard/overrides/react-native-keyboard-controller/KeyboardController.tsx +470 -0
- package/plugins/keyboard/overrides/react-native-keyboard-controller/overrides.ts +9 -0
- package/plugins/keyboard/plugin.ts +13 -0
- package/plugins/keyboard/shell.tsx +133 -0
- package/plugins/keyboard/state.ts +92 -0
- package/plugins/keyboard/suggestions.ts +63 -0
- package/plugins/keyboard/taps.ts +77 -0
- package/plugins/keyboard/typing.ts +112 -0
- package/plugins/locale/app.ts +232 -0
- package/plugins/locale/locale.ts +143 -0
- package/plugins/locale/overrides/expo-localization/ExpoLocalization.ts +41 -0
- package/plugins/locale/overrides/expo-localization/overrides.ts +9 -0
- package/plugins/locale/plugin.ts +9 -0
- package/plugins/locale/shell.tsx +147 -0
- package/plugins/locale/state.ts +36 -0
- package/plugins/location/geo.ts +56 -0
- package/plugins/location/overrides/expo-location/ExpoLocation.ts +235 -0
- package/plugins/location/overrides/expo-location/overrides.ts +11 -0
- package/plugins/location/places.ts +38 -0
- package/plugins/location/plugin.ts +8 -0
- package/plugins/location/shell.tsx +188 -0
- package/plugins/location/state.ts +51 -0
- package/plugins/motion/overrides/expo-sensors/DeviceSensor.ts +169 -0
- package/plugins/motion/overrides/expo-sensors/ExponentPedometer.ts +54 -0
- package/plugins/motion/overrides/expo-sensors/overrides.ts +14 -0
- package/plugins/motion/plugin.ts +8 -0
- package/plugins/motion/sensors.ts +116 -0
- package/plugins/motion/shell.tsx +136 -0
- package/plugins/motion/state.ts +46 -0
- package/plugins/network/app.ts +400 -0
- package/plugins/network/overrides/expo-network/ExpoNetwork.ts +51 -0
- package/plugins/network/overrides/expo-network/overrides.ts +9 -0
- package/plugins/network/plugin.ts +8 -0
- package/plugins/network/shell.tsx +67 -0
- package/plugins/network/state.ts +29 -0
- package/plugins/notifications/center.ts +255 -0
- package/plugins/notifications/content.ts +36 -0
- package/plugins/notifications/overrides/expo-notifications/NotificationsModule.ts +175 -0
- package/plugins/notifications/overrides/expo-notifications/channels.ts +108 -0
- package/plugins/notifications/overrides/expo-notifications/getDevicePushTokenAsync.ts +19 -0
- package/plugins/notifications/overrides/expo-notifications/getExpoPushTokenAsync.ts +16 -0
- package/plugins/notifications/overrides/expo-notifications/overrides.ts +36 -0
- package/plugins/notifications/plugin.ts +8 -0
- package/plugins/notifications/shell.tsx +192 -0
- package/plugins/notifications/state.ts +56 -0
- package/plugins/notifications/tokens.ts +47 -0
- package/plugins/notifications/trigger.ts +107 -0
- package/plugins/permissions/plugin.ts +6 -0
- package/plugins/permissions/shell.tsx +50 -0
- package/plugins/permissions/state.ts +38 -0
- package/plugins/photos/appLibrary.ts +114 -0
- package/plugins/photos/library.ts +322 -0
- package/plugins/photos/overrides/expo-image-picker/ExponentImagePicker.ts +153 -0
- package/plugins/photos/overrides/expo-image-picker/overrides.ts +13 -0
- package/plugins/photos/overrides/expo-media-library/ExpoMediaLibrary.ts +235 -0
- package/plugins/photos/overrides/expo-media-library/NativeMediaLibraryModule.ts +318 -0
- package/plugins/photos/overrides/expo-media-library/overrides.ts +19 -0
- package/plugins/photos/plugin.ts +8 -0
- package/plugins/photos/samples.ts +367 -0
- package/plugins/photos/shell.tsx +164 -0
- package/plugins/photos/state.ts +88 -0
- package/plugins/storage/files.ts +351 -0
- package/plugins/storage/overrides/expo-file-system/ExpoFileSystem.ts +770 -0
- package/plugins/storage/overrides/expo-file-system/ExponentFileSystem.ts +196 -0
- package/plugins/storage/overrides/expo-file-system/overrides.ts +13 -0
- package/plugins/storage/overrides/expo-secure-store/ExpoSecureStore.ts +75 -0
- package/plugins/storage/overrides/expo-secure-store/overrides.ts +9 -0
- package/plugins/storage/plugin.ts +8 -0
- package/plugins/storage/reset.ts +136 -0
- package/plugins/storage/secure.ts +71 -0
- package/plugins/storage/shell.tsx +365 -0
- package/plugins/storage/state.ts +13 -0
- package/src/client/app/appDocument.ts +129 -0
- package/src/client/app/appFocus.ts +97 -0
- package/src/client/app/console.ts +118 -0
- package/src/client/app/fonts.ts +76 -0
- package/src/client/app/main.tsx +99 -0
- package/src/client/app/routeContext.ts +45 -0
- package/src/client/app/touch.ts +640 -0
- package/src/client/entry.ts +20 -0
- package/src/client/modules.d.ts +8 -0
- package/src/client/react-native/app.ts +57 -0
- package/src/client/react-native/deep.ts +13 -0
- package/src/client/react-native/keyboard.tsx +192 -0
- package/src/client/react-native/library.ts +256 -0
- package/src/client/shell/Cookies.tsx +95 -0
- package/src/client/shell/LogDrawer.tsx +415 -0
- package/src/client/shell/PhotoPicker.tsx +174 -0
- package/src/client/shell/PluginOverlays.tsx +42 -0
- package/src/client/shell/PluginPanels.tsx +82 -0
- package/src/client/shell/ResetApp.tsx +87 -0
- package/src/client/shell/Resizer.tsx +99 -0
- package/src/client/shell/Shell.tsx +531 -0
- package/src/client/shell/SystemBars.tsx +376 -0
- package/src/client/shell/SystemCamera.tsx +181 -0
- package/src/client/shell/SystemOverlay.tsx +422 -0
- package/src/client/shell/SystemSheets.tsx +462 -0
- package/src/client/shell/backdrop.ts +229 -0
- package/src/client/shell/control.ts +91 -0
- package/src/client/shell/events.ts +226 -0
- package/src/client/shell/geometry.ts +68 -0
- package/src/client/shell/host.ts +492 -0
- package/src/client/shell/icons.tsx +34 -0
- package/src/client/shell/logFeed.ts +102 -0
- package/src/client/shell/main.tsx +22 -0
- package/src/client/shell/permissionCopy.ts +169 -0
- package/src/client/shell/plugins.ts +266 -0
- package/src/client/shell/popout.ts +185 -0
- package/src/client/shell/popover.ts +27 -0
- package/src/client/shell/shell.css +878 -0
- package/src/client/shell/statusTime.ts +31 -0
- package/src/client/shell/tooltips.ts +104 -0
- package/src/client/virtual.d.ts +23 -0
- package/src/devices/catalog.ts +223 -0
- package/src/devices/types.ts +83 -0
- package/src/node/appInfo.ts +188 -0
- package/src/node/appSource.ts +140 -0
- package/src/node/config.ts +180 -0
- package/src/node/control.ts +144 -0
- package/src/node/doctor.ts +199 -0
- package/src/node/events/cli.ts +206 -0
- package/src/node/events/shared.ts +238 -0
- package/src/node/events/store.ts +266 -0
- package/src/node/fonts.ts +377 -0
- package/src/node/index.ts +5 -0
- package/src/node/overrides/define.ts +56 -0
- package/src/node/overrides/packages.ts +136 -0
- package/src/node/overrides/platformPatch.ts +56 -0
- package/src/node/overrides/plugin.ts +169 -0
- package/src/node/overrides/registry.ts +258 -0
- package/src/node/paths.ts +18 -0
- package/src/node/platform.ts +171 -0
- package/src/node/plugins.ts +98 -0
- package/src/node/proxy.ts +421 -0
- package/src/node/server.ts +254 -0
- package/src/node/worklets.ts +86 -0
- package/src/plugin/index.ts +33 -0
- package/src/plugin/types.ts +127 -0
- package/src/plugin/ui.tsx +562 -0
- package/src/runtime/clock.ts +112 -0
- package/src/runtime/draw.ts +196 -0
- package/src/runtime/index.ts +519 -0
- package/src/runtime/media.ts +379 -0
- package/src/runtime/records.ts +76 -0
- package/src/runtime/timezone.ts +81 -0
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
import { PermissionStatus, type PermissionResponse } from 'expo'
|
|
2
|
+
import { getPermission, getState, plugin, realNow, requestPermission, sessionValue } from 'web-sim/runtime'
|
|
3
|
+
import {
|
|
4
|
+
accelerometer,
|
|
5
|
+
altitude,
|
|
6
|
+
deviceMotion,
|
|
7
|
+
gyroscope,
|
|
8
|
+
magnetometer,
|
|
9
|
+
magnetometerUncalibrated,
|
|
10
|
+
type Phone,
|
|
11
|
+
} from '../../sensors.ts'
|
|
12
|
+
import { INITIAL_MOTION, type MotionState } from '../../state.ts'
|
|
13
|
+
|
|
14
|
+
export { PermissionStatus }
|
|
15
|
+
|
|
16
|
+
type Listener<M> = (event: M) => void
|
|
17
|
+
type Subscription = { remove: () => void }
|
|
18
|
+
|
|
19
|
+
const DEFAULT_INTERVAL_MS = 100
|
|
20
|
+
const MIN_INTERVAL_MS = 10
|
|
21
|
+
|
|
22
|
+
const motion = () => plugin<MotionState>('motion', INITIAL_MOTION)
|
|
23
|
+
|
|
24
|
+
/** Seconds since the simulated phone booted, as the sensors stamp their readings. Shared with expo-device. */
|
|
25
|
+
function uptime(): number {
|
|
26
|
+
const boot = sessionValue('expo-device:boot', () => Date.now() - Math.round((2 + Math.random() * 3) * 86_400_000))
|
|
27
|
+
return (Date.now() - boot) / 1000
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function phone(): Phone {
|
|
31
|
+
const { device, orientation } = getState()
|
|
32
|
+
return { platform: device.platform, orientation }
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** What a reading is made from: the motion panel's state, the phone, the time, and the sensor's own start. */
|
|
36
|
+
type Moment = { state: MotionState; phone: Phone; now: number; intervalMs: number; startAltitude: number }
|
|
37
|
+
|
|
38
|
+
/** Each sensor's reading, by the event its native module sends, as that module shapes it on each platform. */
|
|
39
|
+
const READINGS: Record<string, (moment: Moment) => Record<string, unknown>> = {
|
|
40
|
+
accelerometerDidUpdate: ({ state, phone, now }) => ({ ...accelerometer(state, phone, now), timestamp: uptime() }),
|
|
41
|
+
gyroscopeDidUpdate: ({ state, phone, now }) => ({ ...gyroscope(state, phone, now), timestamp: uptime() }),
|
|
42
|
+
magnetometerDidUpdate: ({ state, phone }) => ({ ...magnetometer(state, phone), timestamp: uptime() }),
|
|
43
|
+
magnetometerUncalibratedDidUpdate: ({ state, phone }) => ({ ...magnetometerUncalibrated(state, phone), timestamp: uptime() }),
|
|
44
|
+
deviceMotionDidUpdate: ({ state, phone, now, intervalMs }) => {
|
|
45
|
+
const timestamp = uptime()
|
|
46
|
+
const ios = phone.platform === 'ios'
|
|
47
|
+
const reading = deviceMotion(state, phone, now)
|
|
48
|
+
return {
|
|
49
|
+
acceleration: { ...reading.acceleration, timestamp },
|
|
50
|
+
accelerationIncludingGravity: { ...reading.accelerationIncludingGravity, timestamp },
|
|
51
|
+
rotation: { ...reading.rotation, timestamp },
|
|
52
|
+
rotationRate: { ...reading.rotationRate, timestamp },
|
|
53
|
+
// iOS gives the interval in seconds, Android in milliseconds.
|
|
54
|
+
interval: ios ? intervalMs / 1000 : intervalMs,
|
|
55
|
+
// `DeviceMotionOrientation`: iOS calls the cutout-left landscape LeftLandscape, Android ROTATION_90.
|
|
56
|
+
orientation: phone.orientation === 'portrait' ? 0 : ios ? -90 : 90,
|
|
57
|
+
}
|
|
58
|
+
},
|
|
59
|
+
// iOS adds the altitude gained since updates began.
|
|
60
|
+
barometerDidUpdate: ({ state, phone, startAltitude }) => ({
|
|
61
|
+
pressure: state.pressure,
|
|
62
|
+
...(phone.platform === 'ios' ? { relativeAltitude: altitude(state.pressure) - startAltitude } : {}),
|
|
63
|
+
timestamp: uptime(),
|
|
64
|
+
}),
|
|
65
|
+
lightSensorDidUpdate: ({ state }) => ({ illuminance: state.lux, timestamp: uptime() }),
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** Sensors the platform has. iOS gives apps no ambient light sensor. */
|
|
69
|
+
function available(eventName: string): boolean {
|
|
70
|
+
return eventName in READINGS && !(eventName === 'lightSensorDidUpdate' && getState().device.platform === 'ios')
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* A sensor of the simulated phone, reading the motion panel: how the phone is held, shakes and walks, the air
|
|
75
|
+
* pressure and the light. Listeners share one timer per sensor, at the interval `setUpdateInterval` sets, and each
|
|
76
|
+
* reading follows the phone's orientation as the shell rotates it.
|
|
77
|
+
*/
|
|
78
|
+
export default class DeviceSensor<Measurement> {
|
|
79
|
+
_nativeModule: unknown
|
|
80
|
+
_nativeEventName: string
|
|
81
|
+
private listeners = new Set<Listener<Measurement>>()
|
|
82
|
+
private intervalMs = DEFAULT_INTERVAL_MS
|
|
83
|
+
private timer: ReturnType<typeof setInterval> | null = null
|
|
84
|
+
private startAltitude = 0
|
|
85
|
+
|
|
86
|
+
constructor(nativeSensorModule: unknown, nativeEventName: string) {
|
|
87
|
+
this._nativeModule = nativeSensorModule
|
|
88
|
+
this._nativeEventName = nativeEventName
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
addListener(listener: Listener<Measurement>): Subscription {
|
|
92
|
+
this.listeners.add(listener)
|
|
93
|
+
this.start()
|
|
94
|
+
return { remove: () => this.removeListener(listener) }
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
hasListeners(): boolean {
|
|
98
|
+
return this.listeners.size > 0
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
getListenerCount(): number {
|
|
102
|
+
return this.listeners.size
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
removeAllListeners(): void {
|
|
106
|
+
this.listeners.clear()
|
|
107
|
+
this.stop()
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
removeSubscription(subscription: Subscription): void {
|
|
111
|
+
subscription.remove()
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
setUpdateInterval(intervalMs: number): void {
|
|
115
|
+
this.intervalMs = Math.max(MIN_INTERVAL_MS, intervalMs)
|
|
116
|
+
if (this.timer !== null) {
|
|
117
|
+
this.stop()
|
|
118
|
+
this.start()
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
async isAvailableAsync(): Promise<boolean> {
|
|
123
|
+
return available(this._nativeEventName)
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/** iOS asks for motion access for device motion; the raw sensors need no permission. */
|
|
127
|
+
async getPermissionsAsync(): Promise<PermissionResponse> {
|
|
128
|
+
return this.needsPermission() ? (getPermission('motion') as PermissionResponse) : granted()
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
async requestPermissionsAsync(): Promise<PermissionResponse> {
|
|
132
|
+
return this.needsPermission() ? ((await requestPermission('motion')) as PermissionResponse) : granted()
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
private needsPermission(): boolean {
|
|
136
|
+
return this._nativeEventName === 'deviceMotionDidUpdate' && getState().device.platform === 'ios'
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
private removeListener(listener: Listener<Measurement>): void {
|
|
140
|
+
this.listeners.delete(listener)
|
|
141
|
+
if (this.listeners.size === 0) {
|
|
142
|
+
this.stop()
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
private start(): void {
|
|
147
|
+
const read = READINGS[this._nativeEventName]
|
|
148
|
+
if (this.timer !== null || read === undefined || !available(this._nativeEventName)) {
|
|
149
|
+
return
|
|
150
|
+
}
|
|
151
|
+
this.startAltitude = altitude(motion().get().pressure)
|
|
152
|
+
this.timer = setInterval(() => {
|
|
153
|
+
const moment = { state: motion().get(), phone: phone(), now: realNow(), intervalMs: this.intervalMs, startAltitude: this.startAltitude }
|
|
154
|
+
const reading = read(moment) as Measurement
|
|
155
|
+
this.listeners.forEach((listener) => listener(reading))
|
|
156
|
+
}, this.intervalMs)
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
private stop(): void {
|
|
160
|
+
if (this.timer !== null) {
|
|
161
|
+
clearInterval(this.timer)
|
|
162
|
+
this.timer = null
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
function granted(): PermissionResponse {
|
|
168
|
+
return { status: PermissionStatus.GRANTED, granted: true, canAskAgain: true, expires: 'never' }
|
|
169
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { getPermission, platform, plugin, requestPermission } from 'web-sim/runtime'
|
|
2
|
+
import { INITIAL_MOTION, type MotionState, stepsToday } from '../../state.ts'
|
|
3
|
+
|
|
4
|
+
const motion = () => plugin<MotionState>('motion', INITIAL_MOTION)
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Steps between two times: today's count from the motion panel, taken evenly since midnight. Earlier days have none.
|
|
8
|
+
*/
|
|
9
|
+
function stepsBetween(start: number, end: number): number {
|
|
10
|
+
const now = Date.now()
|
|
11
|
+
const midnight = new Date(now).setHours(0, 0, 0, 0)
|
|
12
|
+
const overlap = Math.min(end, now) - Math.max(start, midnight)
|
|
13
|
+
if (overlap <= 0 || now <= midnight) {
|
|
14
|
+
return 0
|
|
15
|
+
}
|
|
16
|
+
return Math.round(stepsToday(motion().get(), now) * Math.min(1, overlap / (now - midnight)))
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* The pedometer, counting the motion panel's steps: the count it sets, and the steps a walk adds. A watch hears the
|
|
21
|
+
* steps taken since it began. iOS asks for motion access, Android for activity recognition.
|
|
22
|
+
*/
|
|
23
|
+
const ExponentPedometer = {
|
|
24
|
+
async isAvailableAsync() {
|
|
25
|
+
return true
|
|
26
|
+
},
|
|
27
|
+
async getPermissionsAsync() {
|
|
28
|
+
return getPermission('motion')
|
|
29
|
+
},
|
|
30
|
+
async requestPermissionsAsync() {
|
|
31
|
+
return requestPermission('motion')
|
|
32
|
+
},
|
|
33
|
+
...(platform() === 'ios'
|
|
34
|
+
? {
|
|
35
|
+
async getStepCountAsync(start: number, end: number) {
|
|
36
|
+
return { steps: stepsBetween(start, end) }
|
|
37
|
+
},
|
|
38
|
+
}
|
|
39
|
+
: {}),
|
|
40
|
+
addListener(_eventName: string, listener: (result: { steps: number }) => void) {
|
|
41
|
+
const start = stepsToday(motion().get(), Date.now())
|
|
42
|
+
const remove = motion().watch((next, previous) => {
|
|
43
|
+
const [steps, last] = [stepsToday(next, Date.now()), stepsToday(previous, Date.now())]
|
|
44
|
+
if (steps > last) {
|
|
45
|
+
listener({ steps: Math.max(0, steps - start) })
|
|
46
|
+
}
|
|
47
|
+
})
|
|
48
|
+
return { remove }
|
|
49
|
+
},
|
|
50
|
+
startObserving() {},
|
|
51
|
+
stopObserving() {},
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export default ExponentPedometer
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { defineOverrides } from '../../../../src/node/overrides/define.ts'
|
|
2
|
+
|
|
3
|
+
export default defineOverrides('expo-sensors', [
|
|
4
|
+
{
|
|
5
|
+
versions: '>=58 <59',
|
|
6
|
+
note: "sensors read the motion panel: the phone's pose and rotation, shakes, walks, pressure, light and steps",
|
|
7
|
+
files: {
|
|
8
|
+
// Every sensor but the pedometer is a `DeviceSensor` over its native module: replacing the base class
|
|
9
|
+
// simulates them all and keeps each sensor's own file, types and enums.
|
|
10
|
+
'build/DeviceSensor.js': './DeviceSensor.ts',
|
|
11
|
+
'build/ExponentPedometer.web.js': './ExponentPedometer.ts',
|
|
12
|
+
},
|
|
13
|
+
},
|
|
14
|
+
])
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import type { MotionState } from './state.ts'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* What each sensor reads for the motion panel's state at a moment, shared by the `expo-sensors` override and the
|
|
5
|
+
* panel's live readings. Pure: the same state, platform, orientation and time give the same reading.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
export type Vector = { x: number; y: number; z: number }
|
|
9
|
+
export type Phone = { platform: 'ios' | 'android'; orientation: 'portrait' | 'landscape' }
|
|
10
|
+
|
|
11
|
+
export const GRAVITY = 9.80665
|
|
12
|
+
const RAD = Math.PI / 180
|
|
13
|
+
|
|
14
|
+
/** A shake: a few hundred ms of strong side-to-side acceleration. */
|
|
15
|
+
export const SHAKE_MS = 600
|
|
16
|
+
const SHAKE_HZ = 6
|
|
17
|
+
const SHAKE_G = 2.5
|
|
18
|
+
/** A walk: two steps a second, each a bob of the phone. */
|
|
19
|
+
export const STEPS_PER_SECOND = 2
|
|
20
|
+
const BOB_G = 0.25
|
|
21
|
+
|
|
22
|
+
/** The Earth's field in microtesla at mid-northern latitudes, in world axes (x east, y magnetic north, z up). */
|
|
23
|
+
const EARTH_FIELD: Vector = { x: 0, y: 19.4, z: -38.7 }
|
|
24
|
+
/** A fixed hard-iron offset in the uncalibrated magnetometer. */
|
|
25
|
+
const HARD_IRON: Vector = { x: 41.3, y: -12.8, z: 25.1 }
|
|
26
|
+
|
|
27
|
+
const add = (a: Vector, b: Vector): Vector => ({ x: a.x + b.x, y: a.y + b.y, z: a.z + b.z })
|
|
28
|
+
const scale = (a: Vector, k: number): Vector => ({ x: a.x * k, y: a.y * k, z: a.z * k })
|
|
29
|
+
const ZERO: Vector = { x: 0, y: 0, z: 0 }
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* A vector in world axes, in the phone's axes (x right, y up the screen, z out of it). The pose turns the phone by
|
|
33
|
+
* yaw about the vertical, then pitch about its x axis, then roll about its y axis, as Core Motion's attitude. In
|
|
34
|
+
* landscape the shell has turned the phone a quarter anticlockwise (the cutout on the left) on top of that.
|
|
35
|
+
*/
|
|
36
|
+
function inPhone(world: Vector, state: MotionState, phone: Phone): Vector {
|
|
37
|
+
const [yaw, pitch, roll] = [state.yaw * RAD, state.pitch * RAD, state.roll * RAD]
|
|
38
|
+
// Undo the yaw.
|
|
39
|
+
const a = { x: world.x * Math.cos(yaw) - world.y * Math.sin(yaw), y: world.x * Math.sin(yaw) + world.y * Math.cos(yaw), z: world.z }
|
|
40
|
+
// Undo the pitch.
|
|
41
|
+
const b = { x: a.x, y: a.y * Math.cos(pitch) + a.z * Math.sin(pitch), z: -a.y * Math.sin(pitch) + a.z * Math.cos(pitch) }
|
|
42
|
+
// Undo the roll.
|
|
43
|
+
const c = { x: b.x * Math.cos(roll) - b.z * Math.sin(roll), y: b.y, z: b.x * Math.sin(roll) + b.z * Math.cos(roll) }
|
|
44
|
+
return phone.orientation === 'portrait' ? c : { x: c.y, y: -c.x, z: c.z }
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** Which way the ground is, in the phone's axes, as a unit vector. */
|
|
48
|
+
function down(state: MotionState, phone: Phone): Vector {
|
|
49
|
+
return inPhone({ x: 0, y: 0, z: -1 }, state, phone)
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const shaking = (state: MotionState, now: number) => now < state.shakeUntil
|
|
53
|
+
const walking = (state: MotionState, now: number) => now < state.walkUntil
|
|
54
|
+
|
|
55
|
+
/** The acceleration the user gives the phone, in m/s² in its axes: a shake side to side, a walk's bob. */
|
|
56
|
+
function userAcceleration(state: MotionState, phone: Phone, now: number): Vector {
|
|
57
|
+
let acceleration = ZERO
|
|
58
|
+
if (shaking(state, now)) {
|
|
59
|
+
const side = Math.sin(2 * Math.PI * SHAKE_HZ * ((now % 1000) / 1000)) * SHAKE_G * GRAVITY
|
|
60
|
+
acceleration = add(acceleration, phone.orientation === 'portrait' ? { x: side, y: 0, z: 0 } : { x: 0, y: -side, z: 0 })
|
|
61
|
+
}
|
|
62
|
+
if (walking(state, now)) {
|
|
63
|
+
const bob = Math.sin(2 * Math.PI * STEPS_PER_SECOND * ((now % 1000) / 1000)) * BOB_G * GRAVITY
|
|
64
|
+
acceleration = add(acceleration, inPhone({ x: 0, y: 0, z: bob }, state, phone))
|
|
65
|
+
}
|
|
66
|
+
return acceleration
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** Rotation rate in rad/s in the phone's axes: a shake twists it about its long axis. */
|
|
70
|
+
export function gyroscope(state: MotionState, phone: Phone, now: number): Vector {
|
|
71
|
+
if (!shaking(state, now)) {
|
|
72
|
+
return ZERO
|
|
73
|
+
}
|
|
74
|
+
const twist = Math.cos(2 * Math.PI * SHAKE_HZ * ((now % 1000) / 1000)) * 6
|
|
75
|
+
return phone.orientation === 'portrait' ? { x: 0, y: twist, z: 0 } : { x: twist, y: 0, z: 0 }
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* The accelerometer in g. Android reports the force holding the phone up (+1 on z lying face up); iOS the opposite
|
|
80
|
+
* sign.
|
|
81
|
+
*/
|
|
82
|
+
export function accelerometer(state: MotionState, phone: Phone, now: number): Vector {
|
|
83
|
+
const proper = add(scale(userAcceleration(state, phone, now), 1 / GRAVITY), scale(down(state, phone), -1))
|
|
84
|
+
return phone.platform === 'ios' ? scale(proper, -1) : proper
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** The Earth's field in µT in the phone's axes, turning with the yaw. */
|
|
88
|
+
export function magnetometer(state: MotionState, phone: Phone): Vector {
|
|
89
|
+
return inPhone(EARTH_FIELD, state, phone)
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export function magnetometerUncalibrated(state: MotionState, phone: Phone): Vector {
|
|
93
|
+
return add(magnetometer(state, phone), HARD_IRON)
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** Device motion: gravity's acceleration and the user's in m/s², the attitude in radians, the rotation rate in deg/s. */
|
|
97
|
+
export function deviceMotion(state: MotionState, phone: Phone, now: number) {
|
|
98
|
+
const ground = down(state, phone)
|
|
99
|
+
const user = userAcceleration(state, phone, now)
|
|
100
|
+
const rate = gyroscope(state, phone, now)
|
|
101
|
+
return {
|
|
102
|
+
acceleration: user,
|
|
103
|
+
accelerationIncludingGravity: add(scale(ground, GRAVITY), user),
|
|
104
|
+
rotation: {
|
|
105
|
+
alpha: (((360 - state.yaw) % 360) + 360) % 360 * RAD,
|
|
106
|
+
beta: Math.asin(Math.max(-1, Math.min(1, -ground.y))),
|
|
107
|
+
gamma: Math.atan2(ground.x, -ground.z),
|
|
108
|
+
},
|
|
109
|
+
rotationRate: { alpha: rate.x / RAD, beta: rate.y / RAD, gamma: rate.z / RAD },
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** Metres above sea level for a pressure, by the standard atmosphere. */
|
|
114
|
+
export function altitude(pressure: number): number {
|
|
115
|
+
return 44_330 * (1 - (pressure / 1013.25) ** 0.1903)
|
|
116
|
+
}
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import { DeviceRotateIcon } from '@phosphor-icons/react'
|
|
2
|
+
import { useEffect, useState } from 'react'
|
|
3
|
+
import { Button, defineShellPlugin, Field, NumberField, Readout, Row, Slider } from 'web-sim/shell'
|
|
4
|
+
import { accelerometer, altitude, deviceMotion, gyroscope, magnetometer, type Phone, SHAKE_MS, STEPS_PER_SECOND, type Vector } from './sensors.ts'
|
|
5
|
+
import { INITIAL_MOTION, localDay, type MotionState, stepsToday } from './state.ts'
|
|
6
|
+
|
|
7
|
+
const TICK_MS = 1000 / STEPS_PER_SECOND
|
|
8
|
+
|
|
9
|
+
const POSES = [
|
|
10
|
+
{ label: 'Held', pitch: 70, roll: 0 },
|
|
11
|
+
{ label: 'Flat', pitch: 0, roll: 0 },
|
|
12
|
+
{ label: 'Upright', pitch: 90, roll: 0 },
|
|
13
|
+
{ label: 'Face down', pitch: 180, roll: 0 },
|
|
14
|
+
] as const
|
|
15
|
+
|
|
16
|
+
const fixed = (value: number, digits: number) => (Object.is(Math.round(value * 10 ** digits), -0) ? 0 : value).toFixed(digits)
|
|
17
|
+
const vector = (v: Vector, digits = 2) => `${fixed(v.x, digits)} ${fixed(v.y, digits)} ${fixed(v.z, digits)}`
|
|
18
|
+
const degrees = (radians: number) => `${Math.round((radians * 180) / Math.PI)}°`
|
|
19
|
+
|
|
20
|
+
/** Light from 1 to 100,000 lux on a log scale, so dim rooms get as much of the slider as daylight. */
|
|
21
|
+
const toSlider = (lux: number) => Math.log10(Math.max(1, lux))
|
|
22
|
+
const fromSlider = (value: number) => Math.round(10 ** value)
|
|
23
|
+
|
|
24
|
+
export default defineShellPlugin<MotionState>({
|
|
25
|
+
id: 'motion',
|
|
26
|
+
title: 'Motion',
|
|
27
|
+
icon: DeviceRotateIcon,
|
|
28
|
+
initialState: INITIAL_MOTION,
|
|
29
|
+
actions: {
|
|
30
|
+
/** A few hundred ms of strong acceleration side to side, as apps' shake-to-undo listens for. */
|
|
31
|
+
shake: (context) => {
|
|
32
|
+
context.set({ shakeUntil: Date.now() + SHAKE_MS })
|
|
33
|
+
return context.state()
|
|
34
|
+
},
|
|
35
|
+
/** Walks for `seconds` (default 10): the accelerometer bobs and the pedometer counts two steps a second. */
|
|
36
|
+
walk: (context, args) => {
|
|
37
|
+
const { seconds = 10 } = (args ?? {}) as { seconds?: number }
|
|
38
|
+
context.set({ walkUntil: Date.now() + seconds * 1000 })
|
|
39
|
+
return context.state()
|
|
40
|
+
},
|
|
41
|
+
/** Stops a walk or a shake. */
|
|
42
|
+
stop: (context) => {
|
|
43
|
+
context.set({ walkUntil: 0, shakeUntil: 0 })
|
|
44
|
+
return context.state()
|
|
45
|
+
},
|
|
46
|
+
/** Adds `count` steps to today's. */
|
|
47
|
+
steps: (context, args) => {
|
|
48
|
+
const { count } = args as { count: number }
|
|
49
|
+
const now = Date.now()
|
|
50
|
+
context.set({ steps: stepsToday(context.state(), now) + count, stepsDay: localDay(now) })
|
|
51
|
+
return context.state()
|
|
52
|
+
},
|
|
53
|
+
},
|
|
54
|
+
setup: (context) => {
|
|
55
|
+
const timer = setInterval(() => {
|
|
56
|
+
const state = context.state()
|
|
57
|
+
const now = Date.now()
|
|
58
|
+
const today = localDay(now)
|
|
59
|
+
if (state.stepsDay !== today) {
|
|
60
|
+
// A new day starts from none; a count never given a day is today's.
|
|
61
|
+
context.set({ steps: stepsToday(state, now), stepsDay: today })
|
|
62
|
+
}
|
|
63
|
+
if (now < state.walkUntil) {
|
|
64
|
+
context.set({ steps: context.state().steps + 1 })
|
|
65
|
+
}
|
|
66
|
+
}, TICK_MS)
|
|
67
|
+
return () => clearInterval(timer)
|
|
68
|
+
},
|
|
69
|
+
Panel: ({ state, set, run, sim }) => {
|
|
70
|
+
const now = useNow(state.walkUntil > Date.now() || state.shakeUntil > Date.now() ? 250 : 1000)
|
|
71
|
+
const phone: Phone = { platform: sim.device.platform, orientation: sim.orientation }
|
|
72
|
+
const motion = deviceMotion(state, phone, now)
|
|
73
|
+
const walking = now < state.walkUntil
|
|
74
|
+
return (
|
|
75
|
+
<>
|
|
76
|
+
<Field label="Pitch" value={`${state.pitch}°`}>
|
|
77
|
+
<Slider label="Pitch" value={state.pitch} min={-180} max={180} step={1} onChange={(pitch) => set({ pitch })} />
|
|
78
|
+
</Field>
|
|
79
|
+
<Field label="Roll" value={`${state.roll}°`}>
|
|
80
|
+
<Slider label="Roll" value={state.roll} min={-180} max={180} step={1} onChange={(roll) => set({ roll })} />
|
|
81
|
+
</Field>
|
|
82
|
+
<Field label="Heading" value={`${state.yaw}°`}>
|
|
83
|
+
<Slider label="Heading" value={state.yaw} min={0} max={359} step={1} onChange={(yaw) => set({ yaw })} />
|
|
84
|
+
</Field>
|
|
85
|
+
<Row>
|
|
86
|
+
{POSES.map((pose) => (
|
|
87
|
+
<Button key={pose.label} pressed={state.pitch === pose.pitch && state.roll === pose.roll} onClick={() => set({ pitch: pose.pitch, roll: pose.roll })}>
|
|
88
|
+
{pose.label}
|
|
89
|
+
</Button>
|
|
90
|
+
))}
|
|
91
|
+
</Row>
|
|
92
|
+
<Row>
|
|
93
|
+
<Button title="Shake the phone" pressed={now < state.shakeUntil} onClick={() => run('shake')}>
|
|
94
|
+
Shake
|
|
95
|
+
</Button>
|
|
96
|
+
{walking ? (
|
|
97
|
+
<Button title="Stop walking" pressed onClick={() => run('stop')}>
|
|
98
|
+
Stop walk · {Math.ceil((state.walkUntil - now) / 1000)} s
|
|
99
|
+
</Button>
|
|
100
|
+
) : (
|
|
101
|
+
<Button title="Walk for 10 seconds" onClick={() => run('walk', { seconds: 10 })}>
|
|
102
|
+
Walk 10 s
|
|
103
|
+
</Button>
|
|
104
|
+
)}
|
|
105
|
+
</Row>
|
|
106
|
+
<NumberField label="Steps today" value={stepsToday(state, now)} onChange={(steps) => set({ steps: Math.max(0, Math.round(steps)), stepsDay: localDay(Date.now()) })} />
|
|
107
|
+
<Field label="Pressure" value={`${state.pressure.toFixed(1)} hPa · ${Math.round(altitude(state.pressure))} m`}>
|
|
108
|
+
<Slider label="Pressure" value={state.pressure} min={850} max={1085} step={0.25} onChange={(pressure) => set({ pressure })} />
|
|
109
|
+
</Field>
|
|
110
|
+
{sim.device.platform === 'android' && (
|
|
111
|
+
<Field label="Light" value={`${state.lux.toLocaleString()} lx`}>
|
|
112
|
+
<Slider label="Light" value={toSlider(state.lux)} min={0} max={5} step={0.01} onChange={(value) => set({ lux: fromSlider(value) })} />
|
|
113
|
+
</Field>
|
|
114
|
+
)}
|
|
115
|
+
<Readout
|
|
116
|
+
rows={[
|
|
117
|
+
['Accelerometer g', vector(accelerometer(state, phone, now))],
|
|
118
|
+
['Gyroscope rad/s', vector(gyroscope(state, phone, now))],
|
|
119
|
+
['Magnetometer µT', vector(magnetometer(state, phone), 1)],
|
|
120
|
+
['Attitude', `${degrees(motion.rotation.alpha)} ${degrees(motion.rotation.beta)} ${degrees(motion.rotation.gamma)}`],
|
|
121
|
+
]}
|
|
122
|
+
/>
|
|
123
|
+
</>
|
|
124
|
+
)
|
|
125
|
+
},
|
|
126
|
+
})
|
|
127
|
+
|
|
128
|
+
/** The time, refreshed every `ms`, for the live readings. */
|
|
129
|
+
function useNow(ms: number): number {
|
|
130
|
+
const [now, setNow] = useState(() => Date.now())
|
|
131
|
+
useEffect(() => {
|
|
132
|
+
const timer = setInterval(() => setNow(Date.now()), ms)
|
|
133
|
+
return () => clearInterval(timer)
|
|
134
|
+
}, [ms])
|
|
135
|
+
return now
|
|
136
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* How the phone is held and what its other sensors sense, set in the motion panel. `expo-sensors` reads it; the
|
|
3
|
+
* shell's rotation turns the phone on top of the pose.
|
|
4
|
+
*/
|
|
5
|
+
export type MotionState = {
|
|
6
|
+
/** Degrees the top edge is raised from lying flat, face up: 0 flat, 90 upright, 180 flat face down. */
|
|
7
|
+
pitch: number
|
|
8
|
+
/** Degrees the phone is turned about its long axis, positive with the right edge down. */
|
|
9
|
+
roll: number
|
|
10
|
+
/** The compass heading the phone faces (its top edge lying flat, its back held up), in degrees from magnetic north. */
|
|
11
|
+
yaw: number
|
|
12
|
+
/** Air pressure in hPa. */
|
|
13
|
+
pressure: number
|
|
14
|
+
/** Ambient light in lux (Android's light sensor). */
|
|
15
|
+
lux: number
|
|
16
|
+
/** Steps counted on `stepsDay`. */
|
|
17
|
+
steps: number
|
|
18
|
+
/** The local date (YYYY-MM-DD) `steps` counts; another day's count reads as none today. */
|
|
19
|
+
stepsDay: string
|
|
20
|
+
/** A shake runs until this time, in ms since the epoch. */
|
|
21
|
+
shakeUntil: number
|
|
22
|
+
/** A walk runs until this time, in ms since the epoch. */
|
|
23
|
+
walkUntil: number
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export const INITIAL_MOTION: MotionState = {
|
|
27
|
+
pitch: 70,
|
|
28
|
+
roll: 0,
|
|
29
|
+
yaw: 315,
|
|
30
|
+
pressure: 1013.25,
|
|
31
|
+
lux: 320,
|
|
32
|
+
steps: 2400,
|
|
33
|
+
stepsDay: '',
|
|
34
|
+
shakeUntil: 0,
|
|
35
|
+
walkUntil: 0,
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export function localDay(time: number): string {
|
|
39
|
+
const date = new Date(time)
|
|
40
|
+
return `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, '0')}-${String(date.getDate()).padStart(2, '0')}`
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** Today's steps: the count, unless it was kept for another day. */
|
|
44
|
+
export function stepsToday(state: MotionState, now: number): number {
|
|
45
|
+
return state.stepsDay === '' || state.stepsDay === localDay(now) ? state.steps : 0
|
|
46
|
+
}
|