|
| 1 | +#include "powerSave.h" |
| 2 | +#include <Wire.h> |
| 3 | +#include <interface.h> |
| 4 | + |
| 5 | +#ifndef TFT_BRIGHT_CHANNEL |
| 6 | +#define TFT_BRIGHT_CHANNEL 0 |
| 7 | +#define TFT_BRIGHT_FREQ 5000 |
| 8 | +#define TFT_BRIGHT_Bits 8 |
| 9 | +#define TFT_BL 25 |
| 10 | +#endif |
| 11 | + |
| 12 | +// NM-CYD-C5 uses XPT2046 resistive touch on the shared SPI bus |
| 13 | +#include "CYD28_TouchscreenR.h" |
| 14 | +#ifndef CYD28_DISPLAY_HOR_RES_MAX |
| 15 | +#define CYD28_DISPLAY_HOR_RES_MAX 320 |
| 16 | +#endif |
| 17 | +#ifndef CYD28_DISPLAY_VER_RES_MAX |
| 18 | +#define CYD28_DISPLAY_VER_RES_MAX 240 |
| 19 | +#endif |
| 20 | +CYD28_TouchR touch(CYD28_DISPLAY_HOR_RES_MAX, CYD28_DISPLAY_VER_RES_MAX); |
| 21 | + |
| 22 | +/*************************************************************************************** |
| 23 | +** Function name: _setup_gpio() |
| 24 | +** Location: main.cpp |
| 25 | +** Description: initial setup for the device |
| 26 | +***************************************************************************************/ |
| 27 | +void _setup_gpio() { |
| 28 | + pinMode(CYD28_TouchR_CS, OUTPUT); // Touch CS pin (XPT2046) |
| 29 | +} |
| 30 | + |
| 31 | +/*************************************************************************************** |
| 32 | +** Function name: _post_setup_gpio() |
| 33 | +** Location: main.cpp |
| 34 | +** Description: second stage gpio setup to make a few functions work |
| 35 | +***************************************************************************************/ |
| 36 | +void _post_setup_gpio() { |
| 37 | + // Brightness control must be initialized after tft in this case @Pirata |
| 38 | + pinMode(TFT_BL, OUTPUT); |
| 39 | + ledcAttach(TFT_BL, TFT_BRIGHT_FREQ, TFT_BRIGHT_Bits); |
| 40 | + ledcWrite(TFT_BL, bright); |
| 41 | + |
| 42 | + // Display and touch share the same SPI bus; pass &SPI so the driver reuses it |
| 43 | + if (!touch.begin(&SPI)) { |
| 44 | + Serial.println("Touch IC not Started"); |
| 45 | + log_i("Touch IC not Started"); |
| 46 | + } else Serial.println("Touch IC Started"); |
| 47 | +} |
| 48 | + |
| 49 | +/********************************************************************* |
| 50 | +** Function: setBrightness |
| 51 | +** location: settings.cpp |
| 52 | +** set brightness value |
| 53 | +**********************************************************************/ |
| 54 | +void _setBrightness(uint8_t brightval) { |
| 55 | + int dutyCycle; |
| 56 | + if (brightval == 100) dutyCycle = 250; |
| 57 | + else if (brightval == 75) dutyCycle = 130; |
| 58 | + else if (brightval == 50) dutyCycle = 70; |
| 59 | + else if (brightval == 25) dutyCycle = 20; |
| 60 | + else if (brightval == 0) dutyCycle = 0; |
| 61 | + else dutyCycle = ((brightval * 250) / 100); |
| 62 | + |
| 63 | + log_i("dutyCycle for bright 0-255: %d", dutyCycle); |
| 64 | + if (!ledcWrite(TFT_BL, dutyCycle)) { |
| 65 | + Serial.println("Failed to set brightness"); |
| 66 | + ledcDetach(TFT_BL); |
| 67 | + ledcAttach(TFT_BL, TFT_BRIGHT_FREQ, TFT_BRIGHT_Bits); |
| 68 | + ledcWrite(TFT_BL, dutyCycle); |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +/********************************************************************* |
| 73 | +** Function: InputHandler |
| 74 | +** Handles the variables PrevPress, NextPress, SelPress, AnyKeyPress and EscPress |
| 75 | +**********************************************************************/ |
| 76 | +void InputHandler(void) { |
| 77 | + static long d_tmp = millis(); |
| 78 | + if (millis() - d_tmp > 250 || LongPress) { // I know R3CK.. I Should NOT nest if statements.. |
| 79 | + // but it is needed to not keep SPI bus used without need, it save resources |
| 80 | + TouchPoint t; |
| 81 | +#ifdef DONT_USE_INPUT_TASK |
| 82 | + checkPowerSaveTime(); |
| 83 | +#endif |
| 84 | + if (touch.touched()) { |
| 85 | + auto t = touch.getPointScaled(); |
| 86 | + d_tmp = millis(); |
| 87 | +#ifdef DONT_USE_INPUT_TASK // need to reset the variables to avoid ghost click |
| 88 | + NextPress = false; |
| 89 | + PrevPress = false; |
| 90 | + UpPress = false; |
| 91 | + DownPress = false; |
| 92 | + SelPress = false; |
| 93 | + EscPress = false; |
| 94 | + AnyKeyPress = false; |
| 95 | + touchPoint.pressed = false; |
| 96 | +#endif |
| 97 | + if (rotation == 3) { |
| 98 | + t.y = (tftHeight + 20) - t.y; |
| 99 | + t.x = tftWidth - t.x; |
| 100 | + } |
| 101 | + if (rotation == 0) { |
| 102 | + int tmp = t.x; |
| 103 | + t.x = tftWidth - t.y; |
| 104 | + t.y = tmp; |
| 105 | + } |
| 106 | + if (rotation == 2) { |
| 107 | + int tmp = t.x; |
| 108 | + t.x = t.y; |
| 109 | + t.y = (tftHeight + 20) - tmp; |
| 110 | + } |
| 111 | + Serial.printf("\nTouch Pressed on x=%d, y=%d, rot=%d\n", t.x, t.y, rotation); |
| 112 | + log_i("\nTouch Pressed on x=%d, y=%d, rot=%d\n", t.x, t.y, rotation); |
| 113 | + |
| 114 | + if (!wakeUpScreen()) AnyKeyPress = true; |
| 115 | + else return; |
| 116 | + |
| 117 | + // Touch point global variable |
| 118 | + touchPoint.x = t.x; |
| 119 | + touchPoint.y = t.y; |
| 120 | + touchPoint.pressed = true; |
| 121 | + touchHeatMap(touchPoint); |
| 122 | + } |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +/********************************************************************* |
| 127 | +** Function: powerOff |
| 128 | +** location: mykeyboard.cpp |
| 129 | +** Turns off the device (or try to) |
| 130 | +**********************************************************************/ |
| 131 | +void powerOff() { esp_deep_sleep_start(); } |
| 132 | + |
| 133 | +/********************************************************************* |
| 134 | +** Function: checkReboot |
| 135 | +** location: mykeyboard.cpp |
| 136 | +** Btn logic to tornoff the device (name is odd btw) |
| 137 | +**********************************************************************/ |
| 138 | +void checkReboot() { /* No dedicated reboot button on NM-CYD-C5 */ } |
0 commit comments