As part of a series of articles on a set of projects targeted at using a Windows Surface device as the presentation layer for a RPI or Arduino device, this article views the apps as state machines and documents the app states coupling through message passing between them.



Index of Articles

Next: TCPIP Socket Connectivity

Prev: Text Output

Code Repository: (Github) djaus2/SurfPad





The Remote App and the UI App State Machines


The initial states make sure that the two apps are operating in lockstep making sure that the UI app is expecting the Json strings to be sent when the remote app transmits them. Yes its a bit of an overkill with the number of synchronism states before that but it does demonstrate how to implement some detailed state machines.Note that app states change upon reception prior to transmission. For example the remote app transitions from the  Connected state to the ACK0 state when it receives character ‘0’. It then sends character ‘1 to the UI app. Subsequently the UI App transitions from JustConnected to ACK1 when it receives character ‘1’ and then sends back character ‘2’.  An exception is the Remote NotConnected state. When it is able to transmit its StartMsg then it transitions to Connected as this means the UI app has received the message and is therefore connected. Also Json2 only transitions once MenuJson string has been transmitted. Similarly the remote Json2 state transitions upon its transmission completion. The UI Config state transitions to Running when the UI completes configuration using the received Json text.


enum Mode { Disconnected, Connected, ACK0, ACK2,ACK4, Json1, Json2, Running }; Mode mode = Disconnected;

Arduino Sketch State Definitions


//In the loop() funtion having receiveda valid character (!= -1) switch (thisByte) { case '0': if (mode == Connected) { client.write('1'); mode = ACK0; } break;

Arduino Sketch Connected->ACK0 state transition


enum Mode { Disconnected, JustConnected, ACK0, ACK2, ACK4, AwaitJson, JsonConfig, Running } Mode _Mode = Mode.Disconnected;

UWP UI App State Definitions

//In RecvAsync() having received bytes if (_Mode == Mode.JustConnected) { if ('1' == (char)bytes) { _Mode = Mode.ACK1; SendCh('2'); } }

UWP Code for JustConnected –>ACK1 state transition


Running States

When in the running state, the UI app sends a single unique character for each button pressed. It also receives text in response to display in reverse chronological order in the UI’s ListView. There is a special character, ‘^’ that can be sent that will reset the Arduino state machine (as part of a reset of the UI app’s state machine). Also before transitioning to the Running state (in the Config state), the UI app parses the Mainmenu Json string and sets up  the UI.

When in the running state, the Arduino sketch receives individual characters and interprets them as commends to read a sensor or action an actuator. If ‘^’ is received then its state machine is restarted by setting its state to Connected.


After a state machine restart, which would be initiated by the UI app, it is expected that the UI app will reset itself back to the JustConnected state  and send ‘0’ and follow its state machine from there.