controller_evk1104.c File Reference


Detailed Description

Controller.

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

Definition in file controller_evk1104.c.

#include "controller.h"
#include "board.h"
#include "gpio.h"
#include "spi.h"
#include "qt60168.h"
#include "conf_qt60168.h"
#include "cycle_counter.h"

Go to the source code of this file.

Defines

#define CLEAR_PRESSED_STATE(x)   controller_state &= ~STATE_##x##_PRESSED
#define CLEAR_RELEASED_STATE(x)   controller_state &= ~STATE_##x##_RELEASED
#define SET_PRESSED_STATE(x)   controller_state = (controller_state & ~STATE_##x##_RELEASED) | STATE_##x##_PRESSED
#define SET_RELEASED_STATE(x)   controller_state = (controller_state & ~STATE_##x##_PRESSED) | STATE_##x##_RELEASED

Enumerations

enum  { TOUCH_IDLE = 0, TOUCH_PRESS = 1, TOUCH_RELEASE = 2 }
enum  {
  STATE_IDLE = 0x0000, STATE_BACK_PRESSED = 0x0001, STATE_FCT1_PRESSED = 0x0002, STATE_FCT2_PRESSED = 0x0004,
  STATE_FCT3_PRESSED = 0x0008, STATE_BACK_RELEASED = 0x0010, STATE_FCT1_RELEASED = 0x0020, STATE_FCT2_RELEASED = 0x0040,
  STATE_FCT3_RELEASED = 0x0080, STATE_WHEEL_LEFT = 0x0100, STATE_WHEEL_RIGHT = 0x0200, STATE_BACK_PRESSING = 0x0400,
  STATE_BACK_LONG_PRESS = 0x0800
}

Functions

void controller_init (int cpu_hz, int hsb_hz, int pba_hz, int pbb_hz)
Bool controller_key_back (void)
Bool controller_key_fct1 (void)
Bool controller_key_fct1_pressed (void)
Bool controller_key_fct2 (void)
Bool controller_key_fct2_pressed (void)
Bool controller_key_fct3 (void)
Bool controller_key_fct3_pressed (void)
Bool controller_key_pressed (void)
Bool controller_key_released (void)
Bool controller_key_reset (void)
void controller_reset (void)
void controller_task (void)
Bool controller_wheel_left (int wheel_inc)
Bool controller_wheel_pressed (void)
Bool controller_wheel_right (int wheel_inc)
static void qt60168_resources_init (int cpu_hz)
 Initializes QT60168 resources: GPIO and SPI.
static void set_wheel_left ()
static void set_wheel_right ()
static void update_controller_state (void)

Variables

static int controller_cpu_hz
static enum { ... }  controller_state
static t_cpu_time long_press_timer
static enum { ... }  touch_states [QT60168_TOUCH_NUMBER_OF_SENSORS]
static int wheel_step_counter = 0


Define Documentation

#define CLEAR_PRESSED_STATE (  )     controller_state &= ~STATE_##x##_PRESSED

Definition at line 56 of file controller_evk1104.c.

#define CLEAR_RELEASED_STATE (  )     controller_state &= ~STATE_##x##_RELEASED

#define SET_PRESSED_STATE (  )     controller_state = (controller_state & ~STATE_##x##_RELEASED) | STATE_##x##_PRESSED

Definition at line 58 of file controller_evk1104.c.

Referenced by update_controller_state().

#define SET_RELEASED_STATE (  )     controller_state = (controller_state & ~STATE_##x##_PRESSED) | STATE_##x##_RELEASED

Definition at line 59 of file controller_evk1104.c.

Referenced by update_controller_state().


Enumeration Type Documentation

anonymous enum

Enumerator:
TOUCH_IDLE 
TOUCH_PRESS 
TOUCH_RELEASE 

Definition at line 61 of file controller_evk1104.c.

00062 {
00063   TOUCH_IDLE    = 0,
00064   TOUCH_PRESS   = 1,
00065   TOUCH_RELEASE = 2
00066 } touch_states[QT60168_TOUCH_NUMBER_OF_SENSORS];

anonymous enum

