beetlebug▾
/

Module ztcl::device

ztcl::device gives the battery and the network state of a phone, and makes it vibrate or give a haptic feedback.

puts [ztcl::device battery]
puts [ztcl::device network]
set h [ztcl::device watch battery -command {apply {{d} {
    puts "now [dict get $d level] %"
    if {[dict get $d level] >= 82} { set ::done 1 }
}}}]
vwait ::done
ztcl::device cancel $h
level 80 state charging
connected 1 type wifi metered 0 constrained 0
now 80 %
now 81 %
now 82 %

This example runs with ztcl --allow device,event --fake-device: the fake host of the ztcl program gives a phone on Wi-Fi whose battery charges from 80 %. On a phone, the same script gets the battery and the network of the phone.

How a reading arrives

ztcl::device battery and ztcl::device network give the state now, at once, with no event loop. ztcl::device watch battery|network -command SCRIPT gives a handle such as device1 at once, with no reading; the phone then sends the state at the start and at each change, from a thread of the OS, to the event loop of the interpreter. For each one the event loop runs SCRIPT with one more argument, the state as the dict of battery or network, so SCRIPT is a command prefix that takes one argument, such as apply {{d} {...}} or the name of a proc.

So no change arrives while the event loop does not run: a script needs vwait, update, or the event loop of Tk. A watch runs until ztcl::device cancel HANDLE, or until the interpreter ends.

The readings

Reading Type Meaning
battery level integer, % The charge, 0 to 100, or -1 when the device gives no level. iOS gives it in steps of 5 %.
battery state word charging, full (plugged in and full), unplugged, or unknown.
network connected 0 or 1 1 when the device has a network that reaches the internet.
network type word wifi, cellular, ethernet, or none.
network metered 0 or 1 1 for a network that costs, such as cellular or a hotspot.
network constrained 0 or 1 1 in Low Data Mode (iOS) or with Data Saver (Android): the app should use less data.

vibrate and haptic give no reading: they act and give an empty string.

When there is no reading

Case What the script gets
The capability device is off The command does not exist: invalid command name "ztcl::device", with the errorCode TCL LOOKUP COMMAND ztcl::device.
The host has no such service (the ztcl program on a desktop, or no battery on a Mac) The error the battery is not available on this platform (or the network state, vibration, haptics), with the errorCode ZTCL DEVICE UNAVAILABLE. A watch gives the same error.
A device with no motor, such as an iPad, or the iOS Simulator vibrate gives ZTCL DEVICE UNAVAILABLE.
The permission of the user None is asked. On Android, the manifest needs VIBRATE and ACCESS_NETWORK_STATE, which ztk bundle writes for the capability device, and Android grants them at the install.

The rate and the battery

A watch sends the state at the start and at each change, not on a timer of the script. On Android the host reads the battery and the network every 2 seconds and sends a change; iOS sends its own notifications. A watch costs little, but cancel it when the app does not need it.

Platforms

Platform battery network vibrate haptic
iOS (ztk) UIDevice nw_path_monitor Core Haptics UIKit feedback
Android (ztk) BatteryManager ConnectivityManager Vibrator the effects of Vibrator
macOS (ztk) none nw_path_monitor none none
The ztcl program, Linux, Windows none none none none
The web none none none none

The ztcl program has no device; --fake-device gives it the fake host of the examples (src/device_fake.zig).

Commands

Command Description
ztcl::device battery The battery: the dict of the table above.
ztcl::device network The network: the dict of the table above.
ztcl::device vibrate MS Vibrates for MS milliseconds, 0 to 10,000.
ztcl::device vibrate -pattern LIST Vibrates with a pattern: on, off, on, and so on, 1 to 64 times of 0 to 10,000 milliseconds. On iOS a time or a pattern needs Core Haptics.
ztcl::device haptic KIND A haptic feedback: light, medium, heavy, success, warning, or error.
ztcl::device watch battery|network -command SCRIPT Gives a handle, and runs SCRIPT with the dict of battery or network at the start and at each change.
ztcl::device cancel HANDLE Stops a watch. A handle that ended is no error.

The command is not a package: it exists when the embedder turns on the capability device, and it is absent otherwise. A child interp has it only when its parent has it. While a watch is active, the event loop wakes at least every 20 milliseconds, since the host gives the readings from its own thread. The interpreter stops each watch when it ends.

docs/dev/device-apis.md of the repository is the design of the device APIs of iOS and Android: this module, ztcl::location, ztcl::motion, and ztcl::audio. A WebAssembly module gets them through the import families of the device APIs for WebAssembly.

The host

An embedder gives Interp.device_host, a table of functions: battery, network, vibrate, haptic, watch, and stop. Each one can be null, and gives false or null when the device cannot do it now. watch gets a Sink and a handle; the host calls Sink.post with each reading, from any thread.