Arduino

From Sim Innovations Wiki
Jump to navigation Jump to search

Both Air Manager & Air Player have support for multiple Arduino devices.
You are able to use Air Manager or Air Player to drive LED's, Buttons, Switches, Rotary encoders etc. through the Arduino.

We provide an installer that will flash your Arduino to make it possible to communicate with Air Manager or Air Player automatically using an USB connection.

Info Arduino MEGA 2560, Uno, Micro, Leonardo, Nano and Nano Every are supported, other Arduino models will not work.
Arduino's are supported from Air Manager 3.3 or Air Player 3.3.


Prepare your Arduino

1. Connect your Arduino to your computer using a USB cable.
2. Start Air Manager, and go to the devices tab.
3. Click the Flash button on the top ribbon bar.
4. Select the type of Arduino you have.
5. Select the COM port to which the Arduino is connected.
6. Select the channel. Each Arduino of the same type should have a unique channel assigned to it.
7. Click install and wait until the installer is done.

Use the hardware API to interact with your Arduino. Go to our Hardware API page to learn more.

Arduino Library (SiMessagePort)

Info The Arduino Library is for advanced users only. See above for normal usage.

We offer a way to communicate between your Arduino sketch and an instrument. We have created an Arduino library called the 'Message Port'.

You get to choose which messages you want to send back and forth.

Implementing the Message Port library in your sketch

Download the Message Port library

Download here:
Download

Import the library into Arduino IDE

Unpack the downloaded zip, and import the Message Port library by going to Sketch->Include Library->Add .ZIP Library and select the 'SiMessagePort.zip' file.

Getting started

Below, you will see a very basic example on how to send messages between the Arduino and the instrument.

Instrument code

-- This function will be called when a message is received from the Arduino.
function new_message(id, payload)
  -- Do something with the message from the Arduino
end

id = hw_message_port_add("ARDUINO_MEGA2560_A", new_message)

-- Send a message to the Arduino with id 777 and 3 bytes as payload (0x01, 0x02, 0x03)
hw_message_port_send(id, 777, "BYTE[3]", { 1, 2, 3 })

Arduino sketch

#include <si_message_port.hpp>

SiMessagePort* messagePort;

static void new_message_callback(uint16_t message_id, struct SiMessagePortPayload* payload) {
  // We received a message from Air Manager or Air Player
}

void setup() {
  // Init library on channel A and Arduino type MEGA 2560
  messagePort = new SiMessagePort(SI_MESSAGE_PORT_DEVICE_ARDUINO_MEGA_2560, SI_MESSAGE_PORT_CHANNEL_A, new_message_callback);
}

void loop() {
  // Make sure this function is called regularly
  messagePort->Tick();

  if ( ... ) {
    // Send a message with id 777 and three bytes payload to the instrument
    uint8_t payload[3] = { 0x00, 0x01, 0x02 };
    messagePort->SendMessage(777, payload, 3);

    // There are multiple payload types available (integer, float, byte and string)
    messagePort->SendMessage(777, "hello"); // String
    messagePort->SendMessage(777, (int32_t)-890); // Integer
    messagePort->SendMessage(777, 5.6f); // Float
  }
}

Functions

SiMessagePort (constructor)

SiMessagePort(enum SiMessagePortDevice device, enum SiMessagePortChannel channel, void (*message_callback)(uint16_t message_id, struct SiMessagePortPayload* payload));

Call this function in the Arduino setup() function.

Arguments

# Argument Type Description
1 device enum SiMessagePortDevice Defined the Arduino type. See si_message_port.h for available types.
SI_MESSAGE_PORT_DEVICE_ARDUINO_MEGA_2560
SI_MESSAGE_PORT_DEVICE_ARDUINO_NANO
SI_MESSAGE_PORT_DEVICE_ARDUINO_UNO
SI_MESSAGE_PORT_DEVICE_ARDUINO_MICRO
SI_MESSAGE_PORT_DEVICE_ARDUINO_LEONARDO
SI_MESSAGE_PORT_DEVICE_ARDUINO_NANO_EVERY
SI_MESSAGE_PORT_DEVICE_ESP32
SI_MESSAGE_PORT_DEVICE_NODE_MCU
SI_MESSAGE_PORT_DEVICE_ESP8266
SI_MESSAGE_PORT_DEVICE_TEENSY_2_0
SI_MESSAGE_PORT_DEVICE_TEENSY_PP_2_0
SI_MESSAGE_PORT_DEVICE_TEENSY_LC
SI_MESSAGE_PORT_DEVICE_TEENSY_3_2
SI_MESSAGE_PORT_DEVICE_TEENSY_3_5
SI_MESSAGE_PORT_DEVICE_TEENSY_3_6
SI_MESSAGE_PORT_DEVICE_TEENSY_4_0
SI_MESSAGE_PORT_DEVICE_TEENSY_4_1
2 channel enum SiMessagePortChannel Defined the channel your Arduino should have (A->P).
SI_MESSAGE_PORT_CHANNEL_A
...
SI_MESSAGE_PORT_CHANNEL_P.
3 message_callback function(message_id, payload) Function that will be called when a message is received from the instrument.

