qt60168_example.c File Reference


Detailed Description

QT60168 driver example driver for AVR32 UC3.

This file provides an example for the QT60168 driver.

Author:
Atmel Corporation: http://www.atmel.com
Support and FAQ: http://support.atmel.no/

Definition in file qt60168_example.c.

#include "board.h"
#include "gpio.h"
#include "spi.h"
#include "pm.h"
#include "print_funcs.h"
#include "usart.h"
#include "et024006dhu.h"
#include "qt60168.h"
#include "conf_qt60168.h"

Go to the source code of this file.

Defines

#define COS0   1
#define COS30   0.87
#define COS60   0.5
#define COS90   0
#define DEMO_SIZE_X   ET024006_WIDTH
#define DEMO_SIZE_Y   160
#define DEMO_START_X   0
#define DEMO_START_Y   38
#define DEMO_WHEEL_RADIUS   60
#define DEMO_WHEEL_SIZE_X   20
#define DEMO_WHEEL_SIZE_Y   20
#define DEMO_WHEEL_START_X   ET024006_WIDTH/2 - DEMO_WHEEL_SIZE_X/2
#define DEMO_WHEEL_START_Y   ET024006_HEIGHT/2 - DEMO_WHEEL_SIZE_Y/2
#define SIN0   0
#define SIN30   0.5
#define SIN60   0.87
#define SIN90   1

Enumerations

enum  demo_color_t {
  DEMO_COLOR_ALL = 0, DEMO_COLOR_BLUE, DEMO_COLOR_RED, DEMO_COLOR_GREEN,
  DEMO_COLOR_MAX
}
enum  demo_display_t { DEMO_DISPLAY_BOXES = 0, DEMO_DISPLAY_WHEEL, DEMO_DISPLAY_MAX }

Functions

int main (void)
 Main function.
static void qt60168_resources_init (void)
 Initializes QT60168 resources: GPIO and SPI.


Define Documentation

#define COS0   1

Definition at line 108 of file qt60168_example.c.

Referenced by main().

#define COS30   0.87

Definition at line 109 of file qt60168_example.c.

Referenced by main().

#define COS60   0.5

Definition at line 110 of file qt60168_example.c.

Referenced by main().

#define COS90   0

Definition at line 111 of file qt60168_example.c.

Referenced by main().

#define DEMO_SIZE_X   ET024006_WIDTH

Definition at line 94 of file qt60168_example.c.

Referenced by main().

#define DEMO_SIZE_Y   160

Definition at line 95 of file qt60168_example.c.

Referenced by main().

#define DEMO_START_X   0

Definition at line 92 of file qt60168_example.c.

Referenced by main().

#define DEMO_START_Y   38

Definition at line 93 of file qt60168_example.c.

Referenced by main().

#define DEMO_WHEEL_RADIUS   60

Definition at line 97 of file qt60168_example.c.

Referenced by main().

#define DEMO_WHEEL_SIZE_X   20

Definition at line 100 of file qt60168_example.c.

Referenced by main().

#define DEMO_WHEEL_SIZE_Y   20

Definition at line 101 of file qt60168_example.c.

Referenced by main().

#define DEMO_WHEEL_START_X   ET024006_WIDTH/2 - DEMO_WHEEL_SIZE_X/2

Definition at line 98 of file qt60168_example.c.

Referenced by main().

#define DEMO_WHEEL_START_Y   ET024006_HEIGHT/2 - DEMO_WHEEL_SIZE_Y/2

Definition at line 99 of file qt60168_example.c.

Referenced by main().

#define SIN0   0

Definition at line 103 of file qt60168_example.c.

Referenced by main().

#define SIN30   0.5

Definition at line 104 of file qt60168_example.c.

Referenced by main().

#define SIN60   0.87

Definition at line 105 of file qt60168_example.c.

Referenced by main().

#define SIN90   1

Definition at line 106 of file qt60168_example.c.

Referenced by main().


Enumeration Type Documentation

Enumerator:
DEMO_COLOR_ALL 
DEMO_COLOR_BLUE 
DEMO_COLOR_RED 
DEMO_COLOR_GREEN 
DEMO_COLOR_MAX 

Definition at line 158 of file qt60168_example.c.

