The Prompt component lets your Arduino program ask the user a question by sending a serial command and receive a response.
To have MegunoLink request confirmation before sending a message, use a button control and set its ConfirmBeforeSending
property to true
.
Property | Access | Type | Method | Description |
---|---|---|---|---|
DefaultPrompt | Design only | string | ShowPrompt | The default message to present to the user when the prompt is triggered. This can be overridden with a custom message sent from your Arduino. |
Title | Design only | string | — | The window title for the prompt message |
OnNoSend | Design only | string | — | Sets the command sent to a remote device when a user clicks the No button on the prompt |
OnYesSend | Design only | string | — | Sets the command sent to a remote device when a user clicks the Yes button on the prompt |
Id | OnNoSend/ OnYesSend only | int | — | The (optional) prompt ID supplied when the prompt is triggered. |
Response | OnNoSend/ OnYesSend only | Yes | No | — | The response selected by the user. |
Example
In this example, the Arduino program asks for confirmation before ‘launching’ a missile using the Prompt component when it receives a command to start the missile launch.
The function Cmd_BeginMissileLaunch
handles the command sent with the Begin Missile Launch! button is clicked on the interface panel. It sends a command back to the interface panel to ask for launch confirmation, which displays the prompt to the user. When the user selects a response on in the prompt window, MegunoLink sends a PromptResponse
command which includes the prompt Id
and response information. You can’t be too careful when launching missiles!
The example Arduino code and MegunoLink project are available in the MegunoLink library. Select Library Examples→Interface Panel→Prompt to open the example.
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 |
#include "MegunoLink.h" #include "CommandHandler.h" #include "ArduinoTimer.h" InterfacePanel Panel; CommandHandler<> SerialCmds; bool WaitingForPromptResponse = false; int CurrentPromptId = 1; ArduinoTimer PromptTimeout; void Cmd_BeginMissileLaunch(CommandParameter& p) { if (WaitingForPromptResponse) { if (PromptTimeout.TimePassed_Seconds(5)) { WaitingForPromptResponse = false; } else { Serial.println(F("Missile launch underway. Waiting for confirmation")); } } if (!WaitingForPromptResponse) { WaitingForPromptResponse = true; Panel.ShowPrompt("MissilePrompt", ++CurrentPromptId, "Do you really want to launch the missile now?"); PromptTimeout.Reset(); } } void Cmd_HandlePromptResponse(CommandParameter& p) { int PromptId = p.NextParameterAsInteger(); const char* Response = p.NextParameter(); if (PromptId == CurrentPromptId) { WaitingForPromptResponse = false; bool Launch = *Response != '\0' && *Response == 'Y'; if (Launch) { Serial.println(F("Fire missiles!!")); } else { Serial.println(F("Launch aborted.")); } } } void setup() { Serial.begin(9600); Serial.println(F("Missile launch program")); SerialCmds.AddCommand(F("BeginLaunch"), Cmd_BeginMissileLaunch); SerialCmds.AddCommand(F("PromptResponse"), Cmd_HandlePromptResponse); } void loop() { SerialCmds.Process(); } |