aboutsummaryrefslogtreecommitdiff
path: root/ios/WALT/WALTClient.h
blob: df4c1e0bf9d528e2e562a34ad5ed9d9ec65a3675 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * 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
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * 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.
 */

#import <Foundation/Foundation.h>

#import "MIDIClient.h"
#import "MIDIMessage.h"

extern NSString * const WALTClientErrorDomain;

/** A reasonable timeout to use when reading from the WALT. */
extern const NSTimeInterval kWALTReadTimeout;

typedef NS_ENUM(MIDIByte, WALTCommand) {
  WALTDelayedPingCommand     = 'D',  // Ping with a delay
  WALTResetCommand           = 'F',  // Reset all vars
  WALTSendSyncCommand        = 'I',  // Send some digits for clock sync
  WALTPingCommand            = 'P',  // Ping with a single byte
  WALTVersionCommand         = 'V',  // Determine WALT's firmware version
  WALTReadoutSyncCommand     = 'R',  // Read out sync times
  WALTGShockCommand          = 'G',  // Send last shock time and watch for another
  WALTTimeCommand            = 'T',  // Current time
  WALTZeroSyncCommand        = 'Z',  // Initial zero
  WALTScreenOnCommand        = 'C',  // Send a message on screen color change
  WALTScreenOffCommand       = 'c',
  WALTSendLastScreenCommand  = 'E',  // Send info about last screen color change
  WALTBrightnessCurveCommand = 'U',  // Probe screen for brightness vs time curve
  WALTLaserOnCommand         = 'L',  // Send messages on state change of the laser
  WALTLaserOffCommand        = 'l',
  WALTSendLastLaserCommand   = 'J',
  WALTAudioCommand           = 'A',  // Start watching for signal on audio out line
  WALTBeepCommand            = 'B',  // Generate a tone into the mic and send timestamp
  WALTBeepStopCommand        = 'S',  // Stop generating tone
  WALTMIDICommand            = 'M',  // Start listening for a MIDI message
  WALTNoteCommand            = 'N',  // Generate a MIDI NoteOn message
};

typedef struct {
  char tag;
  NSTimeInterval t;
  int value;
  unsigned int count;
} WALTTrigger;

/**
 * A client for a WALT device.
 *
 * The client will automatically try to connect to any available WALT device, and monitor the system
 * for device connections/disconnections. Users should observe the "connected" key to be notified of
 * connection changes.
 *
 * Most commands produce a corresponding response from the WALT. The -sendCommand:error: method
 * should be used to send the command, and -readResponse used to collect the response.
 */
@interface WALTClient : NSObject <MIDIClientDelegate>
@property (readonly, nonatomic, getter=isConnected) BOOL connected;

/**
 * Returns the base time of the WALT device.
 *
 * The time value is an adjusted version of -currentTime.
 */
@property (readonly, nonatomic) NSTimeInterval baseTime;

/** Returns the number of seconds the system has been awake since it was last restarted. */
@property (readonly, nonatomic) NSTimeInterval currentTime;

/** Initialises the client and attempts to connect to any available WALT device. */
- (instancetype)initWithError:(NSError **)error;

/** Sends a command to the WALT. */
- (BOOL)sendCommand:(WALTCommand)command error:(NSError **)error;

/** Reads a response from the WALT, blocking up to timeout until one becomes available. */
- (NSData *)readResponseWithTimeout:(NSTimeInterval)timeout;

/**
 * Reads a trigger response from the WALT.
 *
 * If an error occurs, the trigger's tag will be '\0'.
 */
- (WALTTrigger)readTriggerWithTimeout:(NSTimeInterval)timeout;

/** Returns YES if the response data contains a valid acknowledgement for a command. */
- (BOOL)checkResponse:(NSData *)response forCommand:(WALTCommand)command;

/** Forces a complete clock synchronisation with the WALT. */
- (BOOL)syncClocksWithError:(NSError **)error;

/** Refreshes the min/max error synchronisation bounds. */
- (BOOL)updateSyncBoundsWithError:(NSError **)error;

@property (readonly, nonatomic) int64_t minError;
@property (readonly, nonatomic) int64_t maxError;

/**
 * Confirms the connection with the WALT (by setting -isConnected).
 *
 * Note that this method will only return NO if there is an error in the connection process. The
 * absence of a device is not such an error.
 */
- (BOOL)checkConnectionWithError:(NSError **)error;

/** Returns the time of the last shock detected by the WALT. */
- (NSTimeInterval)lastShockTimeWithError:(NSError **)error;
@end