00159 {
00160   DEMO_COLOR_ALL=0
00161 , DEMO_COLOR_BLUE
00162 , DEMO_COLOR_RED
00163 , DEMO_COLOR_GREEN
00164 , DEMO_COLOR_MAX
00165 } demo_color_t;

Enumerator:
DEMO_DISPLAY_BOXES 
DEMO_DISPLAY_WHEEL 
DEMO_DISPLAY_MAX 

Definition at line 167 of file qt60168_example.c.

00168 {
00169   DEMO_DISPLAY_BOXES=0
00170 , DEMO_DISPLAY_WHEEL
00171 , DEMO_DISPLAY_MAX
00172 } demo_display_t;


Function Documentation

int main ( void   ) 

Main function.

Definition at line 176 of file qt60168_example.c.

References COS0, COS30, COS60, COS90, DEMO_COLOR_ALL, DEMO_COLOR_BLUE, DEMO_COLOR_GREEN, DEMO_COLOR_MAX, DEMO_COLOR_RED, DEMO_DISPLAY_BOXES, DEMO_DISPLAY_MAX, DEMO_DISPLAY_WHEEL, DEMO_SIZE_X, DEMO_SIZE_Y, DEMO_START_X, DEMO_START_Y, DEMO_WHEEL_RADIUS, DEMO_WHEEL_SIZE_X, DEMO_WHEEL_SIZE_Y, DEMO_WHEEL_START_X, DEMO_WHEEL_START_Y, qt60168_init(), qt60168_is_key_pressed(), qt60168_resources_init(), SIN0, SIN30, SIN60, and SIN90.