Enumerator:
STATE_IDLE 
STATE_BACK_PRESSED 
STATE_FCT1_PRESSED 
STATE_FCT2_PRESSED 
STATE_FCT3_PRESSED 
STATE_BACK_RELEASED 
STATE_FCT1_RELEASED 
STATE_FCT2_RELEASED 
STATE_FCT3_RELEASED 
STATE_WHEEL_LEFT 
STATE_WHEEL_RIGHT 
STATE_BACK_PRESSING 
STATE_BACK_LONG_PRESS 

Definition at line 68 of file controller_evk1104.c.

00069 {
00070   STATE_IDLE              = 0x0000,
00071   STATE_BACK_PRESSED      = 0x0001,
00072   STATE_FCT1_PRESSED      = 0x0002,
00073   STATE_FCT2_PRESSED      = 0x0004,
00074   STATE_FCT3_PRESSED      = 0x0008,
00075   STATE_BACK_RELEASED     = 0x0010,
00076   STATE_FCT1_RELEASED     = 0x0020,
00077   STATE_FCT2_RELEASED     = 0x0040,
00078   STATE_FCT3_RELEASED     = 0x0080,
00079   STATE_WHEEL_LEFT        = 0x0100,
00080   STATE_WHEEL_RIGHT       = 0x0200,
00081   STATE_BACK_PRESSING     = 0x0400,
00082   STATE_BACK_LONG_PRESS   = 0x0800
00083 } controller_state = STATE_IDLE;


Function Documentation

void controller_init ( int  cpu_hz,
int  hsb_hz,
int  pba_hz,
int  pbb_hz 
)

Definition at line 363 of file controller_evk1104.c.

References controller_cpu_hz, qt60168_resources_init(), TOUCH_IDLE, and touch_states.

00364 {
00365   int i;
00366   // Initialize QT60168 resources: GPIO, SPI and QT60168.
00367   qt60168_resources_init(cpu_hz);
00368   // Initialize QT60168 component.
00369   qt60168_init(cpu_hz);
00370   // Read the touch states
00371   for (i=0; i<QT60168_TOUCH_NUMBER_OF_SENSORS; i++ )
00372     touch_states[i] = TOUCH_IDLE;
00373   controller_cpu_hz = cpu_hz;
00374 }

Bool controller_key_back ( void   ) 

Definition at line 266 of file controller_evk1104.c.

References CLEAR_RELEASED_STATE, controller_state, FALSE, STATE_BACK_RELEASED, and TRUE.

00267 {
00268   if (controller_state & STATE_BACK_RELEASED)
00269   {
00270     CLEAR_RELEASED_STATE(BACK);
00271     return TRUE;
00272   }
00273   return FALSE;
00274 }

Bool controller_key_fct1 ( void   ) 

Definition at line 286 of file controller_evk1104.c.

References CLEAR_RELEASED_STATE, controller_state, FALSE, STATE_FCT1_RELEASED, and TRUE.

00287 {
00288   if (controller_state & STATE_FCT1_RELEASED)
00289   {
00290     CLEAR_RELEASED_STATE(FCT1);
00291     return TRUE;
00292   }
00293   return FALSE;
00294 }

Bool controller_key_fct1_pressed ( void   ) 

Definition at line 316 of file controller_evk1104.c.

References controller_state, FALSE, STATE_FCT1_PRESSED, and TRUE.

00317 {
00318   if (controller_state & STATE_FCT1_PRESSED)
00319     return TRUE;
00320   return FALSE;
00321 }

Bool controller_key_fct2 ( void   ) 

Definition at line 296 of file controller_evk1104.c.

References CLEAR_RELEASED_STATE, controller_state, FALSE, STATE_FCT2_RELEASED, and TRUE.

00297 {
00298   if (controller_state & STATE_FCT2_RELEASED)
00299   {
00300     CLEAR_RELEASED_STATE(FCT2);
00301     return TRUE;
00302   }
00303   return FALSE;
00304 }

Bool controller_key_fct2_pressed ( void   ) 

Definition at line 323 of file controller_evk1104.c.

References controller_state, FALSE, STATE_FCT2_PRESSED, and TRUE.

00324 {
00325   if (controller_state & STATE_FCT2_PRESSED)
00326     return TRUE;
00327   return FALSE;
00328 }

Bool controller_key_fct3 ( void   ) 

Definition at line 306 of file controller_evk1104.c.

References CLEAR_RELEASED_STATE, controller_state, FALSE, STATE_FCT3_RELEASED, and TRUE.

