rtouch.h File Reference

#include <stdint.h>
#include <stdbool.h>

Go to the source code of this file.

Data Structures

struct  rtouch_calibration_matrix_struct
 Calibration matrix coefficients, computed from three calibration points. More...
struct  rtouch_calibration_point_struct
 Info for one calibration point, raw samples and actual panel position. More...
struct  rtouch_calibration_points_struct
 Collection of the three calibration points required to compute calibration. More...
struct  rtouch_event_struct
 User-friendly touch event structure. More...

Typedefs

typedef struct
rtouch_calibration_matrix_struct 
rtouch_calibration_matrix_t
typedef struct
rtouch_calibration_point_struct 
rtouch_calibration_point_t
typedef struct
rtouch_calibration_points_struct 
rtouch_calibration_points_t
typedef void(* rtouch_event_handler_t )(rtouch_event_t const *event)
typedef struct rtouch_event_struct rtouch_event_t
typedef enum rtouch_event_type_enum rtouch_event_type_t

Enumerations

enum  rtouch_event_type_enum { RTOUCH_NO_EVENT, RTOUCH_PRESS, RTOUCH_MOVE, RTOUCH_RELEASE }
 Event type used by event struct TOUCH_event_t. More...

Functions

void rtouch_compute_calibration_matrix (rtouch_calibration_points_t const *points, rtouch_calibration_matrix_t *matrix)
 Compute a calibration matrix from three calibration points.
void rtouch_disable (void)
 Disable driver.
void rtouch_enable (void)
 Enable driver.
void rtouch_get_calibration_matrix (rtouch_calibration_matrix_t *destination)
 Get the current calibration matrix.
void rtouch_get_event (rtouch_event_t *event)
 Get last event.
rtouch_event_handler_t rtouch_get_event_handler (void)
 Get current event handler.
void rtouch_init (void)
 Initialize touch panel driver.
bool rtouch_is_touched (void)
 True if touched.
void rtouch_set_calibration_matrix (rtouch_calibration_matrix_t const *source)
 Assign a calibration matrix to the driver.
void rtouch_set_event_handler (rtouch_event_handler_t handler)
 Set new event handler.


Typedef Documentation

typedef void(* rtouch_event_handler_t)(rtouch_event_t const *event)

Definition at line 66 of file rtouch.h.


Enumeration Type Documentation

Event type used by event struct TOUCH_event_t.

Enumerator:
RTOUCH_NO_EVENT 
RTOUCH_PRESS 
RTOUCH_MOVE 
RTOUCH_RELEASE 

Definition at line 46 of file rtouch.h.

00047 {
00048     RTOUCH_NO_EVENT,
00049     RTOUCH_PRESS,
00050     RTOUCH_MOVE,
00051     RTOUCH_RELEASE
00052 } rtouch_event_type_t;


Function Documentation

void rtouch_compute_calibration_matrix ( rtouch_calibration_points_t const *  points,
rtouch_calibration_matrix_t matrix 
)

Compute a calibration matrix from three calibration points.

This function computes a calibration matrix from a set of three calibration points povided by the caller. Use the raw sample values from the event struct when filling data into the calibration point struct. The calibration matrix will be copied into the struct also provided by the user. Use the TOUCH_SetCalibrationMatrix() function to assign a calibration matrix to the driver.

Parameters:
points Pointer to a calibration point set.
matrix Pointer to the struct where the matrix will be copied.

Definition at line 589 of file rtouch.c.

References rtouch_calibration_matrix_struct::A, rtouch_calibration_matrix_struct::B, rtouch_calibration_matrix_struct::C, rtouch_calibration_matrix_struct::D, rtouch_calibration_matrix_struct::E, rtouch_calibration_matrix_struct::F, rtouch_calibration_matrix_struct::K, rtouch_calibration_point_struct::panelX, rtouch_calibration_point_struct::panelY, rtouch_calibration_points_struct::point1, rtouch_calibration_points_struct::point2, rtouch_calibration_points_struct::point3, rtouch_calibration_point_struct::rawX, and rtouch_calibration_point_struct::rawY.

Referenced by rtouch_calibrate().