00177 {
00178   int i;
00179   Bool idle=FALSE; // Detect key transition (PRESSED -> RELEASED)
00180   U32 x_start;
00181   U32 y_start;
00182   U32 x_size;
00183   U32 y_size;
00184   U16 color;
00185   const U16 icon[QT60168_TOUCH_NUMBER_OF_SENSORS] = {0, 1*16, 2*16, 3*16, 4*16, 5*16, -1, -1, 6*16, 7*16, 8*16, 9*16, 10*16, 11*16, -1, -1};
00186   demo_color_t    demo_color=DEMO_COLOR_ALL;
00187   demo_display_t  demo_display=DEMO_DISPLAY_WHEEL;
00188   Bool touch_states[QT60168_TOUCH_NUMBER_OF_SENSORS];
00189 
00190   // Switch the main clock to the external oscillator 0
00191   pm_switch_to_osc0(&AVR32_PM, FOSC0, OSC0_STARTUP);
00192 
00193   // Initialize RS232 debug text output.
00194   init_dbg_rs232(FOSC0);
00195 
00196   // Initialize QT60168 resources: GPIO, SPI and QT60168.
00197   qt60168_resources_init();
00198 
00199   // Initialize QT60168 component.
00200   qt60168_init(FOSC0);
00201 
00202   // Initialize the LCD.
00203   et024006_Init(  FOSC0/*CPU*/, FOSC0/*HSB*/);
00204 
00205   // Clear the display i.e. make it black
00206   et024006_DrawFilledRect(0, 0, ET024006_WIDTH, ET024006_HEIGHT, BLACK );
00207 
00208   // Set the backlight.
00209   gpio_set_gpio_pin(ET024006DHU_BL_PIN);
00210 
00211   // Display welcome string.
00212   et024006_PrintString("QT60168 EXAMPLE", (const unsigned char *)&FONT8x8, 110,  5, WHITE, -1);
00213   et024006_PrintString("Press the QTouch sensors.", (const unsigned char *)&FONT6x8,  95, 20, WHITE, -1);
00214   et024006_PrintString("Color: All", (const unsigned char *)&FONT6x8,  10, 200, WHITE, -1);
00215   et024006_PrintString("Display sensors", (const unsigned char *)&FONT6x8,  120, 200, WHITE, -1);
00216 
00217   et024006_DrawLine(DEMO_START_X, DEMO_START_Y-1, DEMO_START_X+DEMO_SIZE_X, DEMO_START_Y-1, WHITE );
00218   et024006_DrawLine(DEMO_START_X, DEMO_START_Y+DEMO_SIZE_Y+1, DEMO_START_X+DEMO_SIZE_X, DEMO_START_Y+DEMO_SIZE_Y+1, WHITE );
00219 
00220   // Memorize the status for each key.
00221   for( i=0 ; i<QT60168_TOUCH_NUMBER_OF_SENSORS ; i++ )
00222     touch_states[i] = qt60168_is_key_pressed(i);
00223 
00224   // Set LED state in a known state.
00225   gpio_set_gpio_pin(LED0_GPIO);
00226   gpio_set_gpio_pin(LED1_GPIO);
00227   gpio_set_gpio_pin(LED2_GPIO);
00228   gpio_set_gpio_pin(LED3_GPIO);
00229 
00230   while(1)
00231   {
00232     for( i=0 ; i<QT60168_TOUCH_NUMBER_OF_SENSORS ; i++)
00233     {
00234       // Test Press event on sensors
00235       //
00236       if( !touch_states[i] && qt60168_is_key_pressed(i) )
00237       {
00238         touch_states[i] = TRUE;
00239 
00240         if( i==QT60168_TOUCH_SENSOR_BUTTON_0 )
00241         {
00242           gpio_tgl_gpio_pin(LED0_GPIO);
00243           et024006_PrintString("B0", (const unsigned char *)&FONT6x8,  10, 215, WHITE, -1);
00244           demo_color=(demo_color+1) % DEMO_COLOR_MAX;
00245 
00246           // Erase previous line
00247           et024006_DrawFilledRect(10, 200, 80, 10, BLACK );
00248           switch( demo_color )
00249           {
00250           case DEMO_COLOR_BLUE:
00251             et024006_PrintString("Color: Blue", (const unsigned char *)&FONT6x8,  10, 200, WHITE, -1);
00252             break;
00253           case DEMO_COLOR_RED:
00254             et024006_PrintString("Color: Red", (const unsigned char *)&FONT6x8,  10, 200, WHITE, -1);
00255             break;
00256           case DEMO_COLOR_GREEN:
00257             et024006_PrintString("Color: Green", (const unsigned char *)&FONT6x8,  10, 200, WHITE, -1);
00258             break;
00259           default:
00260             et024006_PrintString("Color: All", (const unsigned char *)&FONT6x8,  10, 200, WHITE, -1);
00261             break;
00262           }
00263         }
00264         else if( i==QT60168_TOUCH_SENSOR_BUTTON_1 )
00265         {
00266           gpio_tgl_gpio_pin(LED1_GPIO);
00267           et024006_PrintString("B1", (const unsigned char *)&FONT6x8,  30, 215, WHITE, -1);
00268           demo_display=(demo_display+1) % DEMO_DISPLAY_MAX;
00269 
00270           // Erase previous line
00271           et024006_DrawFilledRect(120, 200, 160, 10, BLACK );
00272           switch( demo_display )
00273           {
00274           case DEMO_DISPLAY_WHEEL:
00275             et024006_PrintString("Display sensors", (const unsigned char *)&FONT6x8,  120, 200, WHITE, -1);
00276             break;
00277           case DEMO_DISPLAY_BOXES:
00278           default:
00279             et024006_PrintString("Display random boxes", (const unsigned char *)&FONT6x8,  120, 200, WHITE, -1);
00280             break;
00281           }
00282           // Erase display
00283           et024006_DrawFilledRect(DEMO_START_X, DEMO_START_Y, DEMO_SIZE_X, DEMO_SIZE_Y, BLACK );
00284         }
00285         else if( i==QT60168_TOUCH_SENSOR_BUTTON_2 )
00286         {
00287           gpio_tgl_gpio_pin(LED2_GPIO);
00288           et024006_PrintString("B2", (const unsigned char *)&FONT6x8,  50, 215, WHITE, -1);
00289         }
00290 
00291         else if( i==QT60168_TOUCH_SENSOR_BUTTON_3 )
00292         {
00293           gpio_tgl_gpio_pin(LED3_GPIO);
00294           et024006_PrintString("B3", (const unsigned char *)&FONT6x8,  70, 215, WHITE, -1);
00295         }
00296         else
00297         {
00298           // Press transition detected for the wheel
00299           idle = FALSE;
00300 
00301           // Draw Wheel[i]
00302           et024006_DrawFilledRect(100 + icon[i], 215-2, 10, 10, WHITE );
00303         }
00304       }
00305 
00306 
00307 
00308       // Test Release event on sensors
00309       //
00310       if(touch_states[i] && !qt60168_is_key_pressed(i))
00311       {
00312         touch_states[i] = FALSE;
00313         if( i==QT60168_TOUCH_SENSOR_BUTTON_0 )
00314         { // Erase "B0"
00315           et024006_DrawFilledRect(10, 215-2, 12, 12, BLACK );
00316         }
00317         else if( i==QT60168_TOUCH_SENSOR_BUTTON_1 )
00318         { // Erase "B1"
00319           et024006_DrawFilledRect(30, 215-2, 12, 12, BLACK );
00320         }
00321         else if( i==QT60168_TOUCH_SENSOR_BUTTON_2 )
00322         { // Erase "B2"
00323           et024006_DrawFilledRect(50, 215-2, 12, 12, BLACK );
00324         }
00325         else if( i==QT60168_TOUCH_SENSOR_BUTTON_3 )
00326         { // Erase "B3"
00327           et024006_DrawFilledRect(70, 215-2, 12, 12, BLACK );
00328         }
00329         else
00330         { // Erase Wheel[i]
00331           et024006_DrawFilledRect(100 + icon[i], 215-2, 10, 10, BLACK );
00332         }
00333       }
00334     } // for...
00335 
00336 
00337 
00338     if( demo_display==DEMO_DISPLAY_WHEEL )
00339     {
00340       if( touch_states[QT60168_TOUCH_SENSOR_BUTTON_0] ) color = WHITE;
00341       else                                              color = BLUE;
00342       et024006_DrawFilledRect(30, 50, DEMO_WHEEL_SIZE_X, DEMO_WHEEL_SIZE_Y, color );
00343 
00344       if( touch_states[QT60168_TOUCH_SENSOR_BUTTON_1] ) color = WHITE;
00345       else                                              color = BLUE;
00346       et024006_DrawFilledRect(30, 80, DEMO_WHEEL_SIZE_X, DEMO_WHEEL_SIZE_Y, color );
00347 
00348       if( touch_states[QT60168_TOUCH_SENSOR_BUTTON_2] ) color = WHITE;
00349       else                                              color = BLUE;
00350       et024006_DrawFilledRect(30, 110, DEMO_WHEEL_SIZE_X, DEMO_WHEEL_SIZE_Y, color );
00351 
00352       if( touch_states[QT60168_TOUCH_SENSOR_BUTTON_3] ) color = WHITE;
00353       else                                              color = BLUE;
00354       et024006_DrawFilledRect(30, 140, DEMO_WHEEL_SIZE_X, DEMO_WHEEL_SIZE_Y, color );
00355 
00356       if( touch_states[QT60168_TOUCH_SENSOR_WHEEL_0] ) color = WHITE;
00357       else                                             color = BLUE;
00358       et024006_DrawFilledRect(DEMO_WHEEL_START_X + DEMO_WHEEL_RADIUS*SIN0,
00359                               DEMO_WHEEL_START_Y - DEMO_WHEEL_RADIUS*COS0,
00360                               DEMO_WHEEL_SIZE_X, DEMO_WHEEL_SIZE_Y, color );
00361 
00362       if( touch_states[QT60168_TOUCH_SENSOR_WHEEL_1] ) color = WHITE;
00363       else                                             color = BLUE;
00364       et024006_DrawFilledRect(DEMO_WHEEL_START_X + DEMO_WHEEL_RADIUS*SIN30,
00365                               DEMO_WHEEL_START_Y - DEMO_WHEEL_RADIUS*COS30,
00366                               DEMO_WHEEL_SIZE_X, DEMO_WHEEL_SIZE_Y, color );
00367 
00368       if( touch_states[QT60168_TOUCH_SENSOR_WHEEL_2] ) color = WHITE;
00369       else                                             color = BLUE;
00370       et024006_DrawFilledRect(DEMO_WHEEL_START_X + DEMO_WHEEL_RADIUS*SIN60,
00371                               DEMO_WHEEL_START_Y - DEMO_WHEEL_RADIUS*COS60,
00372                               DEMO_WHEEL_SIZE_X, DEMO_WHEEL_SIZE_Y, color );
00373 
00374       if( touch_states[QT60168_TOUCH_SENSOR_WHEEL_3] ) color = WHITE;
00375       else                                             color = BLUE;
00376       et024006_DrawFilledRect(DEMO_WHEEL_START_X + DEMO_WHEEL_RADIUS*SIN90,
00377                               DEMO_WHEEL_START_Y - DEMO_WHEEL_RADIUS*COS90,
00378                               DEMO_WHEEL_SIZE_X, DEMO_WHEEL_SIZE_Y, color );
00379 
00380       if( touch_states[QT60168_TOUCH_SENSOR_WHEEL_4] ) color = WHITE;
00381       else                                             color = BLUE;
00382       et024006_DrawFilledRect(DEMO_WHEEL_START_X + DEMO_WHEEL_RADIUS*SIN60,
00383                               DEMO_WHEEL_START_Y + DEMO_WHEEL_RADIUS*COS60,
00384                               DEMO_WHEEL_SIZE_X, DEMO_WHEEL_SIZE_Y, color );
00385 
00386       if( touch_states[QT60168_TOUCH_SENSOR_WHEEL_5] ) color = WHITE;
00387       else                                             color = BLUE;
00388       et024006_DrawFilledRect(DEMO_WHEEL_START_X + DEMO_WHEEL_RADIUS*SIN30,
00389                               DEMO_WHEEL_START_Y + DEMO_WHEEL_RADIUS*COS30,
00390                               DEMO_WHEEL_SIZE_X, DEMO_WHEEL_SIZE_Y, color );
00391 
00392       if( touch_states[QT60168_TOUCH_SENSOR_WHEEL_6] ) color = WHITE;
00393       else                                             color = BLUE;
00394       et024006_DrawFilledRect(DEMO_WHEEL_START_X - DEMO_WHEEL_RADIUS*SIN0,
00395                               DEMO_WHEEL_START_Y + DEMO_WHEEL_RADIUS*COS0,
00396                               DEMO_WHEEL_SIZE_X, DEMO_WHEEL_SIZE_Y, color );
00397 
00398       if( touch_states[QT60168_TOUCH_SENSOR_WHEEL_7] ) color = WHITE;
00399       else                                             color = BLUE;
00400       et024006_DrawFilledRect(DEMO_WHEEL_START_X - DEMO_WHEEL_RADIUS*SIN30,
00401                               DEMO_WHEEL_START_Y + DEMO_WHEEL_RADIUS*COS30,
00402                               DEMO_WHEEL_SIZE_X, DEMO_WHEEL_SIZE_Y, color );
00403 
00404       if( touch_states[QT60168_TOUCH_SENSOR_WHEEL_8] ) color = WHITE;
00405       else                                             color = BLUE;
00406       et024006_DrawFilledRect(DEMO_WHEEL_START_X - DEMO_WHEEL_RADIUS*SIN60,
00407                               DEMO_WHEEL_START_Y + DEMO_WHEEL_RADIUS*COS60,
00408                               DEMO_WHEEL_SIZE_X, DEMO_WHEEL_SIZE_Y, color );
00409 
00410       if( touch_states[QT60168_TOUCH_SENSOR_WHEEL_9] ) color = WHITE;
00411       else                                             color = BLUE;
00412       et024006_DrawFilledRect(DEMO_WHEEL_START_X - DEMO_WHEEL_RADIUS*SIN90,
00413                               DEMO_WHEEL_START_Y + DEMO_WHEEL_RADIUS*COS90,
00414                               DEMO_WHEEL_SIZE_X, DEMO_WHEEL_SIZE_Y, color );
00415 
00416       if( touch_states[QT60168_TOUCH_SENSOR_WHEEL_10] ) color = WHITE;
00417       else                                              color = BLUE;
00418       et024006_DrawFilledRect(DEMO_WHEEL_START_X - DEMO_WHEEL_RADIUS*SIN60,
00419                               DEMO_WHEEL_START_Y - DEMO_WHEEL_RADIUS*COS60,
00420                               DEMO_WHEEL_SIZE_X, DEMO_WHEEL_SIZE_Y, color );
00421 
00422       if( touch_states[QT60168_TOUCH_SENSOR_WHEEL_11] ) color = WHITE;
00423       else                                              color = BLUE;
00424       et024006_DrawFilledRect(DEMO_WHEEL_START_X - DEMO_WHEEL_RADIUS*SIN30,
00425                               DEMO_WHEEL_START_Y - DEMO_WHEEL_RADIUS*COS30,
00426                               DEMO_WHEEL_SIZE_X, DEMO_WHEEL_SIZE_Y, color );
00427     }
00428 
00429     else if( !idle && ( demo_display==DEMO_DISPLAY_BOXES ) )
00430     { // Display a box randomly on the screen.
00431       idle = TRUE;
00432       x_start = DEMO_START_X + rand()%DEMO_SIZE_X;
00433       y_start = DEMO_START_Y + rand()%DEMO_SIZE_Y;
00434       x_size  = rand()%(DEMO_START_X+DEMO_SIZE_X-x_start);
00435       y_size  = rand()%(DEMO_START_Y+DEMO_SIZE_Y-y_start);
00436       color   = rand()%0x10000;
00437       switch( demo_color )
00438       {
00439       case DEMO_COLOR_BLUE:
00440         color = color & BLUE;
00441         break;
00442       case DEMO_COLOR_RED:
00443         color = color & RED;
00444         break;
00445       case DEMO_COLOR_GREEN:
00446         color = color & GREEN;
00447         break;
00448       default:
00449         break;
00450       }
00451 
00452       et024006_DrawFilledRect(
00453         x_start
00454       , y_start
00455       , x_size
00456       , y_size
00457       , color );
00458     }
00459   } // while(1)...
00460 }