00307 {
00308   if (controller_state & STATE_FCT3_RELEASED)
00309   {
00310     CLEAR_RELEASED_STATE(FCT3);
00311     return TRUE;
00312   }
00313   return FALSE;
00314 }

Bool controller_key_fct3_pressed ( void   ) 

Definition at line 330 of file controller_evk1104.c.

References controller_state, FALSE, STATE_FCT3_PRESSED, and TRUE.

00331 {
00332   if (controller_state & STATE_FCT3_PRESSED)
00333     return TRUE;
00334   return FALSE;
00335 }

Bool controller_key_pressed ( void   ) 

Definition at line 241 of file controller_evk1104.c.

References controller_state, FALSE, STATE_IDLE, and TRUE.

00242 {
00243   if (controller_state == STATE_IDLE)
00244     return FALSE;
00245   return TRUE;
00246 }

Bool controller_key_released ( void   ) 

Bool controller_key_reset ( void   ) 

Definition at line 276 of file controller_evk1104.c.

References controller_state, FALSE, STATE_BACK_LONG_PRESS, and TRUE.

00277 {
00278   if (controller_state & STATE_BACK_LONG_PRESS)
00279   {
00280     controller_state &= ~STATE_BACK_LONG_PRESS;
00281     return TRUE;
00282   }
00283   return FALSE;
00284 }

void controller_reset ( void   ) 

Definition at line 357 of file controller_evk1104.c.

References controller_state, STATE_IDLE, and wheel_step_counter.

00358 {
00359   controller_state = STATE_IDLE;
00360   wheel_step_counter = 0;
00361 }

void controller_task ( void   ) 

Definition at line 376 of file controller_evk1104.c.

References TOUCH_IDLE, TOUCH_PRESS, TOUCH_RELEASE, touch_states, and update_controller_state().

00377 {
00378   int i;
00379 
00380   for (i=0; i<QT60168_TOUCH_NUMBER_OF_SENSORS; i++ )
00381   {
00382     if (qt60168_is_key_pressed(i))
00383       touch_states[i] = TOUCH_PRESS;
00384     else if (touch_states[i] == TOUCH_PRESS)
00385       touch_states[i] = TOUCH_RELEASE;
00386     else
00387       touch_states[i] = TOUCH_IDLE;
00388   }
00389   update_controller_state();
00390 }

Bool controller_wheel_left ( int  wheel_inc  ) 

Definition at line 347 of file controller_evk1104.c.

References controller_state, FALSE, STATE_WHEEL_LEFT, TRUE, and wheel_step_counter.

00348 {
00349   if (wheel_step_counter >= wheel_inc && controller_state & STATE_WHEEL_LEFT)
00350   {
00351     wheel_step_counter -= wheel_inc;
00352     return TRUE;
00353   }
00354   return FALSE;
00355 }

Bool controller_wheel_pressed ( void   ) 

Definition at line 258 of file controller_evk1104.c.

References controller_state, FALSE, STATE_WHEEL_LEFT, STATE_WHEEL_RIGHT, and TRUE.

00259 {
00260   if (controller_state & STATE_WHEEL_LEFT ||
00261       controller_state & STATE_WHEEL_RIGHT)
00262     return TRUE;
00263   return FALSE;
00264 }

Bool controller_wheel_right ( int  wheel_inc  ) 

Definition at line 337 of file controller_evk1104.c.

References controller_state, FALSE, STATE_WHEEL_RIGHT, TRUE, and wheel_step_counter.

00338 {
00339   if (wheel_step_counter >= wheel_inc && controller_state & STATE_WHEEL_RIGHT)
00340   {
00341     wheel_step_counter -= wheel_inc;
00342     return TRUE;
00343   }
00344   return FALSE;
00345 }

static void qt60168_resources_init ( int  cpu_hz  )  [static]

Initializes QT60168 resources: GPIO and SPI.

Definition at line 201 of file controller_evk1104.c.

References QT60168_SPI_BITS, and QT60168_SPI_MASTER_SPEED.

Referenced by controller_init().