Message callback

void (*message_callback)(uint16_t message_id, struct SiMessagePortPayload* payload)
Argument Type Description
message_id uint16_t The message id that was sent from your instrument. Can range from 0 - 65535.
payload struct SiMessagePortPayload* This struct contains information about the payload that was sent from your instrument. The pointer is NULL when no payload has been sent.
payload->type enum SiMessagePortDataType Data type in the payload. Can be:
SI_MESSAGE_PORT_DATA_TYPE_BYTE
SI_MESSAGE_PORT_DATA_TYPE_STRING
SI_MESSAGE_PORT_DATA_TYPE_FLOAT
SI_MESSAGE_PORT_DATA_TYPE_INTEGER
payload->len uint8_t Number of elements in the payload
payload->data_byte uint8_t* The payload data when the payload has type SI_MESSAGE_PORT_DATA_TYPE_BYTE.
payload->data_string char* The payload data when the payload has type SI_MESSAGE_PORT_DATA_TYPE_STRING.
payload->data_int int32_t* The payload data when the payload has type SI_MESSAGE_PORT_DATA_TYPE_INTEGER.
payload->data_float float* The payload data when the payload has type SI_MESSAGE_PORT_DATA_TYPE_FLOAT .

Tick

void Tick();

Call this function in the Arduino loop() function.


SendMessage

SendMessage(uint16_t message_id)
SendMessage(uint16_t message_id, String string)
SendMessage(uint16_t message_id, uint8_t byte)
SendMessage(uint16_t message_id, uint8_t* data, uint8_t len)
SendMessage(uint16_t message_id, int32_t number)
SendMessage(uint16_t message_id, int32_t* numbers, uint8_t len)
SendMessage(uint16_t message_id, float number)
SendMessage(uint16_t message_id, float* numbers, uint8_t len)

Use this function if you want to send a message to an instrument. There are multiple overloads available, depending on what kind of payload you want to send.

Return value

Argument Type Description
result enum SiMessagePortResult Returns if the message has been processed correctly.
SI_MESSAGE_PORT_RESULT_OK, Everything went fine
SI_MESSAGE_PORT_RESULT_ILLEGAL_LEN, You supplied a len bigger then 8
SI_MESSAGE_PORT_RESULT_BUFFER_OVERFLOW, No room in output buffer, try again later

Arguments

# Argument Type Description
1 message_id uint16_t Message id.
n payload * Multiple options are available, depending on what kind of payload type you wish to send.


DebugMessage

DebugMessage(enum SiMessagePortLogLevel level, String message);

Use this function if you want to send debug information to Air Manager or Air Player.

Return value

Argument Type Description
result enum SiMessagePortResult Returns if the message has been processed correctly.
SI_MESSAGE_PORT_RESULT_OK, Everything went fine
SI_MESSAGE_PORT_RESULT_ILLEGAL_LEN, You supplied a len bigger then 8
SI_MESSAGE_PORT_RESULT_BUFFER_OVERFLOW, No room in output buffer, try again later

Arguments

# Argument Type Description
1 level enum SiMessagePortLogLevel Log level
SI_MESSAGE_PORT_LOG_LEVEL_TRACE
SI_MESSAGE_PORT_LOG_LEVEL_DEBUG
SI_MESSAGE_PORT_LOG_LEVEL_INFO
SI_MESSAGE_PORT_LOG_LEVEL_WARN
SI_MESSAGE_PORT_LOG_LEVEL_ERROR
2 message String The debug message.