int tsSetThreshhold(unsigned short value) { // Update threshhold value _tsThreshhold = value; // Persist to EEPROM eepromWriteU16(CFG_EEPROM_TOUCHSCREEN_THRESHHOLD, value); return 0; }
void tsCalibrate(void) { lcdOrientation_t orientation; uint16_t right2, top2, left2, bottom2; bool passed = false; // Determine screen orientation before starting calibration orientation = lcdGetOrientation(); /* -------------- Welcome Screen --------------- */ tsRenderCalibrationScreen(lcdGetWidth() / 2, lcdGetHeight() / 2, 5); // Delay to avoid multiple touch events systickDelay(250); /* -------------- CALIBRATE TOP-LEFT --------------- */ passed = false; while (!passed) { // Read X/Y depending on screen orientation tsRenderCalibrationScreen(3, 3, 5); _calibration.offsetLeft = orientation == LCD_ORIENTATION_LANDSCAPE ? tsReadY() : tsReadX(); _calibration.offsetTop = orientation == LCD_ORIENTATION_LANDSCAPE ? tsReadX() : tsReadY(); // Make sure values are in range if (_calibration.offsetLeft < TS_CALIBRATION_OUTOFRANGE && _calibration.offsetTop < TS_CALIBRATION_OUTOFRANGE) passed = true; } // Delay to avoid multiple touch events systickDelay(250); /* -------------- CALIBRATE BOTTOM-RIGHT --------------- */ passed = false; while (!passed) { tsRenderCalibrationScreen(lcdGetWidth() - 4, lcdGetHeight() - 4, 5); // Read X/Y depending on screen orientation _calibration.offsetRight = orientation == LCD_ORIENTATION_LANDSCAPE ? TS_ADC_LIMIT - tsReadY() : TS_ADC_LIMIT - tsReadX(); _calibration.offsetBottom = orientation == LCD_ORIENTATION_LANDSCAPE ? TS_ADC_LIMIT - tsReadX() : TS_ADC_LIMIT - tsReadY(); // Redo test if value is out of range if (_calibration.offsetRight < TS_CALIBRATION_OUTOFRANGE && _calibration.offsetBottom < TS_CALIBRATION_OUTOFRANGE) passed = true; } // Delay to avoid multiple touch events systickDelay(250); /* -------------- CALIBRATE TOP-RIGHT --------------- */ passed = false; while (!passed) { tsRenderCalibrationScreen(lcdGetWidth() - 4, 3, 5); if (orientation == LCD_ORIENTATION_LANDSCAPE) { right2 = TS_ADC_LIMIT - tsReadY(); top2 = tsReadX(); } else { right2 = tsReadX(); top2 = TS_ADC_LIMIT - tsReadY(); } // Redo test if value is out of range if (right2 < TS_CALIBRATION_OUTOFRANGE && top2 < TS_CALIBRATION_OUTOFRANGE) passed = true; } // Average readings _calibration.offsetRight = (_calibration.offsetRight + right2) / 2; _calibration.offsetTop = (_calibration.offsetTop + top2) / 2; // Delay to avoid multiple touch events systickDelay(250); /* -------------- CALIBRATE BOTTOM-LEFT --------------- */ passed = false; while (!passed) { tsRenderCalibrationScreen(3, lcdGetHeight() - 4, 5); if (orientation == LCD_ORIENTATION_LANDSCAPE) { left2 = tsReadY(); bottom2 = TS_ADC_LIMIT - tsReadX(); } else { left2 = TS_ADC_LIMIT - tsReadX(); bottom2 = tsReadY(); } // Redo test if value is out of range if (left2 < TS_CALIBRATION_OUTOFRANGE && bottom2 < TS_CALIBRATION_OUTOFRANGE) passed = true; } // Average readings _calibration.offsetLeft = (_calibration.offsetLeft + left2) / 2; _calibration.offsetBottom = (_calibration.offsetBottom + bottom2) / 2; // Delay to avoid multiple touch events systickDelay(250); _calibration.divisorX = ((TS_ADC_LIMIT - (_calibration.offsetLeft + _calibration.offsetRight)) * 100) / lcdGetWidth(); _calibration.divisorY = ((TS_ADC_LIMIT - (_calibration.offsetTop + _calibration.offsetBottom)) * 100) / lcdGetHeight(); /* -------------- Persist Data --------------- */ // Persist data to EEPROM eepromWriteU16(CFG_EEPROM_TOUCHSCREEN_OFFSET_LEFT, _calibration.offsetLeft); eepromWriteU16(CFG_EEPROM_TOUCHSCREEN_OFFSET_RIGHT, _calibration.offsetRight); eepromWriteU16(CFG_EEPROM_TOUCHSCREEN_OFFSET_TOP, _calibration.offsetTop); eepromWriteU16(CFG_EEPROM_TOUCHSCREEN_OFFSET_BOT, _calibration.offsetBottom); eepromWriteU16(CFG_EEPROM_TOUCHSCREEN_OFFSET_DIVX, _calibration.divisorX); eepromWriteU16(CFG_EEPROM_TOUCHSCREEN_OFFSET_DIVY, _calibration.divisorY); eepromWriteU8(CFG_EEPROM_TOUCHSCREEN_CALIBRATED, 1); // Clear the screen drawFill(COLOR_BLACK); }