00202 {
00203   static const gpio_map_t QT60168_SPI_GPIO_MAP =
00204   {
00205     {QT60168_SPI_SCK_PIN,          QT60168_SPI_SCK_FUNCTION         },  // SPI Clock.
00206     {QT60168_SPI_MISO_PIN,         QT60168_SPI_MISO_FUNCTION        },  // MISO.
00207     {QT60168_SPI_MOSI_PIN,         QT60168_SPI_MOSI_FUNCTION        },  // MOSI.
00208     {QT60168_SPI_NPCS0_PIN,        QT60168_SPI_NPCS0_FUNCTION}  // Chip Select NPCS.
00209   };
00210 
00211   // SPI options.
00212   spi_options_t spiOptions =
00213   {
00214     .reg          = QT60168_SPI_NCPS,
00215     .baudrate     = QT60168_SPI_MASTER_SPEED, // Defined in conf_qt60168.h.
00216     .bits         = QT60168_SPI_BITS,         // Defined in conf_qt60168.h.
00217     .spck_delay   = 0,
00218     .trans_delay  = 0,
00219     .stay_act     = 0,
00220     .spi_mode     = 3,
00221     .modfdis      = 1
00222   };
00223 
00224   // Assign I/Os to SPI.
00225   gpio_enable_module(QT60168_SPI_GPIO_MAP,
00226                      sizeof(QT60168_SPI_GPIO_MAP) / sizeof(QT60168_SPI_GPIO_MAP[0]));
00227 
00228   // Initialize as master.
00229   spi_initMaster(QT60168_SPI, &spiOptions);
00230 
00231   // Set selection mode: variable_ps, pcs_decode, delay.
00232   spi_selectionMode(QT60168_SPI, 0, 0, 0);
00233 
00234   // Enable SPI.
00235   spi_enable(QT60168_SPI);
00236 
00237   // Initialize QT60168 with SPI clock Osc0.
00238   spi_setupChipReg(QT60168_SPI, &spiOptions, cpu_hz);
00239 }

static void set_wheel_left (  )  [inline, static]

Definition at line 99 of file controller_evk1104.c.

References controller_state, STATE_WHEEL_LEFT, STATE_WHEEL_RIGHT, and wheel_step_counter.

Referenced by update_controller_state().

00100 {
00101   if (controller_state & STATE_WHEEL_RIGHT)
00102   {
00103     controller_state &= ~STATE_WHEEL_RIGHT;
00104     wheel_step_counter = 0;
00105   }
00106   controller_state |= STATE_WHEEL_LEFT;
00107   wheel_step_counter++;
00108 }

static void set_wheel_right (  )  [inline, static]

Definition at line 89 of file controller_evk1104.c.

References controller_state, STATE_WHEEL_LEFT, STATE_WHEEL_RIGHT, and wheel_step_counter.

Referenced by update_controller_state().

00090 {
00091   if (controller_state & STATE_WHEEL_LEFT)
00092   {
00093     controller_state &= ~STATE_WHEEL_LEFT;
00094     wheel_step_counter = 0;
00095   }
00096   controller_state |= STATE_WHEEL_RIGHT;
00097   wheel_step_counter++;
00098 }

static void update_controller_state ( void   )  [static]

Definition at line 110 of file controller_evk1104.c.

References controller_cpu_hz, CONTROLLER_LONG_PRESS_TIME_MS, controller_state, long_press_timer, SET_PRESSED_STATE, SET_RELEASED_STATE, set_wheel_left(), set_wheel_right(), STATE_BACK_LONG_PRESS, STATE_BACK_PRESSING, TOUCH_PRESS, TOUCH_RELEASE, and touch_states.

Referenced by controller_task().

