Requesting data from an interface panel through a message library lets you customize the response MegunoLink sends. For example, you could include multiple values from the interface panel, respond with a specific command or protocol format.
If you only need a single property value and are happy with MegunoLink’s default response format, you can request property values using an InterfacePanel
variable and its GetValue
method.
All messages sent to an interface panel are processed by the MLPUIMessageProcessor. Combining it with a message library allows you request the information and format you want. The steps to set this up are:
- create an interface panel and add controls for the user to enter the parameters you wish to request (see getting started building an Arduino interface),
- add a
MessageLibrary
component to the interface panel, - add a
MLPUIMessageProcessor
component to the interface panel, - set the
MLPUIMessageProcessor
‘sLibrary
property to the message library created above, - add named messages to the
MessageLibrary
that will return the information required by the Arduino sketch, - create an
InterfacePanel
variable in your Arduino sketch and use itsCallCommand
method, passing in one of the named messages defined in the message library, - handle the command sent to your Arduino sketch by the message library (see processing serial commands with an Arduino).
The picture below shows the flow of serial messages between your Arduino sketch and the MegunoLink interface panel.
Set up Interface Panel Components
Add both a MessageLibrary
and MLPUIMessageProcessor
to your interface panel. Both can be found in the Communications group of the interface panel designer toolbox. They can be added to your interface panel by dragging them from the toolbox onto the design surface.
The MessageLibrary
and MLPUIMessageProcessor
live in the component tray at the bottom of the design surface. Unlike buttons and textboxes, they are not visible to the user at run-time.
Link the message library to the message processor component using the processor’s Library
property. Select the MLPUIMessageProcessor
to view its properties and choose the message processor from the dropdown list on the Library
property.
Create Messages
Messages that can be called from your Arduino sketch are stored in the message library. Select the message library component and click the ellipsis () button to open the Message Library Editor.
The Message Library Editor is used to manage the messages stored in the message library. Each message has a:
Name
, uniquely identifying the message in the library,Command
, the response sent to your Arduino sketch when the message is called, andNote
, a short text to help you remember what the message is for (optional).
Use the New button to add a new message. Select the message to edit or delete it. The Command can reference other controls on the interface panel. See creating serial commands for details.
Call the Message and Handle the Response
Use an InterfacePanel
variable to call the message from your Arduino sketch:
1 2 3 4 5 6 7 |
#include "MegunoLink.h" void RequestParameters() { InterfacePanel Panel; Panel.CallCommand(F("GetParameters")); } |
You’ll also need to handle the response from MegunoLink. Using the CommandHandler
from our Arduino library is one solution. Our guide to getting started processing serial commands with an Arduino provides a comprehensive introduction to the command handler.
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 |
#include "MegunoLink.h" #include "CommandHandler.h" #include "ArduinoTimer.h" InterfacePanel ParametersPanel; CommandHandler<> SerialCommandHandler; long LastBlink = 0; // Time we last blinked the LED int OnTime = 10; // Amount of time the LED remains on [milliseconds] int OffTime = 100; // Amount of time the LED remains off [milliseconds] bool GotParameters = false; // True when MegunoLink has supplied the parameters required ArduinoTimer RequestTimer; // Timer to periodically request parameters void setup() { Serial.begin(9600); Serial.println(F("Blink Parameter Requester")); Serial.println(F("=========================")); SerialCommandHandler.AddCommand(F("SetParameters"), Cmd_SetParameters); pinMode(LED_BUILTIN, OUTPUT); } void loop() { // Check for serial commands and dispatch them. SerialCommandHandler.Process(); // Update the LED if (GotParameters) { uint32_t uNow = millis(); digitalWrite(LED_BUILTIN, (uNow - LastBlink < OnTime) ? LOW : HIGH); if (uNow - LastBlink > OnTime + OffTime) { LastBlink = uNow; } } // Request parameters from MegunoLink. if (RequestTimer.TimePassed_Seconds(10)) { RequestParameters(); } } void RequestParameters() { Serial.println(F("Requesting parameters")); ParametersPanel.CallCommand(F("GetParameters")); } void Cmd_SetParameters(CommandParameter &Parameters) { OnTime = Parameters.NextParameterAsInteger(OnTime); OffTime = Parameters.NextParameterAsInteger(OffTime); GotParameters = true; Serial.println(F("Got parameters.")); Serial.print(F("OnTime [ms]=")); Serial.println(OnTime); Serial.print(F("OffTime [ms]=")); Serial.println(OffTime); } |