Your Arduino sketch can request control values and properties from a MegunoLink interface panel by either:
- requesting the property value directly, or
- creating a message library with messages containing the information you want, and invoking the message in the library.
The first approach requires less work, but you can request only one property at a time. Using a message library takes a little more work to setup, but the response can be fully customized and include several property values.
The first approach is described on this page. See “using the message library to format responses” for the second approach.
Use the GetValue
method on an InterfacePanel
variable in your Arduino sketch to request a property value from a control on an interface panel. MegunoLink will respond with the requested property value. Implement a message handler to decode the response. You can use the command handler from our Arduino library to process the response.
The GetValue
method takes two parameters, the:
- name of the control with the property to request, and
- name of the property to request.
Any properties with read or read/write access listed in the control documentation can be requested. Design-only properties cannot be requested.
The picture below shows the flow of serial messages between your Arduino sketch and the MegunoLink interface panel.
Example
Use an InterfacePanel
variable to request a property value on an interface panel visualizer from your Arduino sketch with the GetValue
method.
1 2 3 4 5 6 7 8 9 10 |
#include "MegunoLink.h" void RequestParameters() { Serial.println(F("Requesting parameters")); // numOnTime and numOffTime are the names of numeric up/down controls on the interface panel. // Value is the name of the numeric up/down control's property that returns the current value. ParametersPanel.GetValue(F("numOnTime"), F("Value")); ParametersPanel.GetValue(F("numOffTime"), F("Value")); } |
You’ll also need to handle the response from MegunoLink. Using the CommandHandler
from our Arduino library is one solution. Here the Cmd_SetOnTime
and Cmd_SetOffTime
functions handle the response from the on and off time requests, respectively. Both functions update corresponding variables in the Arduino sketch with values received from MegunoLink.
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 68 69 70 71 72 73 74 75 76 77 |
#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 GotOnTime = false; // True when MegunoLink has supplied the on time value. bool GotOffTime = false; // True when MegunoLink has supplied the off time value. ArduinoTimer RequestTimer; // Timer to periodically request parameters void setup() { Serial.begin(9600); Serial.println(F("Blink Parameter Request Individual Parameters")); Serial.println(F("=============================================")); SerialCommandHandler.AddCommand(F("numOnTime.Value"), Cmd_SetOnTime); SerialCommandHandler.AddCommand(F("numOffTime.Value"), Cmd_SetOffTime); SerialCommandHandler.AddCommand(F("ListAll"), Cmd_ListParameters); pinMode(LED_BUILTIN, OUTPUT); } void loop() { // Check for serial commands and dispatch them. SerialCommandHandler.Process(); // Update the LED if (GotOnTime && GotOffTime) { uint32_t uNow = millis(); digitalWrite(LED_BUILTIN, (uNow - LastBlink < OnTime) ? HIGH : LOW); 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.GetValue(F("numOnTime"), F("Value")); ParametersPanel.GetValue(F("numOffTime"), F("Value")); } void Cmd_SetOnTime(CommandParameter& Parameters) { OnTime = Parameters.NextParameterAsInteger(OnTime); GotOnTime = true; } void Cmd_SetOffTime(CommandParameter& Parameters) { OffTime = Parameters.NextParameterAsInteger(OffTime); GotOffTime = true; } void Cmd_ListParameters(CommandParameter& Parameters) { Serial.print(F("OnTime [ms]=")); Serial.println(OnTime); Serial.print(F("OffTime [ms]=")); Serial.println(OffTime); } |