Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
learn:tutorials:libraries:linx:misc:custom_command_example [2015/06/29 16:57]
Sudharsan Sukumar
learn:tutorials:libraries:linx:misc:custom_command_example [2015/07/06 17:13] (current)
Sudharsan Sukumar
Line 1: Line 1:
 ====== Custom Command Example ====== ====== Custom Command Example ======
  
-This guide will go over an example on how to use a custom command to control an LCD display. ​ The LCD display used is the PmodCLP by Digilent.+This is an example on how to use a custom command to control an LCD display. ​ The LCD display used is the [[http://​www.digilentinc.com/​Products/​Detail.cfm?​Prod=PMOD-CLP | PmodCLP]] by Digilent. ​ 
 + 
 +{{:​learn:​tutorials:​libraries:​linx:​misc:​20150629_121254.jpg?​300|}}
  
 ==== Prerequisites ==== ==== Prerequisites ====
 ---- ----
-  - [[learn:​libraries:​linx:​getting_started]]+  - [[learn:tutorials:​libraries:​linx:​getting_started]]
   - [[learn:​tutorials:​libraries:​linx:​misc:​adding_custom_command]]   - [[learn:​tutorials:​libraries:​linx:​misc:​adding_custom_command]]
  
 ==== Hardware ====      ==== Hardware ====     
 ----  ---- 
-Any [[learn:​libraries:​linx:​devices | LINX Device]].+Any [[learn:​libraries:​linx:​devices | LINX Device]].  In this case, a chipKIT WF32 is used.
  
 === Software ==== === Software ====
Line 17: Line 19:
  
  
-^  Firmware IDE  ^^+^  Firmware IDE Used  ^^
 ^Device Family^IDE^ ^Device Family^IDE^
-|Arduino|[[http://​arduino.cc/​en/​Main/​Software | Arduino IDE]]| 
 |chipKIT|[[http://​chipkit.net/​started/​install-chipkit-software/​ | MPIDE]]| |chipKIT|[[http://​chipkit.net/​started/​install-chipkit-software/​ | MPIDE]]|
 +// 
 +//
 === Procedure ==== === Procedure ====
 ---- ----
-The idea for this example was to write a string constant, convert into a U8 array, and display ​what you wrote in the string constant on the LCD display using MPIDE and the chipKIT WF32.+The idea for this example was to write a string constant, convert ​that into a U8 array, and display the string constant on the LCD display using MPIDE and the chipKIT WF32.
  
-  ​- Make sure you're using LINX 1.2 or later. +  - Generate Firmware
-  ​Generate the LINX firmware libraries in the appropriate location for your device'​s IDE by opening LabVIEW and under the Tools tab go to MakerHub --> ​ LINX -->  ​Generate Firmware ​Libraries. +
-  - Generally for MPIDE you want to generate the firmware library in C:​\users\*your name*\My Documents\mpide\libraries) +
-  - Restart the device IDE+
   - In the device IDE open the LINX example for the device and interface you're using. (In this case, **File>>​Examples>>​LINX>>​ChipKIT_WF32_Serial**)   - In the device IDE open the LINX example for the device and interface you're using. (In this case, **File>>​Examples>>​LINX>>​ChipKIT_WF32_Serial**)
-  - The examples are read only so save a copy somewhere.  This will be your custom firmware+  - The examples are read only so save a copy somewhere. 
-  - At the bottom of the firmware, after the void loop, add your custom function. ​ The function must return an **int** and must have 4 parameters: **unsigned char**, **unsigned char***, **unsigned char***, **unsigned char***. ​ Below is the function used. \\ +  - At the bottom of the firmware, after the void loop, the custom function ​is added.  The function must return an **int** and must have 4 parameters: **unsigned char**, **unsigned char***, **unsigned char***, **unsigned char***. ​ Below is the function used. \\ 
  
 <sxh c++; title: Custom Command PmodCLP >  <sxh c++; title: Custom Command PmodCLP > 
Line 47: Line 46:
   - **response** is a U8 array. ​ Fill this with the data bytes you want to send back to LabVIEW.   - **response** is a U8 array. ​ Fill this with the data bytes you want to send back to LabVIEW.
   - The function should return 0 (or use the L_OK constant) if everything went well, or an error number otherwise.   - The function should return 0 (or use the L_OK constant) if everything went well, or an error number otherwise.
-  - The following is the code required to initialize the display for 8-bit interface mode.  This includes function set, turning the display on, clearing the display, and set entry mode.  After this initialization process, the cursor will point to the first entry on the LCD display and will be ready to write.+  - The following is the code required to initialize the display for 8-bit interface mode and then write the input from LabVIEW onto the LCD display.  This includes function set, turning the display on, clearing the display, and setting the entry mode.  After this initialization process, the cursor will point to the first entry on the LCD display and will be ready to write.  Since the function must return at least 1 value, the response variable has one entry (0) and the number of response bytes is 1.
  
  
Line 78: Line 77:
   pinMode(DB7,​ OUTPUT);   pinMode(DB7,​ OUTPUT);
   ​   ​
 +int Channels[8] = {34, 35, 36, 37, 30, 31, 32, 33}; //This array contains the data bus pins from DB0 to DB7.
 +
 +int variable[8];​ //use for dummy array in order to bit shift each input byte
 +
 // FUNCTION SET // FUNCTION SET
 delay(30); delay(30);
Line 139: Line 142:
 //end Initialization!! //end Initialization!!
  
-response[0]=0;​ + 
-*numResponseBytes = 1; +//convert input to bytes 
-return 0;+for(int i = 0; i < (numInputBytes);​ i++) 
 +  { 
 +    input[i] = byte(input[i]);​ 
 +  } 
 +   
 +//Read each byte from the input array and write each channel high or low for each bit position in the data bus. 
 +for (int j=0; j < (numInputBytes);​ j++) 
 +  { 
 +    digitalWrite(E,​ HIGH); //prepare for a new letter or number by writing E HIGH and the following RS,RW values 
 +    digitalWrite(RS,​ HIGH); 
 +    digitalWrite(RW,​ LOW); 
 +    for (int i=0; i<8; i++) 
 +    { 
 +      variable[i] = input[j] & 1; 
 +      if (variable[i] == 1) 
 +     { 
 +        digitalWrite(Channels[i],​ HIGH); 
 +     } 
 +     ​else 
 +     { 
 +       ​digitalWrite(Channels[i],​ LOW); 
 +     } 
 +    input[j] = input [j] >> 1; 
 +   } 
 +  digitalWrite(E,​ LOW); //enable the display to show this input 
 +  delayMicroseconds(50);​ //time needed for command to run 
 +  } 
 +  ​response[0]=0;​ 
 +  *numResponseBytes = 1; 
 +  return 0; 
 } }
  
Line 148: Line 181:
  
  
-  - Now the custom command function needs a forward declaration. ​ Above void setup(), add "int myCustomCommand();" +  - Now the custom command function needs a forward declaration. ​ Above void setup(), add **int clp();** 
-  - Next, custom ​commands need to be attached to a **LINX Listener** so they can be called from LabVIEW. ​ This is accomplished using the **AttachCustomCommand()** function of the LINX Listener.+  - Next, the custom ​command is attached to a **LINX Listener** so it can be called from LabVIEW. ​ This is accomplished using the **AttachCustomCommand()** function of the LINX Listener.
   - The **AttachCustomCommand()** takes two arguments:   - The **AttachCustomCommand()** takes two arguments:
-    - The first is the custom command number (0-63). ​ This is the number you'll use in LabVIEW to call this command.+    - The first is the custom command number (0-63). ​ This is the number you'll use in LabVIEW to call this command. In this case, 0 is used.
     - The second is the custom function.     - The second is the custom function.
   - Add the following code to the **setup()** function to attach the custom command to the LINX Listener (in this case called **LinxSerialConnection**) ​   - Add the following code to the **setup()** function to attach the custom command to the LINX Listener (in this case called **LinxSerialConnection**) ​
  
 <sxh c++; title: Example Attach Custom Command To LINX Listener>​ <sxh c++; title: Example Attach Custom Command To LINX Listener>​
- ​LinxSerialConnection.AttachCustomCommand(0, ​myCustomCommand);+ ​LinxSerialConnection.AttachCustomCommand(0, ​clp);
 </​sxh>​ </​sxh>​
  
 +  - Next, firmware is uploaded to the device.
 +  - The Custom Command VI (**Functions Palette>>​MakerHub>>​LINX>>​Utilities>>​Custom Command**) is used to call the custom function.
 +  - Below are the resulting block diagram and front panel for the given code above as well as the LCD display.
 +  - There are only 16 spaces that can be filled on the LCD display, so the code only allows for a size less than or equal to 16.
  
-  - Build and deploy/​upload the firmware to the device+{{ :​learn:​tutorials:​libraries:​linx:​misc:​customfrontclp.png |}} 
-  - Use the Custom Command VI (**Functions Palette>>​MakerHub>>​LINX>>​Utilities>>​Custom Command**) to call the custom function+True Case: 
-  - Below are the resulting block diagram and front panel for the given code above.+{{ :​learn:​tutorials:​libraries:​linx:​misc:​customblockclp.png |}} 
 +False Case: 
 +{{ :​learn:​tutorials:​libraries:​linx:​misc:​false_case.png |}}
  
-{{:​learn:​tutorials:​libraries:​linx:​misc:​customblock.png|}} ​ {{:​learn:​tutorials:​libraries:​linx:​misc:​customfront.png|}}+{{ :​learn:​tutorials:​libraries:​linx:​misc:​20150629_113538.jpg?​300 ​|}}
  
 === Summary ==== === Summary ====
 ---- ----
-This tutorial ​describes the process ​for adding a custom command to a LINX device. ​ It's important to remember that deploying the firmware from the firmware wizard will overwrite the device firmware with the stock version which will not contain your custom commands. ​ If you have any questions about adding customs commands please post on the [[https://​www.labviewmakerhub.com/​forums/​linx | LINX forums]]. ​ If you've successfully added a LINX command, consider sharing your work with the community in the forums.+This tutorial ​provides an example ​for adding a custom command to a LINX device. ​ It's important to remember that deploying the firmware from the firmware wizard will overwrite the device firmware with the stock version which will not contain your custom commands. ​ If you have any questions about adding customs commands please post on the [[https://​www.labviewmakerhub.com/​forums/​linx | LINX forums]]. ​ If you've successfully added a LINX command, consider sharing your work with the community in the forums.