00592 {
00593     // Reference: http://www.embedded.com/story/OEG20020529S0046
00594 
00595     // Local copies of touch readings.
00596     int32_t Xr0 = points->point1.rawX;
00597     int32_t Yr0 = points->point1.rawY;
00598     int32_t Xr1 = points->point2.rawX;
00599     int32_t Yr1 = points->point2.rawY;
00600     int32_t Xr2 = points->point3.rawX;
00601     int32_t Yr2 = points->point3.rawY;
00602 
00603     // Local copies of display coordinates.
00604     int32_t Xp0 = points->point1.panelX;
00605     int32_t Yp0 = points->point1.panelY;
00606     int32_t Xp1 = points->point2.panelX;
00607     int32_t Yp1 = points->point2.panelY;
00608     int32_t Xp2 = points->point3.panelX;
00609     int32_t Yp2 = points->point3.panelY;
00610 
00611     // Compute coefficients for X calibration.
00612     matrix->A = ((Xp0 - Xp2) * (Yr1 - Yr2)) - ((Xp1 - Xp2) * (Yr0 - Yr2));
00613     matrix->B = ((Xr0 - Xr2) * (Xp1 - Xp2)) - ((Xp0 - Xp2) * (Xr1 - Xr2));
00614     matrix->C =
00615         Yr0 * ((Xr2 * Xp1) - (Xr1 * Xp2)) +
00616         Yr1 * ((Xr0 * Xp2) - (Xr2 * Xp0)) +
00617         Yr2 * ((Xr1 * Xp0) - (Xr0 * Xp1));
00618 
00619     // Compute coefficients for X calibration.
00620     matrix->D = ((Yp0 - Yp2) * (Yr1 - Yr2)) - ((Yp1 - Yp2) * (Yr0 - Yr2));
00621     matrix->E = ((Xr0 - Xr2) * (Yp1 - Yp2)) - ((Yp0 - Yp2) * (Xr1 - Xr2));
00622     matrix->F =
00623         Yr0 * ((Xr2 * Yp1) - (Xr1 * Yp2)) +
00624         Yr1 * ((Xr0 * Yp2) - (Xr2 * Yp0)) +
00625         Yr2 * ((Xr1 * Yp0) - (Xr0 * Yp1));
00626 
00627     // Compute common denominator.
00628     matrix->K = ((Xr0 - Xr2) * (Yr1 - Yr2)) - ((Xr1 - Xr2) * (Yr0 - Yr2));
00629 }

void rtouch_disable ( void   ) 

Disable driver.

This function disables the driver, stopping all touch detection and sampling. This will save power.

Definition at line 495 of file rtouch.c.

References rtouch, rtouch_disable_adc_int(), rtouch_disable_detect_int(), RTOUCH_DISABLED, rtouch_ground_x_surface(), rtouch_ground_y_surface(), and rtouch_struct::state.

Referenced by rtouch_init().

00496 {
00497     // Do not try to disable if already disabled. That will mess up the
00498     // sleep manager lock state.
00499     if (rtouch.state == RTOUCH_DISABLED) {
00500         return;
00501     }
00502 
00503     // Need this to be atomic, in case we are interrupting an ongoing sampling.
00504     //ENTER_CRITICAL_SECTION( DISABLE );
00505     // Shutdown driver.
00506     rtouch_disable_detect_int();
00507     rtouch_disable_adc_int();
00508     //LEAVE_CRITICAL_SECTION( DISABLE );
00509 
00510     // Continue shutdown.
00511     rtouch.state = RTOUCH_DISABLED;
00512 
00513     //SLEEPMGR_Unlock( TOUCH_DETECT_SLEEP_MODE );
00514 
00515     // Ground touch panel to save power.
00516     rtouch_ground_x_surface();
00517     rtouch_ground_y_surface();
00518 }

void rtouch_enable ( void   ) 

Enable driver.

This function enables the driver, resuming touch detecting and sampling.

Definition at line 524 of file rtouch.c.

References rtouch_struct::last_event, rtouch, rtouch_clear_adc_flag(), rtouch_clear_detect_flag(), RTOUCH_DISABLED, rtouch_enable_adc_int(), rtouch_enable_detect_int(), rtouch_ground_x_surface(), RTOUCH_NO_EVENT, RTOUCH_NOT_TOUCHED, rtouch_pullup_y_surface(), rtouch_struct::state, and rtouch_event_struct::type.

Referenced by main().

00525 {
00526     // Do not try to enable if already enabled. That will mess up the
00527     // sleep manager lock state.
00528     if (rtouch.state != RTOUCH_DISABLED) {
00529         return;
00530     }
00531 
00532     // Setup panel for touch detection.
00533     rtouch_ground_x_surface();
00534     rtouch_pullup_y_surface();
00535 
00536     // Initial state is untouched, but if touched, the change detect ISR
00537     // will fire immediately.
00538     rtouch.state = RTOUCH_NOT_TOUCHED;
00539     rtouch.last_event.type = RTOUCH_NO_EVENT;
00540 
00541     // Start driver.
00542     //SLEEPMGR_Lock( TOUCH_DETECT_SLEEP_MODE );
00543     rtouch_clear_adc_flag();
00544     rtouch_enable_adc_int();
00545 
00546     rtouch_clear_detect_flag();
00547     rtouch_enable_detect_int();
00548 }

void rtouch_get_calibration_matrix ( rtouch_calibration_matrix_t destination  ) 

Get the current calibration matrix.

This function retrieves the current calibration matrix from the driver. Use this function to store a calibration matrix to e.g. EEPROM.

Parameters:
destination Pointer to struct where matrix will be copied.

Definition at line 649 of file rtouch.c.

References rtouch_struct::calibration_matrix, and rtouch.

00650 {
00651     destination = rtouch.calibration_matrix;
00652 }

void rtouch_get_event ( rtouch_event_t event  ) 

Get last event.

This function will copy the last event information to a struct. The caller is reponsible for providing memory.

Parameters:
event Pointer to the struct where event will be copied.

Definition at line 557 of file rtouch.c.

References rtouch_struct::last_event, and rtouch.

Referenced by rtouch_calibrate().

00558 {
00559     *event = rtouch.last_event;
00560 }

rtouch_event_handler_t rtouch_get_event_handler ( void   ) 

Get current event handler.

This function returns the old event handler. Use this to store the old handler while replacing it for a short while, e.g. for calibration.

Returns:
Current event handler pointer.

Definition at line 678 of file rtouch.c.

References rtouch_struct::event_handler, and rtouch.

Referenced by rtouch_calibrate().

00679 {
00680     return rtouch.event_handler;
00681 }

void rtouch_init ( void   ) 

Initialize touch panel driver.

This function initializes all peripherals required to detect a touch and sample its position on a resistive touch panel. Call this function before using any other features of this driver.

The driver is disabled initially, so you must call TOUCH_Enable() before any touch events will be detected.

Definition at line 453 of file rtouch.c.

References rtouch_struct::event_handler, rtouch_struct::last_event, rtouch, rtouch_disable(), RTOUCH_NO_EVENT, RTOUCH_NOT_TOUCHED, rtouch_prepare_adc(), rtouch_prepare_detect(), rtouch_struct::state, TOUCH_SAMPLE_IRQ, and rtouch_event_struct::type.

Referenced by main().

00454 {
00455     // Start with no event handler.
00456     rtouch.event_handler = NULL;
00457 
00458 #ifdef AVR32_ADCIFA_100_H_INCLUDED
00459   // GPIO pin/adc-function map.
00460   static const gpio_map_t ADCIFA_GPIO_MAP =
00461   {
00462     {AVR32_ADCREF0_PIN,AVR32_ADCREF0_FUNCTION},
00463     {AVR32_ADCREFP_PIN,AVR32_ADCREFP_FUNCTION},
00464     {AVR32_ADCREFN_PIN,AVR32_ADCREFN_FUNCTION}
00465   };
00466   gpio_enable_module(ADCIFA_GPIO_MAP, sizeof(ADCIFA_GPIO_MAP) / sizeof(ADCIFA_GPIO_MAP[0]));
00467 #endif
00468 
00469     // Prepare required peripherals.
00470     rtouch_prepare_adc();
00471     rtouch_prepare_detect();
00472 
00473 #if (TOUCH_USE_SOFTIRQ == 1)
00474     // Use a soft IRQ for bottom half of driver.
00475     SOFTIRQ_SetHandler( TOUCH_SAMPLE_IRQ, TOUCH_ProcessSamples );
00476     SOFTIRQ_Enable( TOUCH_SAMPLE_IRQ );
00477 #endif
00478 
00479     // TOUCH_Disable() will take care of the rest of the initialization.
00480     // We need to lock the sleep mode, and set a non-disable state for the
00481     // driver, since TOUCH_Disable() will unlock the sleep mode, and also
00482     // check that the driver is not disabled already.
00483 
00484     //SLEEPMGR_Lock( TOUCH_DETECT_SLEEP_MODE );
00485     rtouch.state = RTOUCH_NOT_TOUCHED;
00486     rtouch.last_event.type = RTOUCH_NO_EVENT;
00487     rtouch_disable();
00488 }

bool rtouch_is_touched ( void   ) 

True if touched.

This function returns true if the touch panel is currently touched.

Returns:
True if panel touched.

Definition at line 568 of file rtouch.c.

References rtouch_struct::last_event, rtouch, RTOUCH_MOVE, RTOUCH_PRESS, and rtouch_event_struct::type.

Referenced by rtouch_calibrate().

00569 {
00570     bool is_touched = (rtouch.last_event.type == RTOUCH_PRESS)
00571         || (rtouch.last_event.type == RTOUCH_MOVE);
00572 
00573     return is_touched;
00574 }

void rtouch_set_calibration_matrix ( rtouch_calibration_matrix_t const *  source  ) 

Assign a calibration matrix to the driver.

This function assigns a calibration matrix to the driver.

Parameters:
source Pointer to matrix data.

Definition at line 637 of file rtouch.c.

References rtouch_struct::calibration_matrix, and rtouch.

Referenced by rtouch_calibrate().

00638 {
00639     rtouch.calibration_matrix = (rtouch_calibration_matrix_t *)source;
00640 }

void rtouch_set_event_handler ( rtouch_event_handler_t  handler  ) 

Set new event handler.

This function sets a new touch event handler. The handler is called whenever the position or state of the touch screen changes. The handler is called from the TOUCH_ProcessSamples and TOUCH_Resample functions.

Parameters:
handler Pointer to new event handler, or NULL to disable handler.

Definition at line 662 of file rtouch.c.

References rtouch_struct::event_handler, and rtouch.

Referenced by main(), and rtouch_calibrate().

00663 {
00664     // This needs to be an atomic operation, in case an interrupt
00665     // fires while we change the handler.
00666     //ENTER_CRITICAL_SECTION( SET_HANDLER );
00667     rtouch.event_handler = handler;
00668     //LEAVE_CRITICAL_SECTION( SET_HANDLER );
00669 }


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