static void qt60168_resources_init ( void   )  [static]

Initializes QT60168 resources: GPIO and SPI.

Definition at line 117 of file qt60168_example.c.

References QT60168_SPI_BITS, and QT60168_SPI_MASTER_SPEED.

Referenced by main().

00118 {
00119   static const gpio_map_t QT60168_SPI_GPIO_MAP =
00120   {
00121     {QT60168_SPI_SCK_PIN,          QT60168_SPI_SCK_FUNCTION         },  // SPI Clock.
00122     {QT60168_SPI_MISO_PIN,         QT60168_SPI_MISO_FUNCTION        },  // MISO.
00123     {QT60168_SPI_MOSI_PIN,         QT60168_SPI_MOSI_FUNCTION        },  // MOSI.
00124     {QT60168_SPI_NPCS0_PIN,        QT60168_SPI_NPCS0_FUNCTION}  // Chip Select NPCS.
00125   };
00126 
00127   // SPI options.
00128   spi_options_t spiOptions =
00129   {
00130     .reg          = QT60168_SPI_NCPS,
00131     .baudrate     = QT60168_SPI_MASTER_SPEED, // Defined in conf_qt60168.h.
00132     .bits         = QT60168_SPI_BITS,         // Defined in conf_qt60168.h.
00133     .spck_delay   = 0,
00134     .trans_delay  = 0,
00135     .stay_act     = 0,
00136     .spi_mode     = 3,
00137     .modfdis      = 1
00138   };
00139 
00140   // Assign I/Os to SPI.
00141   gpio_enable_module(QT60168_SPI_GPIO_MAP,
00142                      sizeof(QT60168_SPI_GPIO_MAP) / sizeof(QT60168_SPI_GPIO_MAP[0]));
00143 
00144   // Initialize as master.
00145   spi_initMaster(QT60168_SPI, &spiOptions);
00146 
00147   // Set selection mode: variable_ps, pcs_decode, delay.
00148   spi_selectionMode(QT60168_SPI, 0, 0, 0);
00149 
00150   // Enable SPI.
00151   spi_enable(QT60168_SPI);
00152 
00153   // Initialize QT60168 with SPI clock Osc0.
00154   spi_setupChipReg(QT60168_SPI, &spiOptions, FOSC0);
00155 }


Generated on Fri Feb 19 02:24:04 2010 for AVR32 - QT60168 Driver by  doxygen 1.5.5