From 6a60cfb8721dd2e9085e4203eb16ce499a1e312f Mon Sep 17 00:00:00 2001 From: Beslan Date: Sun, 20 Feb 2022 12:14:43 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=B2=D0=B5=20=D1=8D=D1=82=D0=B8=D0=BA?= =?UTF-8?q?=D0=B5=D1=82=D0=BA=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ili9341.cpp | 228 ++++++++++++++++++++++++++-------------------------- tsc2046.cpp | 144 ++++++++++++++++++--------------- tsc2046.h | 34 ++++---- 3 files changed, 213 insertions(+), 193 deletions(-) diff --git a/ili9341.cpp b/ili9341.cpp index ebe77a7..fea69b8 100644 --- a/ili9341.cpp +++ b/ili9341.cpp @@ -11,166 +11,170 @@ ILI9341::ILI9341(PinName MOSI, PinName MISO, PinName SCK, PinName DC, PinName RS rst = 1; - osDelay(10); + ThisThread::sleep_for(10ms); +// (void)osDelay(10); rst = 0; - osDelay(10); + ThisThread::sleep_for(10ms); +// osDelay(10); rst = 1; - osDelay(10); + ThisThread::sleep_for(10ms); +// osDelay(10); - LCD_WR_REG(0x01); //software reset - osDelay(50); - LCD_WR_REG(0x28); + LCD_WR_REG(0x01U); //software reset + ThisThread::sleep_for(50ms); +// osDelay(50); + LCD_WR_REG(0x28U); // display off //--------------------------------------------------------- // magic? - LCD_WR_REG(0xcf); - LCD_WR_Data(0x00); - LCD_WR_Data(0x83); - LCD_WR_Data(0x30); + LCD_WR_REG(0xcfU); + LCD_WR_Data(0x00U); + LCD_WR_Data(0x83U); + LCD_WR_Data(0x30U); - LCD_WR_REG(0xed); - LCD_WR_Data(0x64); - LCD_WR_Data(0x03); - LCD_WR_Data(0x12); - LCD_WR_Data(0x81); - LCD_WR_REG(0xe8); - LCD_WR_Data(0x85); - LCD_WR_Data(0x01); - LCD_WR_Data(0x79); - LCD_WR_REG(0xcb); - LCD_WR_Data(0x39); - LCD_WR_Data(0x2c); - LCD_WR_Data(0x00); - LCD_WR_Data(0x34); - LCD_WR_Data(0x02); - LCD_WR_REG(0xf7); - LCD_WR_Data(0x20); - LCD_WR_REG(0xea); - LCD_WR_Data(0x00); - LCD_WR_Data(0x00); + LCD_WR_REG(0xedU); + LCD_WR_Data(0x64U); + LCD_WR_Data(0x03U); + LCD_WR_Data(0x12U); + LCD_WR_Data(0x81U); + LCD_WR_REG(0xe8U); + LCD_WR_Data(0x85U); + LCD_WR_Data(0x01U); + LCD_WR_Data(0x79U); + LCD_WR_REG(0xcbU); + LCD_WR_Data(0x39U); + LCD_WR_Data(0x2cU); + LCD_WR_Data(0x00U); + LCD_WR_Data(0x34U); + LCD_WR_Data(0x02U); + LCD_WR_REG(0xf7U); + LCD_WR_Data(0x20U); + LCD_WR_REG(0xeaU); + LCD_WR_Data(0x00U); + LCD_WR_Data(0x00U); //------------power control------------------------------ - LCD_WR_REG(0xc0); //power control - LCD_WR_Data(0x26); - LCD_WR_REG(0xc1); //power control - LCD_WR_Data(0x11); + LCD_WR_REG(0xc0U); //power control + LCD_WR_Data(0x26U); + LCD_WR_REG(0xc1U); //power control + LCD_WR_Data(0x11U); //--------------VCOM - LCD_WR_REG(0xc5); //vcom control - LCD_WR_Data(0x35); //35 - LCD_WR_Data(0x3e); //3E - LCD_WR_REG(0xc7); //vcom control - LCD_WR_Data(0xbe); // 0x94 + LCD_WR_REG(0xc5U); //vcom control + LCD_WR_Data(0x35U); //35 + LCD_WR_Data(0x3eU); //3E + LCD_WR_REG(0xc7U); //vcom control + LCD_WR_Data(0xbeU); // 0x94 //------------memory access control------------------------ - LCD_WR_REG(0x36); + LCD_WR_REG(0x36U); // memory access control LCD_WR_Data(0xE8U); //Поворот экрана на 0 градусов // LCD_WR_Data(0x28U); //Поворот экрана на 180 градусов - LCD_WR_REG(0x3a); // pixel format set - LCD_WR_Data(0x55); //16bit /pixel + LCD_WR_REG(0x3aU); // pixel format set + LCD_WR_Data(0x55U); //16bit /pixel //----------------- frame rate------------------------------ - LCD_WR_REG(0xb1); + LCD_WR_REG(0xb1U); // frame rate LCD_WR_Data(0x00U); LCD_WR_Data(0x1BU); //70 //----------------Gamma--------------------------------- - LCD_WR_REG(0xf2); // 3Gamma Function Disable - LCD_WR_Data(0x08); - LCD_WR_REG(0x26); - LCD_WR_Data(0x01); // gamma set 4 gamma curve 01/02/04/08 + LCD_WR_REG(0xf2U); // 3Gamma Function Disable + LCD_WR_Data(0x08U); + LCD_WR_REG(0x26U); + LCD_WR_Data(0x01U); // gamma set 4 gamma curve 01/02/04/08 - LCD_WR_REG(0xE0); //positive gamma correction - LCD_WR_Data(0x1f); - LCD_WR_Data(0x1a); - LCD_WR_Data(0x18); - LCD_WR_Data(0x0a); - LCD_WR_Data(0x0f); - LCD_WR_Data(0x06); - LCD_WR_Data(0x45); - LCD_WR_Data(0x87); - LCD_WR_Data(0x32); - LCD_WR_Data(0x0a); - LCD_WR_Data(0x07); - LCD_WR_Data(0x02); - LCD_WR_Data(0x07); - LCD_WR_Data(0x05); - LCD_WR_Data(0x00); - LCD_WR_REG(0xE1); //negamma correction - LCD_WR_Data(0x00); - LCD_WR_Data(0x25); - LCD_WR_Data(0x27); - LCD_WR_Data(0x05); - LCD_WR_Data(0x10); - LCD_WR_Data(0x09); - LCD_WR_Data(0x3a); - LCD_WR_Data(0x78); - LCD_WR_Data(0x4d); - LCD_WR_Data(0x05); - LCD_WR_Data(0x18); - LCD_WR_Data(0x0d); - LCD_WR_Data(0x38); - LCD_WR_Data(0x3a); - LCD_WR_Data(0x1f); + LCD_WR_REG(0xE0U); //positive gamma correction + LCD_WR_Data(0x1fU); + LCD_WR_Data(0x1aU); + LCD_WR_Data(0x18U); + LCD_WR_Data(0x0aU); + LCD_WR_Data(0x0fU); + LCD_WR_Data(0x06U); + LCD_WR_Data(0x45U); + LCD_WR_Data(0x87U); + LCD_WR_Data(0x32U); + LCD_WR_Data(0x0aU); + LCD_WR_Data(0x07U); + LCD_WR_Data(0x02U); + LCD_WR_Data(0x07U); + LCD_WR_Data(0x05U); + LCD_WR_Data(0x00U); + LCD_WR_REG(0xE1U); //negamma correction + LCD_WR_Data(0x00U); + LCD_WR_Data(0x25U); + LCD_WR_Data(0x27U); + LCD_WR_Data(0x05U); + LCD_WR_Data(0x10U); + LCD_WR_Data(0x09U); + LCD_WR_Data(0x3aU); + LCD_WR_Data(0x78U); + LCD_WR_Data(0x4dU); + LCD_WR_Data(0x05U); + LCD_WR_Data(0x18U); + LCD_WR_Data(0x0dU); + LCD_WR_Data(0x38U); + LCD_WR_Data(0x3aU); + LCD_WR_Data(0x1fU); //--------------ddram --------------------- - LCD_WR_REG(0x2a); + LCD_WR_REG(0x2aU); // column set // size = 239 - LCD_WR_Data(0x00); - LCD_WR_Data(0x00); - LCD_WR_Data(0x00); - LCD_WR_Data(0xEF); - LCD_WR_REG(0x2b); + LCD_WR_Data(0x00U); + LCD_WR_Data(0x00U); + LCD_WR_Data(0x00U); + LCD_WR_Data(0xEFU); + LCD_WR_REG(0x2bU); // page address set // size = 319 - LCD_WR_Data(0x00); - LCD_WR_Data(0x00); - LCD_WR_Data(0x01); - LCD_WR_Data(0x3F); + LCD_WR_Data(0x00U); + LCD_WR_Data(0x00U); + LCD_WR_Data(0x01U); + LCD_WR_Data(0x3FU); // LCD_WR_REG(0x34); //LCD_WR_REG(0x35); // tearing effect off // tearing effect on // LCD_WR_REG(0xb4); // display inversion // LCD_WR_Data(0x00); - LCD_WR_REG(0xb7); //entry mode set - LCD_WR_Data(0x07); + LCD_WR_REG(0xb7U); //entry mode set + LCD_WR_Data(0x07U); //-----------------display--------------------- - LCD_WR_REG(0xb6); + LCD_WR_REG(0xb6U); // display function control - LCD_WR_Data(0x0a); - LCD_WR_Data(0x82); - LCD_WR_Data(0x27); - LCD_WR_Data(0x00); - LCD_WR_REG(0x11); //sleep out - osDelay(100); - LCD_WR_REG(0x29); // display on + LCD_WR_Data(0x0aU); + LCD_WR_Data(0x82U); + LCD_WR_Data(0x27U); + LCD_WR_Data(0x00U); + LCD_WR_REG(0x11U); //sleep out + ThisThread::sleep_for(100ms); + LCD_WR_REG(0x29U); // display on } void ILI9341::LCD_WR_Data(uint8_t Data) { dc = 1; - dispPort.write(Data); + (void)dispPort.write(static_cast(Data)); } void ILI9341::LCD_WR_REG(uint8_t Reg) { dc = 0; - dispPort.write(Reg); + (void)dispPort.write(static_cast(Reg)); } void ILI9341::LCD_setaddress(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2) //set coordinate for print or other function { - LCD_WR_REG(0x2A); - LCD_WR_Data(x1 >> 8); - LCD_WR_Data(x1); - LCD_WR_Data(x2 >> 8); - LCD_WR_Data(x2); + LCD_WR_REG(0x2AU); + LCD_WR_Data(x1 >> 8U); + LCD_WR_Data(static_cast(x1)); + LCD_WR_Data(x2 >> 8U); + LCD_WR_Data(static_cast(x2)); - LCD_WR_REG(0x2B); - LCD_WR_Data(y1 >> 8); - LCD_WR_Data(y1); - LCD_WR_Data(y2 >> 8); - LCD_WR_Data(y2); + LCD_WR_REG(0x2BU); + LCD_WR_Data(y1 >> 8U); + LCD_WR_Data(static_cast(y1)); + LCD_WR_Data(y2 >> 8U); + LCD_WR_Data(static_cast(y2)); - LCD_WR_REG(0x2C); //memory write + LCD_WR_REG(0x2CU); //memory write } void ILI9341::transmitData(const uint8_t *pixels, uint16_t x, uint16_t y, uint16_t w, uint16_t h) { diff --git a/tsc2046.cpp b/tsc2046.cpp index 273b504..b53a387 100644 --- a/tsc2046.cpp +++ b/tsc2046.cpp @@ -1,11 +1,11 @@ #include "tsc2046.h" -#define CMD_X 0xD1 -#define CMD_Y 0x91 -#define CMD_ENABLE_IRQ 0x80 +constexpr char CMD_X=0xD1U; +constexpr char CMD_Y=0x91U; +constexpr char CMD_ENABLE_IRQ=0x80U; #define RESCALE_FACTOR 1000000 -#define EE_MEM_ADDR 400 +constexpr uint32_t EE_MEM_ADDR=400U; const constexpr auto dispWidth = 320; const constexpr auto dispHeight = 240; @@ -18,33 +18,36 @@ const constexpr auto yd1 = (10 * dispHeight) / 100; const constexpr auto yd2 = (50 * dispHeight) / 100; const constexpr auto yd3 = (90 * dispHeight) / 100; -TSC2046::TSC2046(EEPROM* mem, PinName MOSI, PinName MISO, PinName SCK, PinName CS, PinName INT) +TSC2046::TSC2046(EEPROM* mem, PinName MOSI, PinName MISO, PinName SCK, PinName CS, PinName INT): + tpPort(MOSI, MISO, SCK), + tpCS(CS), + tpInt(INT, PullUp) { - tpPort = new SPI(MOSI, MISO, SCK); - tpCS = new DigitalOut(CS); - tpInt = new DigitalIn(INT, PullUp); +// tpPort = new SPI(MOSI, MISO, SCK); +// tpCS = new DigitalOut(CS); +// tpInt = new DigitalIn(INT, PullUp); eeprom = mem; - eeprom->read(EE_MEM_ADDR, (uint8_t*)&touchCoeff, sizeof(touchCoeff)); - if (touchCoeff.touchIsCalibrated > 2) - touchCoeff.touchIsCalibrated = false; + eeprom->read(EE_MEM_ADDR, reinterpret_cast(&touchCoeff), sizeof(touchCoeff)); + if (touchCoeff.touchIsCalibrated > 2U) { touchCoeff.touchIsCalibrated = false; } - readValue(CMD_ENABLE_IRQ); // Enable IRQ + (void)readValue(CMD_ENABLE_IRQ); // Enable IRQ } void TSC2046::GetRawXY(uint16_t* X, uint16_t* Y) { - readValue(CMD_X); // Dummy read - disable PenIRQ + (void)readValue(CMD_X); // Dummy read - disable PenIRQ *X = readValue(CMD_X); // Read X-Value - readValue(CMD_Y); // Dummy read - disable PenIRQ + (void)readValue(CMD_Y); // Dummy read - disable PenIRQ *Y = readValue(CMD_Y); // Read Y-Value - readValue(CMD_ENABLE_IRQ); // Enable IRQ + (void)readValue(CMD_ENABLE_IRQ); // Enable IRQ } void TSC2046::GetXY(int32_t* X, int32_t* Y) { - uint16_t x, y; + uint16_t x; + uint16_t y; if (DetectTouch()) { GetRawXY(&x, &y); *X = (touchCoeff.cali_A * x + touchCoeff.cali_B * y + touchCoeff.cali_C) / RESCALE_FACTOR; @@ -52,116 +55,129 @@ void TSC2046::GetXY(int32_t* X, int32_t* Y) } } -uint16_t TSC2046::readValue(uint16_t port) +uint16_t TSC2046::readValue(char port) { - static char txbuf[3] = { 0 }; - static char rxbuf[3] = { 0 }; + static char txbuf[3] = { 0U }; + static char rxbuf[3] = { 0U }; txbuf[0] = port; - tpCS->write(0); - tpPort->write(txbuf, 3, rxbuf, 3); - tpCS->write(1); + tpCS.write(0); + (void)tpPort.write(txbuf, 3, rxbuf, 3); + tpCS.write(1); return ((uint16_t)rxbuf[1] << 5) | (rxbuf[2] >> 3); } bool TSC2046::DetectTouch() { - return !tpInt->read(); + return !tpInt.read(); } void TSC2046::coeffCalc() { - - double temp1, temp2; - double cal_A = 0.0, cal_B = 0.0, cal_C = 0.0, cal_D = 0.0, cal_E = 0.0, cal_F = 0.0; + double temp1; + double temp2; + double cal_A = 0.0; + double cal_B = 0.0; + double cal_C = 0.0; + double cal_D = 0.0; + double cal_E = 0.0; + double cal_F = 0.0; //A - temp1 = ((double)xd1 * ((double)yt2 - (double)yt3)) + ((double)xd2 * ((double)yt3 - (double)yt1)) + ((double)xd3 * ((double)yt1 - (double)yt2)); - temp2 = ((double)xt1 * ((double)yt2 - (double)yt3)) + ((double)xt2 * ((double)yt3 - (double)yt1)) + ((double)xt3 * ((double)yt1 - (double)yt2)); + temp1 = (static_cast(xd1) * (static_cast(yt2) - static_cast(yt3))) + (static_cast(xd2) + * (static_cast(yt3) - static_cast(yt1))) + (static_cast(xd3) + * (static_cast(yt1) - static_cast(yt2))); + temp2 = (static_cast(xt1) * (static_cast(yt2) - static_cast(yt3))) + (static_cast(xt2) + * (static_cast(yt3) - static_cast(yt1))) + (static_cast(xt3) * + (static_cast(yt1) - static_cast(yt2))); cal_A = temp1 / temp2; - touchCoeff.cali_A = (int32_t)((double)cal_A * RESCALE_FACTOR); + touchCoeff.cali_A = static_cast(cal_A * RESCALE_FACTOR); //B - temp1 = (cal_A * ((double)xt3 - (double)xt2)) + (double)xd2 - (double)xd3; - temp2 = (double)yt2 - (double)yt3; + temp1 = (cal_A * (static_cast(xt3) - static_cast(xt2))) + static_cast(xd2) - static_cast(xd3); + temp2 = static_cast(yt2) - static_cast(yt3); cal_B = temp1 / temp2; - touchCoeff.cali_B = (int32_t)((double)cal_B * RESCALE_FACTOR); + touchCoeff.cali_B = static_cast(cal_B * RESCALE_FACTOR); //C - cal_C = (double)xd3 - (cal_A * (double)xt3) - (cal_B * (double)yt3); - touchCoeff.cali_C = (int32_t)(cal_C * RESCALE_FACTOR); + cal_C = static_cast(xd3) - (cal_A * static_cast(xt3)) - (cal_B * static_cast(yt3)); + touchCoeff.cali_C = static_cast(cal_C * RESCALE_FACTOR); //D - temp1 = ((double)yd1 * ((double)yt2 - (double)yt3)) + ((double)yd2 * ((double)yt3 - (double)yt1)) + ((double)yd3 * ((double)yt1 - (double)yt2)); - temp2 = ((double)xt1 * ((double)yt2 - (double)yt3)) + ((double)xt2 * ((double)yt3 - (double)yt1)) + ((double)xt3 * ((double)yt1 - (double)yt2)); - cal_D = (double)temp1 / (double)temp2; - touchCoeff.cali_D = (int32_t)(cal_D * RESCALE_FACTOR); + temp1 = (static_cast(yd1) * (static_cast(yt2) - static_cast(yt3))) + (static_cast(yd2) + * (static_cast(yt3) - static_cast(yt1))) + (static_cast(yd3) + * (static_cast(yt1) - static_cast(yt2))); + temp2 = (static_cast(xt1) * (static_cast(yt2) - static_cast(yt3))) + (static_cast(xt2) + * (static_cast(yt3) - static_cast(yt1))) + (static_cast(xt3) + * (static_cast(yt1) - static_cast(yt2))); + cal_D = static_cast(temp1) / static_cast(temp2); + touchCoeff.cali_D = static_cast(cal_D * RESCALE_FACTOR); //E - temp1 = (cal_D * ((double)xt3 - (double)xt2)) + (double)yd2 - (double)yd3; - temp2 = (double)yt2 - (double)yt3; - cal_E = (double)temp1 / (double)temp2; - touchCoeff.cali_E = (int32_t)(cal_E * RESCALE_FACTOR); + temp1 = (cal_D * (static_cast(xt3) - static_cast(xt2))) + static_cast(yd2) - static_cast(yd3); + temp2 = static_cast(yt2) - static_cast(yt3); + cal_E = static_cast(temp1) / static_cast(temp2); + touchCoeff.cali_E = static_cast(cal_E * RESCALE_FACTOR); //F - cal_F = (double)yd3 - cal_D * (double)xt3 - cal_E * (double)yt3; - touchCoeff.cali_F = (int32_t)(cal_F * RESCALE_FACTOR); + cal_F = static_cast(yd3) - cal_D * static_cast(xt3) - cal_E * static_cast(yt3); + touchCoeff.cali_F = static_cast(cal_F * RESCALE_FACTOR); } -uint16_t TSC2046::getXd1(void) +int TSC2046::getXd1() { return xd1; } -uint16_t TSC2046::getXd2(void) +int TSC2046::getXd2() { return xd2; } -uint16_t TSC2046::getXd3(void) +int TSC2046::getXd3() { return xd3; } -uint16_t TSC2046::getYd1(void) +int TSC2046::getYd1() { return yd1; } -uint16_t TSC2046::getYd2(void) +int TSC2046::getYd2() { return yd2; } -uint16_t TSC2046::getYd3(void) +int TSC2046::getYd3() { return yd3; } -void TSC2046::setXt1(uint32_t xt1) +void TSC2046::setXt1(uint32_t _xt1) { - this->xt1 = xt1; + this->xt1 = _xt1; } -void TSC2046::setXt2(uint32_t xt2) +void TSC2046::setXt2(uint32_t _xt2) { - this->xt2 = xt2; + this->xt2 = _xt2; } -void TSC2046::setXt3(uint32_t xt3) +void TSC2046::setXt3(uint32_t _xt3) { - this->xt3 = xt3; + this->xt3 = _xt3; } -void TSC2046::setYt1(uint32_t yt1) +void TSC2046::setYt1(uint32_t _yt1) { - this->yt1 = yt1; + this->yt1 = _yt1; } -void TSC2046::setYt2(uint32_t yt2) +void TSC2046::setYt2(uint32_t _yt2) { - this->yt2 = yt2; + this->yt2 = _yt2; } -void TSC2046::setYt3(uint32_t yt3) +void TSC2046::setYt3(uint32_t _yt3) { - this->yt3 = yt3; + this->yt3 = _yt3; } -bool TSC2046::getCalibrated() +bool TSC2046::getCalibrated() const { return touchCoeff.touchIsCalibrated; } @@ -169,5 +185,5 @@ bool TSC2046::getCalibrated() void TSC2046::setCalibrated() { touchCoeff.touchIsCalibrated = true; - eeprom->write(EE_MEM_ADDR, (uint8_t*)&touchCoeff, sizeof(touchCoeff)); + eeprom->write(EE_MEM_ADDR, reinterpret_cast(&touchCoeff), sizeof(touchCoeff)); } diff --git a/tsc2046.h b/tsc2046.h index 78f4e7b..56eb398 100644 --- a/tsc2046.h +++ b/tsc2046.h @@ -7,21 +7,21 @@ public: void GetXY(int32_t* X, int32_t* Y); - uint16_t getXd1(void); - uint16_t getXd2(void); - uint16_t getXd3(void); - uint16_t getYd1(void); - uint16_t getYd2(void); - uint16_t getYd3(void); + static int getXd1(); + static int getXd2(); + static int getXd3(); + static int getYd1(); + static int getYd2(); + static int getYd3(); - void setXt1(uint32_t xt1); - void setXt2(uint32_t xt2); - void setXt3(uint32_t xt3); - void setYt1(uint32_t yt1); - void setYt2(uint32_t yt2); - void setYt3(uint32_t yt3); + void setXt1(uint32_t _xt1); + void setXt2(uint32_t _xt2); + void setXt3(uint32_t _xt3); + void setYt1(uint32_t _yt1); + void setYt2(uint32_t _yt2); + void setYt3(uint32_t _yt3); void setCalibrated(); - bool getCalibrated(); + bool getCalibrated() const; bool DetectTouch(); @@ -49,9 +49,9 @@ private: uint32_t yt2; uint32_t yt3; - SPI* tpPort; - DigitalOut* tpCS; - DigitalIn* tpInt; + SPI tpPort; + DigitalOut tpCS; + DigitalIn tpInt; EEPROM* eeprom; - uint16_t readValue(uint16_t port); + uint16_t readValue(char port); };