Microcontroller.cs - UF-CSSALT/SMARTS-SDK-Unity-Asset-Package GitHub Wiki

The microcontroller class’s primary function is to establish and maintain a connection with the white box’s internal Arduino microcontroller. It accepts user inputs and sends them to the Arduino, but also reads in data captured from the Arduino and makes it available to the user. The Microcontroller.cs script is used as a singleton. A single static reference is maintained in the scene. This static Microcontroller can be accessed using the public field Microcontroller.ME. An instance of Microcontroller.cs must exist within the scene for this static reference to be created at Awake(). Any excess instances of the script existing within the scene will be destroyed in Awake().

Namespace

SMARTS_SDK:

To use the Microcontroller.cs script and access its functionality, any scripts must be using the SMARTS_SDK namespace.

Static Fields

Microcontroller.ME:

This is the singleton reference of the Microcontroller.cs script. It provides simple access to any external scripts using the SMARTS_SDK namespace.

Public Enums

InputMode:

  • Microcontroller
  • Replay_Data

MicrocontrollerCommand:

  • Microcontroller_Command_A
  • Microcontroller_Command_B
  • Microcontroller_Command_C
  • Microcontroller_Command_D
  • Microcontroller_Command_E
  • Microcontroller_Command_F
  • Microcontroller_Command_G
  • Microcontroller_Command_H
  • Microcontroller_Command_I
  • Microcontroller_Command_J
  • Microcontroller_Command_K
  • Microcontroller_Command_L
  • Microcontroller_Command_M
  • Microcontroller_Command_N
  • Microcontroller_Command_O
  • Microcontroller_Command_P
  • Microcontroller_Command_Q
  • Microcontroller_Command_R
  • Microcontroller_Command_S
  • Microcontroller_Command_T
  • Microcontroller_Command_U
  • Microcontroller_Command_V
  • Microcontroller_Command_W
  • Microcontroller_Command_X
  • Microcontroller_Command_Y
  • Microcontroller_Command_Z
  • Microcontroller_Command_0
  • Microcontroller_Command_1
  • Microcontroller_Command_2
  • Microcontroller_Command_3
  • Microcontroller_Command_4
  • Microcontroller_Command_5
  • Microcontroller_Command_6
  • Microcontroller_Command_7
  • Microcontroller_Command_8
  • Microcontroller_Command_9

Connection_Status:

  • Connected
  • Establishing_Connection
  • Not_Connected

Public Fields

bool DisableMicrocontrollerConnection:

If true, severs any active connection to the white box Arduino. If false, allows connection to Arduino to be normally established and maintained.

bool Connected (readonly):

In normal operation mode (see CurrentInputMode) returns true if maintained connection to Arduino has been established, false otherwise. In replay mode (see CurrentInputMode) returns true if the most recent replay provided data indicated stable connection at the time of the replay data being recorded, false otherwise.

Connection_Status CurrentConnectionStatus (readonly):

In normal operation mode (see CurrentInputMode) returns Connected if maintained connection to Arduino has been established, Establishing_Connection if a connection attempt is currently being made, Not_Connected otherwise. In replay mode (see CurrentInputMode) returns Connected if the most recent replay provided data indicated stable connection at the time of the replay data being recorded, Establishing_Connection if a connection attempt was being made, Not_Connected otherwise.

bool microcontrollerConnected (inspector only):

Shows true if microcontroller connection is present, false otherwise.

Connection_Status currentMicrocontrollerConnectionStatus (inspector only): Shows the current state of the microcontroller connection irrespective of current operation mode. See currentConnectionStatus for further elaboration.

string SpecifyComPortNumber:

If not null or empty, any subsequent connection attempts will be made to this specified COM port. Specifying the COM port while already connected will not sever the current connection to the Arduino. If null or empty, a connection attempt will be made individually to each available COM port.

string currentConnectedPort (readonly):

If a stable connection to a SerialPort has been made, will return the name of the Port (i.e. COM5). If no connection found, returns “N/A-No Connection.”

InputMode CurrentInputMode:

Determines the operating mode of the microcontroller. Depending on the defined InputMode, certain elements of the microcontroller are displayed using either the current microcontroller input, or given replay data. The fields affected by this InputMode are:

  • Connected
  • CurrentConnectionStatus
  • MicrocontrollerData

When the current input mode is set to Replay_Data, these fields reflect the state of this microcontroller at the time the replay data was recorded. A microcontroller connection will still be maintained in the background, and its state can be seen in the inspector by using the field microcontrollerConnected, however, the Connected field will reflect the replay data.

string ReplayDataInput:

This field is used to give the Microcontroller input data. When in replay mode (see CurrentInputMode), any data recorded using the ReplayFormattedMicrocontrollerInputString field will be parsed in update, and the state of the microcontroller at the time of recording will be replicated. To view all fields updated differently in replay mode see CurrentInputMode.

string ReplayFormattedMicrocontrollerInputString (readonly):

This field is used to save raw microcontroller input to later replay using the microcontroller. The microcontroller has built in replay functionality and can replay the microcontroller state at the time of this string’s recording natively at a later time. This string cannot be accessed when in replay mode (see CurrentInputMode).

string[] MicrocontrollerData (readonly):

Microcontroller data sent from the Arduino is analyzed, and split into this array using the comma as a delimiter. This data can be access by other software elements to discern information about the state of the Arduino elements.

bool DebuggingActive:

When true, uses Debug.Log() to print useful information about connection attempts to the console. Otherwise, does nothing.

bool ErrorDebuggingActive:

When true, uses Debug.LogError() to print information to the console about critical errors, exceptions and malfunctions. When true, this information will print to the console in an executable built as a Development Build.

Public Methods

void SendCommand(MicrocontrollerCommand command)

Used to send information to the Arduino. Commands get sent to the Arduino after each iteration of the microcontroller receiving data from the Arduino. All commands get sent one after the other, and only once per send cycle. The microcontroller connection is maintained on a separate thread so this data is not sent from Update() or LateUpdate(), but rather once data has been received from the microcontroller.

Input Parameters:
MicrocontrollerCommand command:

This is the command the user wishes to send to the microcontroller. It uses the MicrocontrollerCommand enum of the Microcontroller class.