00111 {
00112   // Long pressing for BACK key handler
00113   if (touch_states[QT60168_TOUCH_SENSOR_BUTTON_0] == TOUCH_RELEASE)
00114     controller_state &= ~STATE_BACK_PRESSING;
00115   if (touch_states[QT60168_TOUCH_SENSOR_BUTTON_0] == TOUCH_PRESS)
00116   {
00117     if (!(controller_state & STATE_BACK_PRESSING))
00118     {
00119       controller_state |= STATE_BACK_PRESSING;
00120       cpu_set_timeout(cpu_ms_2_cy(CONTROLLER_LONG_PRESS_TIME_MS, controller_cpu_hz), &long_press_timer);
00121     }
00122     if (cpu_is_timeout(&long_press_timer))
00123       controller_state |= STATE_BACK_LONG_PRESS;
00124   }
00125 
00126   // Back key
00127   if (touch_states[QT60168_TOUCH_SENSOR_BUTTON_0] == TOUCH_PRESS)
00128     SET_PRESSED_STATE(BACK);
00129   else if (touch_states[QT60168_TOUCH_SENSOR_BUTTON_0] == TOUCH_RELEASE)
00130     SET_RELEASED_STATE(BACK);
00131   // Function 1 key
00132   if (touch_states[QT60168_TOUCH_SENSOR_BUTTON_1] == TOUCH_PRESS)
00133     SET_PRESSED_STATE(FCT1);
00134   else if (touch_states[QT60168_TOUCH_SENSOR_BUTTON_1] == TOUCH_RELEASE)
00135     SET_RELEASED_STATE(FCT1);
00136   // Function 2 key
00137   if (touch_states[QT60168_TOUCH_SENSOR_BUTTON_2] == TOUCH_PRESS)
00138     SET_PRESSED_STATE(FCT2);
00139   else if (touch_states[QT60168_TOUCH_SENSOR_BUTTON_2] == TOUCH_RELEASE)
00140     SET_RELEASED_STATE(FCT2);
00141   // Function 3 key
00142   if (touch_states[QT60168_TOUCH_SENSOR_BUTTON_3] == TOUCH_PRESS)
00143     SET_PRESSED_STATE(FCT3);
00144   else if (touch_states[QT60168_TOUCH_SENSOR_BUTTON_3] == TOUCH_RELEASE)
00145     SET_RELEASED_STATE(FCT3);
00146 
00147   // Wheel right
00148   if (touch_states[QT60168_TOUCH_SENSOR_WHEEL_0] == TOUCH_RELEASE && touch_states[QT60168_TOUCH_SENSOR_WHEEL_1] == TOUCH_PRESS)
00149     set_wheel_right();
00150   if (touch_states[QT60168_TOUCH_SENSOR_WHEEL_1] == TOUCH_RELEASE && touch_states[QT60168_TOUCH_SENSOR_WHEEL_2] == TOUCH_PRESS)
00151     set_wheel_right();
00152   if (touch_states[QT60168_TOUCH_SENSOR_WHEEL_2] == TOUCH_RELEASE && touch_states[QT60168_TOUCH_SENSOR_WHEEL_3] == TOUCH_PRESS)
00153     set_wheel_right();
00154   if (touch_states[QT60168_TOUCH_SENSOR_WHEEL_3] == TOUCH_RELEASE && touch_states[QT60168_TOUCH_SENSOR_WHEEL_4] == TOUCH_PRESS)
00155     set_wheel_right();
00156   if (touch_states[QT60168_TOUCH_SENSOR_WHEEL_4] == TOUCH_RELEASE && touch_states[QT60168_TOUCH_SENSOR_WHEEL_5] == TOUCH_PRESS)
00157     set_wheel_right();
00158   if (touch_states[QT60168_TOUCH_SENSOR_WHEEL_5] == TOUCH_RELEASE && touch_states[QT60168_TOUCH_SENSOR_WHEEL_6] == TOUCH_PRESS)
00159     set_wheel_right();
00160   if (touch_states[QT60168_TOUCH_SENSOR_WHEEL_6] == TOUCH_RELEASE && touch_states[QT60168_TOUCH_SENSOR_WHEEL_7] == TOUCH_PRESS)
00161     set_wheel_right();
00162   if (touch_states[QT60168_TOUCH_SENSOR_WHEEL_7] == TOUCH_RELEASE && touch_states[QT60168_TOUCH_SENSOR_WHEEL_8] == TOUCH_PRESS)
00163     set_wheel_right();
00164   if (touch_states[QT60168_TOUCH_SENSOR_WHEEL_8] == TOUCH_RELEASE && touch_states[QT60168_TOUCH_SENSOR_WHEEL_9] == TOUCH_PRESS)
00165     set_wheel_right();
00166   if (touch_states[QT60168_TOUCH_SENSOR_WHEEL_9] == TOUCH_RELEASE && touch_states[QT60168_TOUCH_SENSOR_WHEEL_10] == TOUCH_PRESS)
00167     set_wheel_right();
00168   if (touch_states[QT60168_TOUCH_SENSOR_WHEEL_10] == TOUCH_RELEASE && touch_states[QT60168_TOUCH_SENSOR_WHEEL_11] == TOUCH_PRESS)
00169     set_wheel_right();
00170   if (touch_states[QT60168_TOUCH_SENSOR_WHEEL_11] == TOUCH_RELEASE && touch_states[QT60168_TOUCH_SENSOR_WHEEL_0] == TOUCH_PRESS)
00171     set_wheel_right();
00172   // Wheel left
00173   if (touch_states[QT60168_TOUCH_SENSOR_WHEEL_11] == TOUCH_PRESS && touch_states[QT60168_TOUCH_SENSOR_WHEEL_0] == TOUCH_RELEASE)
00174     set_wheel_left();
00175   if (touch_states[QT60168_TOUCH_SENSOR_WHEEL_10] == TOUCH_PRESS && touch_states[QT60168_TOUCH_SENSOR_WHEEL_11] == TOUCH_RELEASE)
00176     set_wheel_left();
00177   if (touch_states[QT60168_TOUCH_SENSOR_WHEEL_9] == TOUCH_PRESS && touch_states[QT60168_TOUCH_SENSOR_WHEEL_10] == TOUCH_RELEASE)
00178     set_wheel_left();
00179   if (touch_states[QT60168_TOUCH_SENSOR_WHEEL_8] == TOUCH_PRESS && touch_states[QT60168_TOUCH_SENSOR_WHEEL_9] == TOUCH_RELEASE)
00180     set_wheel_left();
00181   if (touch_states[QT60168_TOUCH_SENSOR_WHEEL_7] == TOUCH_PRESS && touch_states[QT60168_TOUCH_SENSOR_WHEEL_8] == TOUCH_RELEASE)
00182     set_wheel_left();
00183   if (touch_states[QT60168_TOUCH_SENSOR_WHEEL_6] == TOUCH_PRESS && touch_states[QT60168_TOUCH_SENSOR_WHEEL_7] == TOUCH_RELEASE)
00184     set_wheel_left();
00185   if (touch_states[QT60168_TOUCH_SENSOR_WHEEL_5] == TOUCH_PRESS && touch_states[QT60168_TOUCH_SENSOR_WHEEL_6] == TOUCH_RELEASE)
00186     set_wheel_left();
00187   if (touch_states[QT60168_TOUCH_SENSOR_WHEEL_4] == TOUCH_PRESS && touch_states[QT60168_TOUCH_SENSOR_WHEEL_5] == TOUCH_RELEASE)
00188     set_wheel_left();
00189   if (touch_states[QT60168_TOUCH_SENSOR_WHEEL_3] == TOUCH_PRESS && touch_states[QT60168_TOUCH_SENSOR_WHEEL_4] == TOUCH_RELEASE)
00190     set_wheel_left();
00191   if (touch_states[QT60168_TOUCH_SENSOR_WHEEL_2] == TOUCH_PRESS && touch_states[QT60168_TOUCH_SENSOR_WHEEL_3] == TOUCH_RELEASE)
00192     set_wheel_left();
00193   if (touch_states[QT60168_TOUCH_SENSOR_WHEEL_1] == TOUCH_PRESS && touch_states[QT60168_TOUCH_SENSOR_WHEEL_2] == TOUCH_RELEASE)
00194     set_wheel_left();
00195   if (touch_states[QT60168_TOUCH_SENSOR_WHEEL_0] == TOUCH_PRESS && touch_states[QT60168_TOUCH_SENSOR_WHEEL_1] == TOUCH_RELEASE)
00196     set_wheel_left();
00197 }


Variable Documentation

int controller_cpu_hz [static]

Definition at line 87 of file controller_evk1104.c.

Referenced by controller_init(), and update_controller_state().

enum { ... } controller_state [static]

t_cpu_time long_press_timer [static]

Definition at line 86 of file controller_evk1104.c.

Referenced by update_controller_state().

enum { ... } touch_states[QT60168_TOUCH_NUMBER_OF_SENSORS] [static]

int wheel_step_counter = 0 [static]


Generated on Fri Feb 19 02:23:18 2010 for AVR32 UC3 - EVK1104 DSPLib Demo Documentation by  doxygen 1.5.5