|
| 1 | +#include "CYD28_TouchscreenR.h" |
| 2 | +#include "core/powerSave.h" |
| 3 | +#include "core/utils.h" |
| 4 | +#include <Arduino.h> |
| 5 | +#include <ELECHOUSE_CC1101_SRC_DRV.h> |
| 6 | +#include <driver/adc.h> |
| 7 | +#include <esp_adc_cal.h> |
| 8 | +#include <interface.h> |
| 9 | +#include <soc/adc_channel.h> |
| 10 | +#include <soc/soc_caps.h> |
| 11 | +CYD28_TouchR touch(320, 240); |
| 12 | + |
| 13 | +/*************************************************************************************** |
| 14 | +** Function name: _setup_gpio() |
| 15 | +** Location: main.cpp |
| 16 | +** Description: initial setup for the device |
| 17 | +***************************************************************************************/ |
| 18 | +void _setup_gpio() { |
| 19 | + pinMode(XPT2046_SPI_CONFIG_CS_GPIO_NUM, OUTPUT); |
| 20 | + digitalWrite(XPT2046_SPI_CONFIG_CS_GPIO_NUM, HIGH); |
| 21 | + pinMode(PWR_ON_PIN, OUTPUT); |
| 22 | + digitalWrite(PWR_ON_PIN, HIGH); |
| 23 | + pinMode(PWR_EN_PIN, OUTPUT); |
| 24 | + digitalWrite(PWR_EN_PIN, HIGH); |
| 25 | + bruceConfig.colorInverted = 0; |
| 26 | +} |
| 27 | + |
| 28 | +/*************************************************************************************** |
| 29 | +** Function name: _post_setup_gpio() |
| 30 | +** Location: main.cpp |
| 31 | +** Description: second stage gpio setup to make a few functions work |
| 32 | +***************************************************************************************/ |
| 33 | +void _post_setup_gpio() { |
| 34 | + CC_NRF_SPI.begin(XPT2046_SPI_BUS_SCLK_IO_NUM, XPT2046_SPI_BUS_MISO_IO_NUM, XPT2046_SPI_BUS_MOSI_IO_NUM); |
| 35 | + if (!touch.begin(&CC_NRF_SPI)) { Serial.println("Touchscreen initialization failed!"); } |
| 36 | + ELECHOUSE_cc1101.setSPIinstance(&CC_NRF_SPI); |
| 37 | +#define TFT_BRIGHT_CHANNEL 0 |
| 38 | +#define TFT_BRIGHT_Bits 8 |
| 39 | +#define TFT_BRIGHT_FREQ 5000 |
| 40 | + // Brightness control must be initialized after tft in this case @Pirata |
| 41 | + pinMode(TFT_BL, OUTPUT); |
| 42 | + ledcSetup(TFT_BRIGHT_CHANNEL, TFT_BRIGHT_FREQ, TFT_BRIGHT_Bits); // Channel 0, 10khz, 8bits |
| 43 | + ledcAttachPin(TFT_BL, TFT_BRIGHT_CHANNEL); |
| 44 | + ledcWrite(TFT_BRIGHT_CHANNEL, 255); |
| 45 | +} |
| 46 | + |
| 47 | +/********************************************************************* |
| 48 | +** Function: setBrightness |
| 49 | +** location: settings.cpp |
| 50 | +** set brightness value |
| 51 | +**********************************************************************/ |
| 52 | +void _setBrightness(uint8_t brightval) { |
| 53 | + int dutyCycle; |
| 54 | + if (brightval == 100) dutyCycle = 255; |
| 55 | + else if (brightval == 75) dutyCycle = 130; |
| 56 | + else if (brightval == 50) dutyCycle = 70; |
| 57 | + else if (brightval == 25) dutyCycle = 20; |
| 58 | + else if (brightval == 0) dutyCycle = 0; |
| 59 | + else dutyCycle = ((brightval * 255) / 100); |
| 60 | + |
| 61 | + // log_i("dutyCycle for bright 0-255: %d", dutyCycle); |
| 62 | + ledcWrite(TFT_BRIGHT_CHANNEL, dutyCycle); // Channel 0 |
| 63 | +} |
| 64 | + |
| 65 | +/********************************************************************* |
| 66 | +** Function: InputHandler |
| 67 | +** Handles the variables PrevPress, NextPress, SelPress, AnyKeyPress and EscPress |
| 68 | +**********************************************************************/ |
| 69 | +void InputHandler(void) { |
| 70 | + static long d_tmp = 0; |
| 71 | + if (millis() - d_tmp > 200 || LongPress) { |
| 72 | + // I know R3CK.. I Should NOT nest if statements.. |
| 73 | + // but it is needed to not keep SPI bus used without need, it save resources |
| 74 | + if (touch.touched()) { |
| 75 | + auto t = touch.getPointScaled(); |
| 76 | + // Serial.printf("\nRAW: Touch Pressed on x=%d, y=%d", t.x, t.y); |
| 77 | + if (bruceConfig.rotation == 3) { |
| 78 | + // t.y = t.y; |
| 79 | + t.x = tftWidth - t.x; |
| 80 | + } |
| 81 | + if (bruceConfig.rotation == 1) { |
| 82 | + t.y = (tftHeight + 20) - t.y; |
| 83 | + // t.x = t.x; |
| 84 | + } |
| 85 | + if (bruceConfig.rotation == 0) { |
| 86 | + int tmp = t.x; |
| 87 | + t.x = t.y; |
| 88 | + t.y = tmp; |
| 89 | + } |
| 90 | + if (bruceConfig.rotation == 2) { |
| 91 | + int tmp = t.x; |
| 92 | + t.x = tftWidth - t.y; |
| 93 | + t.y = (tftHeight + 20) - tmp; |
| 94 | + } |
| 95 | + // Serial.printf("\nROT: Touch Pressed on x=%d, y=%d\n", t.x, t.y); |
| 96 | + |
| 97 | + if (!wakeUpScreen()) AnyKeyPress = true; |
| 98 | + else goto END; |
| 99 | + |
| 100 | + // Touch point global variable |
| 101 | + touchPoint.x = t.x; |
| 102 | + touchPoint.y = t.y; |
| 103 | + touchPoint.pressed = true; |
| 104 | + touchHeatMap(touchPoint); |
| 105 | + END: |
| 106 | + d_tmp = millis(); |
| 107 | + } |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +/********************************************************************* |
| 112 | +** Function: powerOff |
| 113 | +** location: mykeyboard.cpp |
| 114 | +** Turns off the device (or try to) |
| 115 | +**********************************************************************/ |
| 116 | +void powerOff() { |
| 117 | + esp_sleep_enable_ext0_wakeup(GPIO_NUM_0, LOW); |
| 118 | + esp_deep_sleep_start(); |
| 119 | +} |
| 120 | + |
| 121 | +/********************************************************************* |
| 122 | +** Function: checkReboot |
| 123 | +** location: mykeyboard.cpp |
| 124 | +** Btn logic to tornoff the device (name is odd btw) |
| 125 | +**********************************************************************/ |
| 126 | +void checkReboot() {} |
| 127 | + |
| 128 | +/*************************************************************************************** |
| 129 | +** Function name: getBattery() |
| 130 | +** location: display.cpp |
| 131 | +** Description: Delivers the battery value from 1-100 |
| 132 | +***************************************************************************************/ |
| 133 | +int getBattery() { |
| 134 | + uint8_t percent; |
| 135 | + uint8_t _batAdcCh = ADC1_GPIO5_CHANNEL; |
| 136 | + uint8_t _batAdcUnit = 1; |
| 137 | + |
| 138 | + adc1_config_width(ADC_WIDTH_BIT_12); |
| 139 | + adc1_config_channel_atten((adc1_channel_t)_batAdcCh, ADC_ATTEN_DB_12); |
| 140 | + static esp_adc_cal_characteristics_t *adc_chars = nullptr; |
| 141 | + static constexpr int BASE_VOLATAGE = 3600; |
| 142 | + adc_chars = (esp_adc_cal_characteristics_t *)calloc(1, sizeof(esp_adc_cal_characteristics_t)); |
| 143 | + esp_adc_cal_characterize( |
| 144 | + (adc_unit_t)_batAdcUnit, ADC_ATTEN_DB_12, ADC_WIDTH_BIT_12, BASE_VOLATAGE, adc_chars |
| 145 | + ); |
| 146 | + int raw; |
| 147 | + raw = adc1_get_raw((adc1_channel_t)_batAdcCh); |
| 148 | + uint32_t volt = esp_adc_cal_raw_to_voltage(raw, adc_chars); |
| 149 | + |
| 150 | + float mv = volt * 2; |
| 151 | + percent = (mv - 3300) * 100 / (float)(4150 - 3350); |
| 152 | + |
| 153 | + return (percent < 0) ? 0 : (percent >= 100) ? 100 : percent; |
| 154 | +} |
0 commit comments