My first mbed-os 5.x test project with MAX32630FTHR and Adafruit 2.4" TFT with touch.
Dependencies: BMI160 SPI_STMPE610 USBDevice UniGraphic max32630fthr
My first test program of mbed-os 5.x, using MAX32630FTHR and Adafruit 2.4" TFT with Touch.
On 22-Sep-2017 Monitoring both Acc and Gyr added to the screen 3 Controlling the backlight via STMPE610 GPIO-2 added
main.cpp
- Committer:
- Rhyme
- Date:
- 2017-09-21
- Revision:
- 0:a4d7417f7672
- Child:
- 1:a890937c9c30
File content as of revision 0:a4d7417f7672:
/** * TFT test MAX32630FTHR * MCU Board: MAX32630FTHR * TFT Module: Adafruit 2.4 TFT with touch */ #include "mbed.h" #include "max32630fthr.h" #include "USBSerial.h" #include "ILI9341.h" #include "SPI_STMPE610.h" #include "Arial12x12.h" #include "Arial24x23.h" #include "Arial28x28.h" #include "Arial43x48_numb.h" #include "BMI160.h" #define PIN_MOSI SPI2_MOSI #define PIN_MISO SPI2_MISO #define PIN_SCLK SPI2_SCK #define PIN_TSC_CS P3_3 #define PIN_CS_TFT P5_3 #define PIN_RESET_TFT P1_7 // dummy #define PIN_DC_TFT P5_4 #define PIN_LED_R P2_4 #define PIN_LED_G P2_5 #define PIN_LED_B P2_6 #define PIN_RX2 P2_0 #define PIN_TX2 P2_1 #define PIN_SDA2 P5_7 #define PIN_SCL2 P6_0 #define BMI160_I2C_ADDRESS 0x68 MAX32630FTHR *sakura = 0 ; ILI9341 *tft = 0 ; SPI_STMPE610 *tsc = 0 ; DigitalOut *ledR = 0 ; DigitalOut *ledG = 0 ; DigitalOut *ledB = 0 ; BMI160 *acc = 0 ; // Hardware serial port over DAPLink // Serial daplink(P2_1, P2_0); // Virtual serial port over USB USBSerial *tty = 0 ; // USBSerial microUSB; int page = 0 ; int numPage = 4 ; extern void doMaze(void) ; void init_hardware(void) { sakura = new MAX32630FTHR(MAX32630FTHR::VIO_3V3) ; tty = new USBSerial() ; tsc = new SPI_STMPE610(SPI2_MOSI, SPI2_MISO, SPI2_SCK, PIN_TSC_CS) ; tsc->spi_format(8, 0) ; tsc->calibrate(3552, 400, 439, 3680) ; tft = new ILI9341(SPI_8, 16000000, SPI2_MOSI, SPI2_MISO, SPI2_SCK, PIN_CS_TFT, PIN_RESET_TFT, PIN_DC_TFT, "Adafruit2.4") ; ledR = new DigitalOut(PIN_LED_R) ; ledG = new DigitalOut(PIN_LED_G) ; ledB = new DigitalOut(PIN_LED_B) ; acc = new BMI160(PIN_SDA2, PIN_SCL2, BMI160_I2C_ADDRESS) ; // acc_set_pmu_mode 0x10 | ACC_PMU_XXX acc->setCMD(0x10 | ACC_PMU_NORMAL) ; Thread::wait(100) ; acc->setCMD(0x14 | GYR_PMU_NORMAL) ; Thread::wait(100) ; acc->setCMD(0x08 | MAG_PMU_SUSPEND) ; Thread::wait(100) ; acc->setCMD(0x03) ; /* start_foc */ Thread::wait(100) ; } void initTFT(void) { //Configure the display driver tft->BusEnable(true) ; tft->FastWindow(true) ; tft->background(Black); tft->foreground(White); Thread::wait(0.01) ; tft->cls(); tft->BusEnable(false) ; } void screen1(void) // Welcome Screen { tft->BusEnable(true) ; // backlight = 0 ; tft->background(White) ; wait(0.1) ; tft->cls() ; wait(0.1) ; tft->set_font((unsigned char*) Arial24x23); tft->foreground(Red) ; tft->locate(80, 40) ; tft->printf("MBED") ; tft->foreground(Blue); tft->locate(60, 80) ; tft->printf("2.4\"TFT") ; tft->locate(40, 120) ; tft->printf("with touch") ; tft->foreground(Black); tft->set_font((unsigned char*) Arial12x12); tft->foreground(Blue) ; tft->locate(30, 180) ; tft->printf("This program is running on") ; tft->locate(30, 200) ; tft->printf("Maxim MAX32630FTHR with") ; tft->locate(30, 220) ; tft->printf("a program developed in mbed") ; tft->foreground(Green) ; tft->locate(30, 260) ; tft->printf("To advance demo page, touch") ; tft->locate(30, 280) ; tft->printf("and hold right side of screen") ; tft->locate(30, 300) ; tft->printf("until the next screen starts") ; tft->BusEnable(false) ; // backlight = 1 ; } void screen2(void) // Graphics { //Draw some graphics int i, x[2], y[2] ; // backlight = 0 ; tft->BusEnable(true) ; tft->background(Black); wait(0.1) ; tft->foreground(White); wait(0.1) ; tft->cls() ; wait(0.1) ; tft->set_font((unsigned char*) Arial12x12); tft->locate(90,0); tft->printf("Graphics"); x[0] = 25 ; x[1] = 224 ; y[0] = 20 ; y[1] = 219 ; for (i = 20 ; i < 220 ; i += 10) { tft->line(i+5, y[0], i+5, y[1], Blue) ; tft->line(x[0], i, x[1], i, Blue) ; } tft->line(125, y[0], 125, y[1], Green) ; tft->line(x[0], 120, x[1], 120, Green) ; tft->rect(x[0],y[0], x[1], y[1], Green) ; tft->locate(10, 20) ; tft->printf("V") ; tft->locate(0, 115) ; tft->printf("0.0") ; tft->locate(115, 225) ; tft->printf("0.0") ; tft->locate(215, 225) ; tft->printf("T") ; double s; for (int i = x[0]; i < 225; i++) { s = 40 * sin((long double)i / 20); tft->pixel(i, 120 + (int)s, White); } tft->fillrect(10, 240, 229, 309, White) ; tft->rect(10, 240, 229, 309, Red) ; tft->rect(11, 241, 228, 308, Red) ; tft->background(White) ; tft->foreground(Black) ; tft->locate(20, 250) ; tft->printf("With QVGA resolution") ; tft->locate(20, 270) ; tft->printf("simple graphics drawing") ; tft->locate(20, 290) ; tft->printf("capability is provided") ; tft->BusEnable(false) ; // backlight = 1 ; } double clip(double src) { double value ; value = src ; if (value < 0.0) { value = 0.0 ; } else if (value > 2.0) { value = 2.0 ; } return( value ) ; } void screen3(void) { int t = 0 ; int pt = 0 ; // previous t int i, x, y ; unsigned int data[3] ; // for x, y, z unsigned int prev[3] ; unsigned short signalHeight = 39 ; unsigned short xoffset = 30 ; unsigned short yoffset = 120 ; unsigned short zoffset = 210 ; unsigned short paneX[2] = {20, 235} ; unsigned short paneH = 81 ; float value[3] ; float acc_range ; // backlight = 1 ; acc_range = (float)acc->getAccRange() ; tft->BusEnable(true) ; tft->background(Black) ; tft->foreground(White) ; // tft->cls() ; tft->fillrect(paneX[0], xoffset, paneX[1], xoffset+paneH, Black) ; tft->fillrect(paneX[0], yoffset, paneX[1], yoffset+paneH, Black) ; tft->fillrect(paneX[0], zoffset, paneX[1], zoffset+paneH, Black) ; tft->fillrect(paneX[0], xoffset, paneX[1], xoffset+paneH, Black) ; for (i = 0 ; i < 10 ; i++ ) { y = i * 8 ; tft->line(paneX[0], xoffset + y, paneX[1], xoffset + y, Blue) ; tft->line(paneX[0], yoffset + y, paneX[1], yoffset + y, Blue) ; tft->line(paneX[0], zoffset + y, paneX[1], zoffset + y, Blue) ; } for (x = 30 ; x < paneX[1] ; x += 10 ) { tft->line(x, xoffset, x, xoffset+paneH, Blue) ; tft->line(x, yoffset, x, yoffset+paneH, Blue) ; tft->line(x, zoffset, x, zoffset+paneH, Blue) ; } tft->rect(paneX[0], xoffset, paneX[1], xoffset+paneH, White) ; tft->rect(paneX[0], yoffset, paneX[1], yoffset+paneH, White) ; tft->rect(paneX[0], zoffset, paneX[1], zoffset+paneH, White) ; tft->set_font((unsigned char*) Arial12x12); tft->locate(5, xoffset+30) ; tft->printf("X") ; tft->locate(5, yoffset+30) ; tft->printf("Y") ; tft->locate(5, zoffset+30) ; tft->printf("Z") ; tft->locate(70, 10) ; tft->printf("MAX32630FTHR") ; tft->locate(90, 300) ; tft->printf("BMI160") ; acc->getAcc(value) ; prev[0] = xoffset + (signalHeight * clip((acc_range + value[0])/(acc_range * 1.1))) ; prev[1] = yoffset + (signalHeight * clip((acc_range + value[1])/(acc_range * 1.1))) ; prev[2] = zoffset + (signalHeight * clip((acc_range + value[2])/(acc_range * 1.1))) ; pt = paneX[0] ; // backlight = 1 ; for(t = 21 ; t < paneX[1] ; t++) { acc->getAcc(value) ; data[0] = xoffset + (signalHeight * clip((acc_range + value[0])/(acc_range * 1.1))) ; data[1] = yoffset + (signalHeight * clip((acc_range + value[1])/(acc_range * 1.1))) ; data[2] = zoffset + (signalHeight * clip((acc_range + value[2])/(acc_range * 1.1))) ; tft->line(pt, prev[0], t, data[0], Red) ; tft->line(pt, prev[1], t, data[1], Green) ; tft->line(pt, prev[2], t, data[2], Yellow) ; prev[0] = data[0] ; prev[1] = data[1] ; prev[2] = data[2] ; pt = t ; Thread::wait(20) ; } tft->BusEnable(false) ; } void incPage(void) { page++ ; if (page >= numPage) { page = 0 ; } } void decPage(void) { page-- ; if (page < 0) { page = numPage - 1 ; } } // main() runs in its own thread in the OS // (note the calls to Thread::wait below for delays) int main() { int prevPage = 99 ; bool waitTouch = false ; uint16_t x, y, z ; init_hardware() ; initTFT() ; tty->printf("MAX32630 test (%s)\n", __DATE__) ; tft->cls() ; for(;;) { switch(page) { case 0: if (prevPage != page) { screen1() ; } waitTouch = true ; break ; case 1: if (prevPage != page) { screen2() ; } waitTouch = true ; break ; case 2: if (prevPage != page) { tft->BusEnable(true) ; tft->background(Black) ; tft->foreground(White) ; tft->cls() ; tft->BusEnable(false) ; } screen3() ; waitTouch = false ; break ; case 3: if (prevPage != page) { doMaze() ; waitTouch = true ; } break ; default: page = 0 ; break ; } prevPage = page ; do { tsc->getPoint(&x, &y, &z) ; if ((x != 0)&&(y != 0)) { tty->printf("%d, %d, %d\n", x, y, z) ; if (x < 50) { // left decPage() ; } else if (x > 190) { // right incPage() ; } waitTouch = false ; } } while(waitTouch != false) ; Thread::wait(100) ; } }