web analytics
July 12, 2015

Display – Touchscreen

display_touch

The touchscreen allows for full user interaction: clicking buttons, dragging sliders and scrolling menus. Largely implemented by widgets, the display touch logic allows you to read where the user is currently pressing down on the screen. The screen calibration data is persistently stored on the device, which means you only have to be concerned with handling the touch itself.

Updating touch input

After calling display.update() the touchscreen input is updated. After updating, you can check for various touch conditions: entering an area, leaving an area, touching down and releasing. Since the display tracks both past and current touch input, you do not have to keep track of previous touch states yourself. Below is a simple example code showing the many ways touch input can be handled.

Tracking the current position

#include "Phoenard.h"

void setup() {
  // Initialize Serial
  Serial.begin(9600);
}

void loop() {
  // Update and check for touching down
  display.update();
  if (display.isTouched()) {
    PressPoint pos = display.getTouch();
    Serial.print("Touched at [");
    Serial.print(pos.x);
    Serial.print(", ");
    Serial.print(pos.y);
    Serial.print("] Pressure=");
    Serial.println(pos.pressure);
  }
}

 

Events for a rectangular area

#include "Phoenard.h"

void setup() {
  // Initialize Serial
  Serial.begin(9600);
  
  // Draw a testing rectangle
  display.fillRect(50, 50, 150, 100, WHITE);
}

void loop() {
  // Update touch input
  display.update();

  // Handle touch events inside the area
  if (display.isTouchChange(50, 50, 150, 100)) {
    Serial.println("Area touch changed.");
  }
  if (display.isTouchEnter(50, 50, 150, 100)) {
    Serial.println("Area touch entered.");
  }
  if (display.isTouchLeave(50, 50, 150, 100)) {
    Serial.println("Area touch left.");
  }
  if (display.isTouchClicked(50, 50, 150, 100)) {
    Serial.println("Area touch clicked.");
  }
  if (display.isTouched(50, 50, 150, 100)) {
    Serial.println("Area is touched.");
  }

  // Short delay to reduce spam
  delay(400);
}



Next up: Drawing shapes