whisker.api
Copyright © 2011-2020 Rudolf Cardinal (rudolf@pobox.com).
This file is part of the Whisker Python client library.
Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
Whisker API constants and functions.
- class whisker.api.Brush(colour: Tuple[int, int, int] = (255, 255, 255), bg_colour: Tuple[int, int, int] = (0, 0, 0), opaque: bool = True, style: BrushStyle = BrushStyle.solid, hatch_style: BrushHatchStyle = BrushHatchStyle.cross)[source]
Represents a Whisker Brush graphics object.
- class whisker.api.Pen(width: int = 1, colour: Tuple[int, int, int] = (255, 255, 255), style: PenStyle = PenStyle.solid)[source]
Represents a Whisker Pen graphics object.
- class whisker.api.Rectangle(left: int, top: int, width: int | None = None, height: int | None = None, right: int | None = None, bottom: int | None = None)[source]
Represents a Whisker Rectangle graphics object.
The origin is at the top left, as per Whisker.
- class whisker.api.WhiskerApi(whisker_immsend_get_reply_fn: Callable[[...], str], sysevent_prefix: str = 'sys_', **kwargs)[source]
Whisker API handler.
Distinct from any particular network/threading model, so all can use it (e.g. by inheritance), but hooks in to whichever you choose.
- Parameters:
whisker_immsend_get_reply_fn – A function that must take arguments
*args
, join stringified versions of them using a space as the separator, and send them to the Whisker server via the immediate socket, returning the string that the server sent back.sysevent_prefix – string to prefix system events with
kwargs – used in case of mixin inheritance
- audio_get_sound_duration_ms(device: str, sound: str) int | None [source]
Whisker command: get the duration of a loaded sound.
- Parameters:
device – audio device number/name
sound – sound buffer name
- Returns:
duration in ms, or
None
upon failure
- audio_load_tone(device: str, buffer_: str, frequency_hz: int, tone_type: ToneType, duration_ms: int) bool [source]
Whisker command: synthesize a tone ready to be played.
- Parameters:
device – audio device number/name
buffer – buffer name
frequency_hz – frequency (Hz)
tone_type – tone type (e.g. sine, square, …)
duration_ms – duration (ms)
- Returns:
success?
- audio_load_wav(device: str, sound: str, filename: str) bool [source]
Whisker command: load a WAV file, ready for playing.
- Parameters:
device – audio device number/name
sound – sound buffer name to create
filename – WAV filename on the server
- Returns:
success?
- audio_play_sound(device: str, sound: str, loop: bool = False) bool [source]
Whisker command: play a sound from a named buffer.
- Parameters:
device – audio device number/name
sound – sound buffer name to play
loop – play it on loop?
- Returns:
success?
- audio_play_wav(device: str, filename: str) bool [source]
Whisker command: play a WAV file on an audio device.
- Parameters:
device – audio device number/name
filename – WAV filename on the server
- Returns:
success?
- audio_set_alias(from_: str, to: str) bool [source]
Whisker command: adds an alias for an audio device.
- Parameters:
from – audio device name/number
to – new alias to apply
- Returns:
success?
- audio_set_sound_volume(device: str, sound: str, volume: int) bool [source]
Whisker command: set the volume for a sound.
- Parameters:
device – audio device number/name
sound – sound buffer name to alter
volume – from
0
(100 dB attentuation) to100
(full volume)
- Returns:
success
- audio_stop_sound(device: str, sound: str) bool [source]
Whisker command: stop playing a sound.
- Parameters:
device – audio device number/name
sound – sound buffer name to stop
- Returns:
success?
- audio_unload_all(device: str) bool [source]
Whisker command: unload all sounds from an audio device.
- audio_unload_sound(device: str, sound: str) bool [source]
Whisker command: unload a sound from an audio device.
- Parameters:
device – audio device number/name
sound – sound buffer name to unload
- Returns:
success?
- authenticate_get_challenge(package: str, client_name: str) str | None [source]
Whisker command: ask the server for an authentication challenge, using a particular software package name and client name.
- authenticate_provide_response(response: str) bool [source]
Whisker command: respond to the server’s authentication challenge with an appropriate cryptographic answer.
- broadcast(*args) bool [source]
Broadcasts a message to all other clients of this Whisker server that are listening.
- call_after_delay(delay_ms: int, callback: Callable[[...], None], args: List[Any] | None = None, kwargs: List[Any] | None = None, event: str = '') None [source]
Sets a Whisker timer so we’ll be called back after a delay, and then call a Python function.
- Parameters:
delay_ms – delay in ms
callback – function to call back
args – positional arguments to
callback
kwargs – keyword arguments to
callback
event – optional Whisker event name (a default will be usd if none is provided)
- call_on_event(event: str, callback: Callable[[...], None], args: List[Any] | None = None, kwargs: List[Any] | None = None, swallow_event: bool = False) None [source]
Tells the callback handler to call a function whenever we receive a particular event from Whisker.
- Parameters:
event – Whisker event name
callback – function to call back
args – positional arguments to
callback
kwargs – keyword arguments to
callback
swallow_event – make the API swallow the event, so it’s not seen by our client task?
- claim_audio(number: int | None = None, group: str | None = None, device: str | None = None, alias: str = '') bool [source]
Whisker command: claim an audio device.
- Parameters:
number – device number, for numerical method
group – group name, for named method
device – device name within group, for named method
alias – alias to apply
- Returns:
success?
- claim_display(number: int | None = None, group: str | None = None, device: str | None = None, alias: str = '') bool [source]
Whisker command: claim a dsplay device
- Parameters:
number – device number, for numerical method
group – group name, for named method
device – device name within group, for named method
alias – alias to apply
- Returns:
success?
Autocreating debug views is not supported (see C++ WhiskerClientLib).
- claim_group(group: str, prefix: str = '', suffix: str = '') bool [source]
Whisker command: claim a device group
- Parameters:
group – group name
prefix – prefix to apply to all device names in the group, when creating the alias
suffix – suffix to apply to all device names in the group, when creating the alias
- Returns:
success?
- claim_line(number: int | None = None, group: str | None = None, device: str | None = None, output: bool = False, reset_state: ResetState = ResetState.leave, alias: str = '') bool [source]
Whisker command: claim a digital I/O line.
- Parameters:
number – line number, for numerical method
group – group name, for named method
device – device name, for named method
output – output line? (If not: input line.)
reset_state – if/how to reset this line when the client disconnects
alias – alias to apply
- Returns:
success?
- clear_event_callback(event: str, callback: Callable[[...], None] | None = None) None [source]
Cancels a callback.
- Parameters:
event – Whisker event name
callback – callback function to remove from callback list
- display_add_obj(doc: str, obj: str, obj_type: str, *parameters) bool [source]
Whisker command: add a display object to a document.
This command has more specialized and helpful versions; don’t use it directly.
- display_add_obj_arc(doc: str, obj: str, rect: Rectangle, start, end, pen) bool [source]
Whisker command: adds an Arc display object. See Whisker help for details.
The arc fits into the
rect
.
- display_add_obj_bezier(doc: str, obj: str, start: Tuple[int, int], control1: Tuple[int, int], control2: Tuple[int, int], end: Tuple[int, int], pen: Pen) bool [source]
Whisker command: adds a Bezier display object. See Whisker help for details.
- display_add_obj_bitmap(doc: str, obj: str, pos: Tuple[int, int], filename: str, stretch: bool = False, height: int = -1, width: int = -1, valign: VerticalAlign = VerticalAlign.top, halign: HorizontalAlign = HorizontalAlign.left) bool [source]
Whisker command: adds a Bitmap display object. See Whisker help for details.
- display_add_obj_camcogquadpattern(doc: str, obj: str, pos: Tuple[int, int], pixel_width: int, pixel_height: int, top_left_patterns: List[int], top_right_patterns: List[int], bottom_left_patterns: List[int], bottom_right_patterns: List[int], top_left_colour: Tuple[int, int, int], top_right_colour: Tuple[int, int, int], bottom_left_colour: Tuple[int, int, int], bottom_right_colour: Tuple[int, int, int], bg_colour: Tuple[int, int, int]) bool [source]
Whisker command: adds a CamcogQuadPattern display object. See Whisker help for details.
Patterns are lists (of length 8) of bytes.
- display_add_obj_chord(doc: str, obj: str, rect: Rectangle, line_start: Tuple[int, int], line_end: Tuple[int, int], pen: Pen, brush: Brush) bool [source]
Whisker command: adds a Text display object. See Whisker help for details.
The chord is the intersection of an ellipse (defined by the rect) and a line that intersects it.
- display_add_obj_ellipse(doc: str, obj: str, rect: Rectangle, pen: Pen, brush: Brush) bool [source]
Whisker command: adds an Ellipse display object. See Whisker help for details.
The ellipse fits into the rectangle (and its centre is at the centre of the rectangle).
- display_add_obj_line(doc: str, obj: str, start: Tuple[int, int], end: Tuple[int, int], pen: Pen) bool [source]
Whisker command: adds a Line display object. See Whisker help for details.
- display_add_obj_pie(doc: str, obj: str, rect: Rectangle, arc_start: Tuple[int, int], arc_end: Tuple[int, int], pen: Pen, brush: Brush) bool [source]
Whisker command: adds a Pie display object. See Whisker help for details.
- display_add_obj_polygon(doc: str, obj: str, points: List[Tuple[int, int]], pen: Pen, brush: Brush, alternate: bool = False) bool [source]
Whisker command: adds a Polygon display object. See Whisker help for details.
- display_add_obj_rectangle(doc: str, obj: str, rect: Rectangle, pen: Pen, brush: Brush) bool [source]
Whisker command: adds a Rectangle display object. See Whisker help for details.
- display_add_obj_roundrect(doc: str, obj: str, rect: Rectangle, ellipse_height: int, ellipse_width: int, pen: Pen, brush: Brush) bool [source]
Whisker command: adds a RoundRect display object. See Whisker help for details.
- display_add_obj_text(doc: str, obj: str, pos: Tuple[int, int], text: str, height: int = 0, font: str = '', italic: bool = False, underline: bool = False, weight: int = 0, colour: Tuple[int, int, int] = (255, 255, 255), opaque: bool = False, bg_colour: Tuple[int, int, int] = (0, 0, 0), valign: TextVerticalAlign = TextVerticalAlign.top, halign: TextHorizontalAlign = TextHorizontalAlign.left) bool [source]
Whisker command: adds a Text display object. See Whisker help for details.
- display_add_obj_video(doc: str, video: str, pos: Tuple[int, int], filename: str, loop: bool = False, playmode: VideoPlayMode = VideoPlayMode.wait, width: int = -1, height: int = -1, play_audio: bool = True, valign: VerticalAlign = VerticalAlign.top, halign: HorizontalAlign = HorizontalAlign.left, bg_colour: Tuple[int, int, int] = (0, 0, 0)) bool [source]
Whisker command: adds a Video display object. See Whisker help (http://www.whiskercontrol.com) for details.
- display_blank(device: str) bool [source]
Whisker command: blank a display device (remove any document).
- display_bring_to_front(doc: str, obj: str) bool [source]
Whisker command: bring an object to the front of its document (so it appears in front of, or on top of, other objects).
- display_cache_changes(doc: str) bool [source]
Whisker command: start caching changes to a document (to reduce flicker). When you’ve made all the changes, call
display_show_changes()
.
- display_cache_wrapper(doc: str) Generator[None, None, None] [source]
Context manager to wrap display calls in a “cache changes”, [do stuff], “show changes” sequence.
Use like:
with something.display_cache_wrapper(doc): # do some display-related things
- display_clear_background_event(doc: str, event_type: DocEventType = DocEventType.touch_down) bool [source]
Clears a document event
- Parameters:
doc – document name
event_type – touched, touch released, mouse click, etc.
- Returns:
success?
- display_clear_event(doc: str, obj: str, event_type: DocEventType = DocEventType.touch_down) bool [source]
Whisker command: clears an event from a display object.
- Parameters:
doc – document name
obj – object name
event_type – event type (e.g. touched, released, mouse click, …)
- Returns:
success?
- display_create_device(name: str, resize: bool = True, directdraw: bool = True, rectangle: Rectangle | None = None, debug_touches: bool = False) bool [source]
Whisker command: create a new display device (window).
- Parameters:
name – display name
resize – may the window be resized?
directdraw – use DirectDraw for speed?
rectangle – initial size of the window
debug_touches – if
True
, touches will be shown on all documents displayed on the device
- Returns:
success?
- display_delete_device(device: str) bool [source]
Whisker command: deletes a display device that you’ve created with
display_create_device()
.
- display_delete_obj(doc: str, obj: str) bool [source]
Whisker command: delete a display object from a document.
- display_event_coords(on: bool) bool [source]
Whisker command: enable/disable
x
/y
coordinates being sent with relevant touchscreen/mouse events.
- display_get_document_size(doc: str) Tuple[int, int] | None [source]
Whisker command: get the logical size of a display document.
Returns a
(width, height)
tuple in pixels, orNone
.
- display_get_object_extent(doc: str, obj: str) Rectangle | None [source]
Whisker command: get the rectangle describing the extend of a display object (in logical coordinates).
Returns a
Rectangle
, orNone
.
- display_get_size(device: str) Tuple[int, int] | None [source]
Whisker command: get the size of a display device.
- Parameters:
device – display device number/name
- Returns:
(width, height)
in pixels, orNone
- Return type:
tuple
- display_keyboard_events(doc: str, key_event_type: KeyEventType = KeyEventType.down) bool [source]
Whisker command: enable/disable keyboard events.
- Parameters:
doc – display document
key_event_type – choose “key down” (pressed), “key up” (released), both, or no events.
- Returns:
success?
- display_scale_documents(device: str, scale: bool = True) bool [source]
Whisker command: set document scaling for a display device.
- display_send_to_back(doc: str, obj: str) bool [source]
Whisker command: send an object to the back of its document (so it appears behind, or underneath, other objects).
- display_set_alias(from_: str, to: str) bool [source]
Whisker command: adds an alias for a display device.
- Parameters:
from – audio device name/number
to – new alias to apply
- Returns:
success?
- display_set_audio_device(display_device: str, audio_device: str) bool [source]
Whisker command: sets the audio device associated with a display device, for video playback.
- Parameters:
display_device – display device number/name
audio_device – audio device number/name
- Returns:
success?
- display_set_background_colour(doc: str, colour: Tuple[int, int, int] = (0, 0, 0)) bool [source]
Whisker command: set the background colour of a display document.
- display_set_background_event(doc: str, event: str, event_type: DocEventType = DocEventType.touch_down) bool [source]
Whisker command: set an event for when the background of a document if douched (or released).
- Parameters:
doc – document name
event – event name to create
event_type – touched, touch released, mouse click, etc.
- Returns:
success?
- display_set_document_size(doc: str, width: int, height: int) bool [source]
Whisker command: set the logical size in pixels of a display document.
- display_set_event(doc: str, obj: str, event: str, event_type: DocEventType = DocEventType.touch_down) bool [source]
Whisker command: sets an event (e.g. responding to touchscreen or mouse events) on a display object within a document.
- Parameters:
doc – document name
obj – object name
event – event to set
event_type – event type (e.g. touched, released, mouse click, …)
- Returns:
success?
- display_set_obj_event_transparency(doc: str, obj: str, transparent: bool = False) bool [source]
Whisker command: set object event transparency. Transparency means: an object might respond (or not) to an event, but will it also let objects behind it respond too?
- Parameters:
doc – document name
obj – object name
transparent – transparent?
- Returns:
success?
- display_show_changes(doc: str) bool [source]
Whisker command: show any pending changes (see
display_cache_changes()
).
- display_show_document(device: str, doc: str) bool [source]
Whisker command: show a document on a display device.
- flash_line_ping_pong(line: str, on_now: bool, timing_sequence: List[int]) None [source]
Internal function used by
flash_line_pulses()
.- Parameters:
line – Whisker line number/name
on_now – switch it on now (otherwise off)?
timing_sequence – sequence of [off]/on/off/… times still to do, in ms
- flash_line_pulses(line: str, count: int, on_ms: int, off_ms: int, on_at_rest: bool = False) int [source]
Flash a line for a specified number of pulses.
- Parameters:
line – Whisker line number/name
count – number of flashes
on_ms – time spent in the “on” phase of the cycle, in ms
off_ms – time spent in the “off” phase of the cycle, in ms
on_at_rest – is the line on at rest, so we count “off flashes”? The default, with this
False
, is to assume the line is off at rest and we count “on” flashes.
- Returns:
the total duration of the flashing sequence, in ms
- get_response_with_timestamp(*args) Tuple[str, int | None] [source]
Return the answer from an immediate socket command, with a timestamp.
- get_server_time_ms() int [source]
Whisker command: get the time on the server, from its millisecond clock.
- line_clear_event(event: str) bool [source]
Whisker command: clear (cancel) a line event or events by name.
- line_clear_event_by_line(line: str, event_type: LineEventType) bool [source]
Whisker command: clear (cancel) a line event by line number/name and transition type.
- line_clear_safety_timer(line: str) bool [source]
Whisker command: clears a safety timer for a line.
- line_read_state(line: str) bool | None [source]
Whisker command: reads the state of a digital I/O line.
- Parameters:
line – line number/name
- Returns:
True
for on,False
for off,None
for “problem”
- line_set_alias(line: str, alias: str) bool [source]
Whisker command: adds an alias for a line.
- Parameters:
line – line name/number
alias – new alias to apply
- Returns:
success?
- line_set_event(line: str, event: str, event_type: LineEventType = LineEventType.on) bool [source]
Whisker command: sets an event for a digital I/O line transition.
- Parameters:
line – line number/name
event – event name to create
event_type – fire event upon on transitions, off transitions, or both?
- Returns:
success?
- line_set_safety_timer(line: str, time_ms: int, safety_state: SafetyState) bool [source]
Whisker command: set a safety timer on a line. If the client doesn’t use this line for a while, the safety system will kick in.
- Parameters:
line – line number/name
time_ms – how long after the client’s last action with this line before we consider the line to be being neglected (ms)?
safety_state – turn it off or on after it’s been neglected?
- Returns:
success?
- line_set_state(line: str, on: bool) bool [source]
Whisker command: turns a digital output line on/off.
- log_set_options(events: bool = True, key_events: bool = True, client_client: bool = True, comms: bool = False, signature: bool = True) bool [source]
Whisker command: set log file options.
- Parameters:
events – log events?
key_events – log keyboard events?
client_client – log client/client communications?
comms – log comms?
signature – sign the log digitally?
- permit_client_messages(permit: bool) bool [source]
Whisker command: enable/disable client messages.
- process_backend_event(event: str) bool [source]
Process an through the system event handler.
Returns
True
if the backend API has dealt with the event and it doesn’t need to go to the main behavioural task.
- send_after_delay(delay_ms: int, msg: str, event: str = '') None [source]
Sets a Whisker timer so we’ll be called back after a delay, and then send the command.
- Parameters:
delay_ms – delay in ms
msg – message to send after the delay
event – optional Whisker event name (a default will be usd if none is provided)
- send_to_client(client_num: int, *args) bool [source]
Whisker command: send a message to another client.
- set_media_directory(directory: str) bool [source]
Whisker command: set the server’s media directory.
- timer_set_event(event: str, duration_ms: int, reload_count: int = 0) bool [source]
Whisker command: set a timer.
- Parameters:
event – event name
duration_ms – timer duration (ms)
reload_count –
0
for no reloads, a positive integer for a specified number of reloads,-1
for infinite reloads.
- Returns:
success?
- video_get_duration_ms(doc: str, video: str) int | None [source]
Whisker command: gets a video’s duration.
- Parameters:
doc – display document name
video – video name
- Returns:
duration in ms, or
None
- video_get_time_ms(doc: str, video: str) int | None [source]
Whisker command: gets the current video time (since the start).
- Parameters:
doc – display document name
video – video name
- Returns:
time in ms, or
None
- video_seek_absolute(doc: str, video: str, time_ms: int) bool [source]
Whisker command: execute a “seek” command on a video, using absolute time (milliseconds since the video’s start).
- video_seek_relative(doc: str, video: str, time_ms: int) bool [source]
Whisker command: execute a “seek” command on a video, using relative time (e.g. +5000 ms for 5 seconds forwards; -5000 ms for 5 seconds backwards).
- whisker.api.assert_ducktype_colour(colour: Any) None [source]
If the parameter does not pass the test
is_ducktype_colour()
, raiseValueError
.
- whisker.api.assert_ducktype_pos(pos: Any) None [source]
If the parameter does not pass the test
is_ducktype_pos()
, raiseValueError
.
- whisker.api.is_ducktype_colour(colour: Any) bool [source]
Does the parameter look like a colour
(red, green blue)
tuple, with values for each in the range 0-255?
- whisker.api.is_ducktype_nonnegative_int(x: Any) bool [source]
Does the parameter look like an integer that is 0 or greater?
- whisker.api.is_ducktype_pos(pos: Any) bool [source]
Does the parameter look like a position
(x, y)
tuple, with values that can be made numeric?
- whisker.api.msg_from_args(*args) str [source]
Converts its arguments, which may be of any type, to strings (via
str()
). Then joins the truthy ones them with spaces to make a command.