|
| 1 | +#include "core/powerSave.h" |
| 2 | +#include "core/utils.h" |
| 3 | +#include <Wire.h> |
| 4 | +#include <XPowersLib.h> |
| 5 | +#include <interface.h> |
| 6 | + |
| 7 | +XPowersAXP2101 axp192; |
| 8 | +#include <TouchDrvFT6X36.hpp> |
| 9 | +TouchDrvFT6X36 touch; |
| 10 | + |
| 11 | +// Haptic |
| 12 | +#include "SensorDRV2605.hpp" |
| 13 | +SensorDRV2605 drv; |
| 14 | + |
| 15 | +/*************************************************************************************** |
| 16 | +** Function name: _setup_gpio() |
| 17 | +** Location: main.cpp |
| 18 | +** Description: initial setup for the device |
| 19 | +***************************************************************************************/ |
| 20 | +void _setup_gpio() { |
| 21 | + pinMode(16, INPUT); // Touch IRQ |
| 22 | + Wire.begin(10, 11); // sensors |
| 23 | + delay(10); |
| 24 | + Wire1.begin(39, 40); // touchscreen |
| 25 | + delay(10); |
| 26 | + _rtc.setWire(&Wire); // Cplus uses Wire1 default, the lib had been changed to accept setting I2C bus |
| 27 | + // StickCPlus uses BM8563 that is the same as PCF8536 |
| 28 | + axp192.init(Wire, 10, 11); |
| 29 | + axp192.setVbusVoltageLimit(XPOWERS_AXP2101_VBUS_VOL_LIM_4V36); |
| 30 | + axp192.setVbusCurrentLimit(XPOWERS_AXP2101_VBUS_CUR_LIM_900MA); |
| 31 | + axp192.setSysPowerDownVoltage(2600); |
| 32 | + axp192.setALDO1Voltage(3300); |
| 33 | + axp192.setALDO2Voltage(3300); |
| 34 | + axp192.setALDO3Voltage(3300); |
| 35 | + axp192.setALDO4Voltage(3300); |
| 36 | + axp192.setBLDO2Voltage(3300); |
| 37 | + axp192.setDC3Voltage(3300); |
| 38 | + axp192.enableDC3(); // gps |
| 39 | + axp192.disableDC2(); |
| 40 | + axp192.disableDC4(); |
| 41 | + axp192.disableDC5(); |
| 42 | + axp192.disableBLDO1(); |
| 43 | + axp192.disableCPUSLDO(); |
| 44 | + axp192.disableDLDO1(); |
| 45 | + axp192.disableDLDO2(); |
| 46 | + axp192.enableALDO1(); //! RTC VBAT |
| 47 | + axp192.enableALDO2(); //! TFT BACKLIGHT VDD |
| 48 | + axp192.enableALDO3(); //! Screen touch VDD |
| 49 | + axp192.enableALDO4(); //! Radio VDD |
| 50 | + axp192.enableBLDO2(); //! drv2605 enable |
| 51 | + // Set the time of pressing the button to turn off |
| 52 | + axp192.setPowerKeyPressOffTime(XPOWERS_POWEROFF_4S); |
| 53 | + // Set the button power-on press time |
| 54 | + axp192.setPowerKeyPressOnTime(XPOWERS_POWERON_128MS); |
| 55 | + // It is necessary to disable the detection function of the TS pin on the board |
| 56 | + // without the battery temperature detection function, otherwise it will cause abnormal charging |
| 57 | + axp192.disableTSPinMeasure(); |
| 58 | + // Enable internal ADC detection |
| 59 | + axp192.enableBattDetection(); |
| 60 | + axp192.enableVbusVoltageMeasure(); |
| 61 | + axp192.enableBattVoltageMeasure(); |
| 62 | + axp192.enableSystemVoltageMeasure(); |
| 63 | + // t-watch no chg led |
| 64 | + axp192.setChargingLedMode(XPOWERS_CHG_LED_OFF); |
| 65 | + axp192.disableIRQ(XPOWERS_AXP2101_ALL_IRQ); |
| 66 | + // Enable the required interrupt function |
| 67 | + axp192.enableIRQ( |
| 68 | + XPOWERS_AXP2101_BAT_INSERT_IRQ | XPOWERS_AXP2101_BAT_REMOVE_IRQ | // BATTERY |
| 69 | + XPOWERS_AXP2101_VBUS_INSERT_IRQ | XPOWERS_AXP2101_VBUS_REMOVE_IRQ | // VBUS |
| 70 | + XPOWERS_AXP2101_PKEY_SHORT_IRQ | XPOWERS_AXP2101_PKEY_LONG_IRQ | // POWER KEY |
| 71 | + XPOWERS_AXP2101_BAT_CHG_DONE_IRQ | XPOWERS_AXP2101_BAT_CHG_START_IRQ // CHARGE |
| 72 | + ); |
| 73 | + |
| 74 | + // Clear all interrupt flags |
| 75 | + axp192.clearIrqStatus(); |
| 76 | + // Set the precharge charging current |
| 77 | + axp192.setPrechargeCurr(XPOWERS_AXP2101_PRECHARGE_50MA); |
| 78 | + // Set constant current charge current limit |
| 79 | + axp192.setChargerConstantCurr(XPOWERS_AXP2101_CHG_CUR_300MA); |
| 80 | + // Set stop charging termination current |
| 81 | + axp192.setChargerTerminationCurr(XPOWERS_AXP2101_CHG_ITERM_25MA); |
| 82 | + // Set charge cut-off voltage |
| 83 | + axp192.setChargeTargetVoltage(XPOWERS_AXP2101_CHG_VOL_4V35); |
| 84 | + // Set RTC Battery voltage to 3.3V |
| 85 | + axp192.setButtonBatteryChargeVoltage(3300); |
| 86 | + axp192.enableButtonBatteryCharge(); |
| 87 | + |
| 88 | + touch.begin(Wire1, FT6X36_SLAVE_ADDRESS, 39, 40); |
| 89 | + touch.setSwapXY(true); |
| 90 | + touch.interruptPolling(); |
| 91 | + |
| 92 | + // Haptic driver |
| 93 | + if (!drv.begin(Wire, 10, 11)) { |
| 94 | + Serial.println("Failed to find DRV2605."); |
| 95 | + } else { |
| 96 | + Serial.println("Init DRV2605 Sensor success!"); |
| 97 | + drv.selectLibrary(1); |
| 98 | + drv.setMode(SensorDRV2605::MODE_INTTRIG); |
| 99 | + drv.useERM(); |
| 100 | + |
| 101 | + // Startup buzz |
| 102 | + drv.setWaveform(0, 70); |
| 103 | + drv.setWaveform(1, 0); |
| 104 | + drv.run(); |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +/*************************************************************************************** |
| 109 | +** Function name: _post_setup_gpio() |
| 110 | +** Location: main.cpp |
| 111 | +** Description: second stage gpio setup to make a few functions work |
| 112 | +***************************************************************************************/ |
| 113 | +void _post_setup_gpio() { |
| 114 | + pinMode(TFT_BL, OUTPUT); |
| 115 | + digitalWrite(TFT_BL, HIGH); |
| 116 | + ledcAttach(TFT_BL, TFT_BRIGHT_FREQ, TFT_BRIGHT_Bits); |
| 117 | + ledcWrite(TFT_BL, 255); |
| 118 | +} |
| 119 | + |
| 120 | +/*************************************************************************************** |
| 121 | +** Function name: getBattery() |
| 122 | +** location: display.cpp |
| 123 | +** Description: Delivers the battery value from 1-100 |
| 124 | +***************************************************************************************/ |
| 125 | +int getBattery() { |
| 126 | + int percent = axp192.getBatteryPercent(); |
| 127 | + return percent; |
| 128 | +} |
| 129 | + |
| 130 | +/********************************************************************* |
| 131 | +** Function: setBrightness |
| 132 | +** location: settings.cpp |
| 133 | +** set brightness value |
| 134 | +**********************************************************************/ |
| 135 | +void _setBrightness(uint8_t brightval) { |
| 136 | + int dutyCycle; |
| 137 | + if (brightval == 100) dutyCycle = 255; |
| 138 | + else if (brightval == 75) dutyCycle = 130; |
| 139 | + else if (brightval == 50) dutyCycle = 70; |
| 140 | + else if (brightval == 25) dutyCycle = 20; |
| 141 | + else if (brightval == 0) dutyCycle = 0; |
| 142 | + else dutyCycle = ((brightval * 255) / 100); |
| 143 | + |
| 144 | + // log_i("dutyCycle for bright 0-255: %d", dutyCycle); |
| 145 | + ledcWrite(TFT_BL, dutyCycle); |
| 146 | +} |
| 147 | + |
| 148 | +bool getTouched() { return digitalRead(16) == LOW; } |
| 149 | +struct TP { |
| 150 | + int16_t x[1], y[1]; |
| 151 | +}; |
| 152 | +/********************************************************************* |
| 153 | +** Function: InputHandler |
| 154 | +** Handles the variables PrevPress, NextPress, SelPress, AnyKeyPress and EscPress |
| 155 | +**********************************************************************/ |
| 156 | +void InputHandler(void) { |
| 157 | + TP t; |
| 158 | + static unsigned long tm = 0; |
| 159 | + if (millis() - tm > 200 || LongPress) { |
| 160 | + // I know R3CK.. I Should NOT nest if statements.. |
| 161 | + // but it is needed to not keep SPI bus used without need, it save resources |
| 162 | + if (getTouched()) { |
| 163 | + touch.getPoint(t.x, t.y, 1); |
| 164 | + // Serial.printf("\nRAW: Touch Pressed on x=%d, y=%d",t.x, t.y); |
| 165 | + if (rotation == 3) { |
| 166 | + t.y[0] = (tftHeight + 20) - t.y[0]; |
| 167 | + t.x[0] = t.x[0]; |
| 168 | + } |
| 169 | + if (rotation == 0) { |
| 170 | + int tmp = t.x[0]; |
| 171 | + t.x[0] = tftWidth - t.y[0]; |
| 172 | + t.y[0] = tftHeight - tmp; |
| 173 | + } |
| 174 | + if (rotation == 2) { |
| 175 | + int tmp = t.x[0]; |
| 176 | + t.x[0] = t.y[0]; |
| 177 | + t.y[0] = tmp; |
| 178 | + } |
| 179 | + if (rotation == 1) { t.x[0] = tftWidth - t.x[0]; } |
| 180 | + // Serial.printf("\nROT: Touch Pressed on x=%d, y=%d\n",t.x[0], t.y[0]); |
| 181 | + |
| 182 | + if (!wakeUpScreen()) AnyKeyPress = true; |
| 183 | + else return; |
| 184 | + |
| 185 | + // Touch point global variable |
| 186 | + touchPoint.x = t.x[0]; |
| 187 | + touchPoint.y = t.y[0]; |
| 188 | + touchPoint.pressed = true; |
| 189 | + touchHeatMap(touchPoint); |
| 190 | + |
| 191 | + tm = millis(); |
| 192 | + drv.setWaveform(0, 75); |
| 193 | + drv.setWaveform(1, 0); // end waveform |
| 194 | + drv.run(); |
| 195 | + } |
| 196 | + } |
| 197 | +} |
| 198 | + |
| 199 | +/********************************************************************* |
| 200 | +** Function: powerOff |
| 201 | +** location: mykeyboard.cpp |
| 202 | +** Turns off the device (or try to) |
| 203 | +**********************************************************************/ |
| 204 | +void powerOff() {} |
| 205 | + |
| 206 | +/********************************************************************* |
| 207 | +** Function: checkReboot |
| 208 | +** location: mykeyboard.cpp |
| 209 | +** Btn logic to turn off the device (name is odd btw) |
| 210 | +**********************************************************************/ |
| 211 | +void checkReboot() {} |
| 212 | + |
| 213 | +/*************************************************************************************** |
| 214 | +** Function name: isCharging() |
| 215 | +** Description: Determines if the device is charging |
| 216 | +***************************************************************************************/ |
| 217 | +bool isCharging() { |
| 218 | + return axp192.isCharging(); // Return the charging status from AXP |
| 219 | +} |
0 commit comments