Created
December 6, 2013 04:44
-
-
Save e-Gizmo/7818713 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Demonstration Sketch for e-Gizmo EZ HMI * | |
* by e-Gizmo Mechatronix Central | |
* | |
* This sketch contains simple communications | |
* interface functions you can adopt and improve | |
* according to your requirements | |
*/ | |
/* How to Connect */ | |
/* EZ HMI TXD ---> gizDuino/Arduino RXD | |
* EZ HMI RXD ---> gizDuino/Arduino TXD | |
* EZ HMI GND ---> gizDuino/Arduino GND | |
*/ | |
char rxmsg[30]; | |
byte stage; | |
byte rxctr; | |
byte msg; | |
void setup(){ | |
Serial.begin(9600); | |
delay(1000); // Allow time for LCD terminal to set up | |
} | |
void loop(void) | |
{ | |
// Test LCD Screen | |
Send_EZHMI("m 1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ_-+="); | |
wait_response(); | |
delay(1000); | |
Send_EZHMI("M 1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ_-+="); | |
wait_response(); | |
// Wait until SW2 is pressed | |
do{ | |
delay(200); | |
Send_EZHMI("K0"); // Read Key 0= SW2 | |
}while(rxmsg[0]=='0'); | |
// Diplay Screen Format | |
Send_EZHMI("3Encoder:"); // "Encoder" on line 0 | |
Send_EZHMI("42: 3: 4: 5: "); // switch ID on line 1 | |
while(1){ | |
delay(200); | |
// Test Keys | |
Send_EZHMI("K0"); // Read Key 0 | |
Send_To("2",rxmsg,"2"); // Display Result | |
Send_EZHMI("K1"); | |
Send_To("2",rxmsg,"6"); | |
Send_EZHMI("K2"); | |
Send_To("2",rxmsg,"10"); | |
Send_EZHMI("K3"); | |
Send_To("2",rxmsg,"14"); | |
Send_To("1"," ","9"); | |
Send_EZHMI("e"); // Read Encoder Switch | |
Send_To("1",rxmsg,"9"); // Display Result on line 0 col 9 | |
} | |
} | |
// Wait for EZ HMI response | |
void wait_response(void){ | |
while(stage !=3) read_buffer(); | |
} | |
/* This routine checks the serial input * | |
* scans for the occurrence of STX and ETX * | |
* and extract the message stripped of STX * | |
* and ETX marker | |
* Rx message in rxmsg[] | |
* stage = 3 indicates RX complete | |
*/ | |
void read_buffer(void){ | |
if(Serial.available()>0){ | |
char rxchar=Serial.read(); // Read one Rxed character | |
if(stage==0){ // Look for STX | |
if(rxchar==0x02){ // STX? | |
stage=1; // STX is found, stage 1 | |
return; | |
} | |
} | |
if(stage==1){ // Store next Rx char until ETX is detected | |
if(rxchar==0x03) // ETX? | |
{ | |
stage=3; // ETX found, data ready=stage 3 | |
rxmsg[rxctr]=0; // NULL String terminator | |
} | |
else | |
{ | |
if(rxctr<30){ // collect data until ETX is found or buffer full | |
rxmsg[rxctr]=rxchar; | |
rxctr++; | |
} | |
} | |
} | |
} | |
} | |
/* This function sends the EZ HMI command | |
* wrapped in STX and ETX marker | |
*/ | |
void Send_EZHMI(char *message){ | |
Serial.write(0x02); //STX | |
Serial.print(message); | |
Serial.write(0x03); //ETX | |
stage=0; //Reset Rx buffer | |
rxctr=0; | |
wait_response(); // Wait until EZ HMI responds | |
} | |
/* Display Function with print start position | |
* line= which line to print to, 0 or 1 | |
* message = message to print | |
* pos = print start position | |
*/ | |
void Send_To(char *line,char *message,char *pos){ | |
String k=message; // Save input string | |
Serial.write(0x02); //STX | |
Serial.print(">"); // Send position info | |
Serial.print(pos); | |
Serial.write(0x03); //ETX | |
stage=0; | |
rxctr=0; | |
wait_response(); | |
Serial.write(0x02); | |
Serial.print(line); // Send message to line | |
Serial.print(k); | |
Serial.write(0x03); | |
stage=0; | |
rxctr=0; | |
wait_response(); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment