AN-00066 ViSi DIP Switch

 

Description This application note explains how to configure a DIP switch in the WYSIWYG screen, how to paste the generated code, and how to display the different states.
Supported Processor PICASO, DIABLO16
Supported Environment ViSi
Difficulty

Medium

 

File Downloads
Files

Description

This Application Note shows how to add and configure a DIP switch, one of the widgets available in Workshop. Before getting started, the following are required:

 

and other superseded modules which support the ViSi Genie environment

Visit www.4dsystems.com.au/products to see the latest display module products that use the Diablo16 processor. The display module used in this application note is the uLCD-32PTU, which is a Picaso display. This application note is applicable to Diablo16 display modules as well.

When downloading an application note, a list of recommended application notes is shown. It is assumed that the user has read or has a working knowledge of the topics presented in these recommended application notes.

The ViSi-Genie project and the Arduino sketch are provided as examples to help you along this application note.

 

Application Overview

This application note explains how to configure a DIP switch in the WYSIWYG screen, how to paste the generated code, and how to display the different states. The various orientations and states of a DIP switch are shown below.

Setup Procedure

For instructions on how to launch Workshop 4, how to open a ViSi project, and how to change the target display, kindly refer to the section “Setup Procedure” of the application note

ViSi Getting Started - First Project for Picaso and Diablo16

 

  Create a New Project

For instructions on how to create a new ViSi project, please refer to the section “Create a New Project” of the application note

 

ViSi Getting Started - First Project for Picaso and Diablo16

 

Design the Project

Uncomment the uSD Card Initialization Routine

Remove the block comment symbols as shown below.

The code screen will be updated accordingly, showing the block as an actual part of the code for compilation.

 

 

Leave lines 28 and 29 as they are, since they are not needed in this application.

 

The function file_LoadImageControl(fname1,fname2,mode) in line 30 creates an image control list. It requires two files – fname1 and fname2, the .dat file and .gci file, respectively. These files are created by Workshop. The GCI file contains all the graphics for the images and/or videos created by Workshop. The DAT file contains one line for each image or video, that names the object and gives its starting offset within the GCI and its initial X/Y position. The function returns a handle (pointer to the memory allocation) to the image control list that has been created. This handle will be used to access and display objects, as will be shown later.

 

 

Add a DIP Switch

Go to the Widgets menu, select the Inputs pane, and click on the DIP switch icon.

 

Once the DIP switch is selected, click on the WYSIWYG screen to place it.

 

The Object Inspector shows the different properties of the DIP switch object. Apply the following property values to the DIP switch.

 

 

Insert the DIP Switch Code

Go to the code area and place the cursor just after the handle assignment statement (line 32 in this example).

 

Having selected the DIP switch object, go to the Object Inspector and click on the Paste Code button.

 

The code will be updated accordingly.

 

A new block for the DIP switch is generated, along with comments for each line. Additional explanations are given below.

 

The statement in line 34

 

img_ClearAttributes (hndl, iDipswitch1, I_TOUCH_DISABLE);

 

enables the DIP switch object, Dipswitch1, for touch detection. Not doing this will make Dipswitch1 unresponsive to touch.

 

The command img_Show(hndl,iDipswitch1) displays the DIP switch at the default initial state – state 0.

 

Comment out the statements in lines 37, 39, and 40 for now.

 

 

Change the DIP Switch State From 0 to 1

 

To change the state of the DIP switch, we use the command:

 

Here the value of IMAGE_INDEX for iDipswitch1 is set to 1. The IMAGE_INDEX is the current frame of the object. There are two frames for iDipswitch1 in this case – frame 0 and frame 1, each representing the object at its different states. The following code will illustrate this.

 

Insert the code above to your main program. When compiled, it should display the DIP switch at state 1 for 2 seconds, then at state 0 forever.

Control the DIP Switch with Touch

In this section we will configure the DIP switch to respond to touch. To do this, we will try to simulate how a real DIP switch works.

 

Enable Touch Detection

Before using the touch feature, enable it with the function:

To disable the feature, use the function:

The touch detection feature runs in the background and disabling it when not in use will free up extra resources for the 4DGL CPU cycles.

Check Touch Status

Now that the screen is enabled for touch detection, it needs to be constantly checked for a change in state. The status of a touch response is retrieved by using the following command:

Using the touch_Get() function returns a value depending on the current state. Integers 0 to 3 or their MACRO equivalents are returned based on the following results:

Check if the DIP Switch is Touched

Of course the DIP Switch is only a part of the screen. When the screen is touched, we need to know if the point of touch is within the region of interest, which is the DIP switch. One way to do this is to use the function, img_Touched(handle, index). This function returns back the index if the image is touched or returns -1 if not.

 

Check if What Part of the DIP Switch is Touched

Finally, before deciding which state to display, it necessary that we determine where the last point of touch occurred. To illustrate:

 

The model above can be coded as follows:

 

The formula

 

will assign either 0 or 1 to the variable DIPstate depending on the location of touch and properties of the DIP switch object.

 

For horizontally oriented DIP switch objects, the formula is

 

The formulas above are quite handy when dealing with objects with more than two positions or states. Of course you can have your own way of determining where the point of touch occurred within the object.

 

An Example

Below is a code for a program that initially displays a DIP switch at state 0. The state will then change depending on what part of the switch is touched.  The code comes with a process flow chart to help explain the touch detection part.

 

A Workshop file is attached containing the same code as shown below. For the touch detection part, note that only the TOUCH_RELEASED state was used in this program. There are two other states, namely TOUCH_PRESSED and TOUCH_MOVING. You can easily modify the program and experiment with these states.

 

 

Run the Program

For instructions on how to save a ViSi project, how to connect the target display to the PC, how to select the program destination (this option is not available for Goldelox displays), and how to compile and download a program, please refer to the section “Run the Program” of the application note

ViSi Getting Started - First Project for Picaso and Diablo16

The uLCD-32PTU and uLCD-35DT display modules are commonly used as examples, but the procedure is the same for other displays.

Run the Program

For instructions on how to save a ViSi project, how to connect the target display to the PC, how to select the program destination (this option is not available for Goldelox displays), and how to compile and download a program, please refer to the section “Run the Program” of the application note

ViSi Getting Started - First Project for Picaso and Diablo16

The uLCD-32PTU and uLCD-35DT display modules are commonly used as examples, but the procedure is the same for other displays.

 

Share: