Simple test program for FRDM-KL25Z and Adafruit 2.8" TFT display. For the touch sensor, only polling mode is implemented.
Dependencies: MMA8451Q SPI_STMPE610 TSI UniGraphic mbed
This program was to test Adafruit 2.8" TFT with touch with FRDM-KL25Z.
After download the binary to the FRDM-KL25Z and push the reset button ,
a simple welcome message will be displayed.
このプログラムは私が秋月で購入した Adafruit 2.8" TFT
http://akizukidenshi.com/catalog/g/gM-07747/
をFRDM-KL25Z で試すために書いたものです。
ダウンロード後にFRDM-KL25Zのリセットスイッチを押すと
最初の画面が表示されます。
Now using UniGraphic library.
And the problem of remaining previous screen is gone. ;-)
For a little bonus, TS_Eyes and Maze are also included.
UniGraphic ライブラリ版になり、画面にごみが残るバグが治りました。
おまけに TS_Eyes と Maze も追加してあります。 (^ ^)v
If you push and keep the right side of the screen,
the screen will advance to the page2, which is a simple graph sample.
ここで画面の右側をしばらく押していると、次のページに移行します。
このページでは、簡単なグラフを表示する例を描画しています。
And if you do the same again, the program advances to the page3,
which is a data display page of MMA8451Q accelerometer mounted on
the FRDM-KL25Z.
さらに画面の右側をしばらく押していると、次のページに移行します。
ここでは、FRDM-KL25Zに搭載されている MMA8451Q という3軸の
加速度センサの値を表示しています。
And this is the last page of this program, so far, so if you
try to advance page, it will return to the first page.
このページが最後のページになっているので、さらにページ送りをすると
最初のページに戻ります。
Revision 3:34d8706e1614, committed 2015-08-08
- Comitter:
- Rhyme
- Date:
- Sat Aug 08 12:07:56 2015 +0000
- Parent:
- 2:4b85d308f1ef
- Child:
- 4:8c3681dc676f
- Commit message:
- UniGraphic lib version
Changed in this revision
--- a/SPI_STMPE610.cpp Fri Oct 31 01:43:18 2014 +0000
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,223 +0,0 @@
-/* mbed SPI_STMPE610.cpp to test adafruit 2.8" TFT LCD shiled w Touchscreen
- * Copyright (c) 2014 Motoo Tanaka @ Design Methodology Lab
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- * THE SOFTWARE.
- */
- /*
- * Note: Since the interrupt siganl of the shield was not connected
- * to an interrupt pin on my frdm-kl25z, I just used polling mode.
- */
-
-#include "SPI_STMPE610.h"
-
-/* some definitions here */
-#define REG_CHIP_ID 0x00
-#define REG_CHIP_ID_MSB 0x00
-#define REG_CHIP_ID_LSB 0x01
-
-#define REG_ID_VER 0x02
-#define REG_SYS_CTRL1 0x03
-#define REG_SYS_CTRL1_RESET 0x02
-
-#define REG_SYS_CTRL2 0x04
-#define REG_SPI_CFG 0x08
-#define REG_INT_CTRL 0x09
-#define REG_INT_CTRL_POL_HIGH 0x04
-#define REG_INT_CTRL_POL_LOW 0x00
-#define REG_INT_CTRL_EDGE 0x02
-#define REG_INT_CTRL_LEVEL 0x00
-#define REG_INT_CTRL_ENABLE 0x01
-#define REG_INT_CTRL_DISABLE 0x00
-
-#define REG_INT_EN 0x0A
-#define REG_INT_EN_TOUCHDET 0x01
-#define REG_INT_EN_FIFOTH 0x02
-#define REG_INT_EN_FIFOOF 0x04
-#define REG_INT_EN_FIFOFULL 0x08
-#define REG_INT_EN_FIFOEMPTY 0x10
-#define REG_INT_EN_ADC 0x40
-
-#define REG_INT_STA 0x0B
-#define REG_INT_STA_TOUCHDET 0x01
-
-#define REG_GPIO_EN 0x0C
-#define REG_GPIO_INT_STA 0x0D
-#define REG_ADC_INT_EN 0x0E
-#define REG_ADC_INT_STA 0x0F
-#define REG_GPIO_SET_PIN 0x10
-#define REG_GPIO_CLR_PIN 0x11
-#define REG_GPIO_MP_STA 0x12
-#define REG_GPIO_DIR 0x13
-#define REG_GPIO_ED 0x14
-#define REG_GPIO_RE 0x15
-#define REG_GPIO_FE 0x16
-#define REG_GPIO_AF 0x17
-#define REG_ADC_CTRL1 0x20
-#define REG_ADC_CTRL1_12BIT 0x08
-#define REG_ADC_CTRL1_10BIT 0x00
-
-#define REG_ADC_CTRL2 0x21
-#define REG_ADC_CTRL2_1_625MHZ 0x00
-#define REG_ADC_CTRL2_3_25MHZ 0x01
-#define REG_ADC_CTRL2_6_5MHZ 0x02
-
-#define REG_ADC_CAPT 0x22
-#define REG_ADC_DATA_CH0 0x30
-#define REG_ADC_DATA_CH1 0x32
-#define REG_ADC_DATA_CH4 0x38
-#define REG_ADC_DATA_CH5 0x3A
-#define REG_ADC_DATA_CH6 0x3C
-#define REG_ADC_DATA_CH7 0x3E
-#define REG_TSC_CTRL 0x40
-#define REG_TSC_CTRL_EN 0x01
-#define REG_TSC_CTRL_XYZ 0x00
-#define REG_TSC_CTRL_XY 0x02
-
-#define REG_TSC_CFG 0x41
-#define REG_TSC_CFG_1SAMPLE 0x00
-#define REG_TSC_CFG_2SAMPLE 0x40
-#define REG_TSC_CFG_4SAMPLE 0x80
-#define REG_TSC_CFG_8SAMPLE 0xC0
-#define REG_TSC_CFG_DELAY_10US 0x00
-#define REG_TSC_CFG_DELAY_50US 0x08
-#define REG_TSC_CFG_DELAY_100US 0x10
-#define REG_TSC_CFG_DELAY_500US 0x18
-#define REG_TSC_CFG_DELAY_1MS 0x20
-#define REG_TSC_CFG_DELAY_5MS 0x28
-#define REG_TSC_CFG_DELAY_10MS 0x30
-#define REG_TSC_CFG_DELAY_50MS 0x38
-#define REG_TSC_CFG_SETTLE_10US 0x00
-#define REG_TSC_CFG_SETTLE_100US 0x01
-#define REG_TSC_CFG_SETTLE_500US 0x02
-#define REG_TSC_CFG_SETTLE_1MS 0x03
-#define REG_TSC_CFG_SETTLE_5MS 0x04
-#define REG_TSC_CFG_SETTLE_10MS 0x05
-#define REG_TSC_CFG_SETTLE_50MS 0x06
-#define REG_TSC_CFG_SETTLE_100MS 0x07
-
-#define REG_WDW_TR_X 0x42
-#define REG_WDW_TR_Y 0x44
-#define REG_WDW_BL_X 0x46
-#define REG_WDW_BL_Y 0x48
-#define REG_FIFO_TH 0x4A
-#define REG_FIFO_STA 0x4B
-#define REG_FIFO_SIZE 0x4C
-#define REG_TSC_DATA_X 0x4D
-#define REG_TSC_DATA_Y 0x4F
-#define REG_TSC_DATA_Z 0x51
-#define REG_TSC_DATA_XYZ 0x52
-#define REG_TSC_FRACT_XYZ 0x56
-#define REG_TSC_DATA 0x57
-#define REG_TSC_I_DRIVE 0x58
-#define REG_TSC_SHIELD 0x59
-
-SPI_STMPE610::SPI_STMPE610(PinName mosi, PinName miso, PinName sclk, PinName cs) :
- m_spi(mosi, miso, sclk), m_cs(cs) {
- // activate the peripheral
- m_cs = 0 ;
- _mode = 0 ;
- m_spi.frequency(1000000) ;
- m_spi.format(8, 0) ;
- write8(REG_SYS_CTRL1, REG_SYS_CTRL1_RESET) ;
- wait(0.1) ;
- write8(REG_SYS_CTRL2, 0x00) ; // turn on clocks
- write8(REG_TSC_CFG,
- REG_TSC_CFG_4SAMPLE
- | REG_TSC_CFG_DELAY_100US
- | REG_TSC_CFG_SETTLE_1MS ) ;
-
- write8(REG_TSC_CTRL, REG_TSC_CTRL_XYZ | REG_TSC_CTRL_EN) ;
- m_cs = 1 ;
-}
-
-SPI_STMPE610::~SPI_STMPE610() { }
-
-void SPI_STMPE610::readRegs(int addr, uint8_t * data, int len) {
- m_cs = 0 ;
-
- for (int i = 0 ; i < len ; i++ ) {
- m_spi.write((addr+i)|0x80) ; // spacify address to read
- data[i] = m_spi.write((addr+i)|0x80) ;
- }
- m_spi.write(0x00) ; // to terminate read mode
- m_cs = 1 ;
-}
-
-void SPI_STMPE610::writeRegs(uint8_t * data, int len) {
- m_cs = 0 ;
- for (int i = 0 ; i < len ; i++ ) {
- m_spi.write(data[i]) ;
- }
- m_cs = 1 ;
-}
-
-void SPI_STMPE610::write8(int addr, uint8_t data8)
-{
- uint8_t data[2] ;
- data[0] = addr ;
- data[1] = data8 ;
- writeRegs(data, 2) ;
-}
-
-uint8_t SPI_STMPE610::read8(int addr)
-{
- uint8_t data[1] ;
- readRegs(addr, data, 1) ;
- return( data[0] ) ;
-}
-
-void SPI_STMPE610::write16(int addr, uint16_t data16)
-{
- uint8_t data[3] ;
- data[0] = addr ;
- data[1] = (data16 >> 8) & 0xFF ;
- data[2] = data16 & 0xFF ;
- writeRegs(data, 3) ;
-}
-
-uint16_t SPI_STMPE610::read16(int addr)
-{
- uint8_t data[2] ;
- uint16_t value = 0 ;
- readRegs(addr, data, 2) ;
- value = (data[0] << 8) | data[1] ;
- return( value ) ;
-}
-
-int SPI_STMPE610::getRAWPoint(uint16_t *x, uint16_t *y, uint16_t *z)
-{
- uint8_t data[8], touched = 0 ;
- data[0] = REG_TSC_CTRL ;
- data[1] = REG_TSC_CTRL_EN ;
- writeRegs(data, 2) ;
- wait(0.01) ;
-
- readRegs(REG_TSC_CTRL, data, 1) ;
- touched = data[0] & 0x80 ;
- if (touched) { //Touch Detected
- readRegs(REG_TSC_DATA_X, data,5) ;
- *x = (data[0] << 8) | data[1] ;
- *y = (data[2] << 8) | data[3] ;
- *z = data[4] ;
- } else {
- *x = 0 ;
- *y = 0 ;
- *z = 0 ;
- }
-
- data[0] = 0x4B ;
- data[1] = 0x01 ;
- writeRegs(data, 2) ; // clear FIFO
-
- data[0] = REG_TSC_CTRL ;
- data[1] = 0x00 ; // disable TSC
- writeRegs(data, 2) ;
-
- return( touched & (*x || *y || *z)) ;
-}
--- a/SPI_STMPE610.h Fri Oct 31 01:43:18 2014 +0000
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,58 +0,0 @@
-/* mbed SPI_STMPE610.h to test adafruit 2.8" TFT LCD shiled w Touchscreen
- * Copyright (c) 2014 Motoo Tanaka @ Design Methodology Lab
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- * THE SOFTWARE.
- */
-
-#ifndef SPI_STMPE610_H
-#define SPI_STMPE610_H
-
-#include "mbed.h"
-
-/**
- * STMPE610 Touch Sensor
- *
- *
- */
-
-class SPI_STMPE610
-{
-public:
- /**
- * STMPE610 constructor
- *
- * @param mosi SPI_MOSI pin
- * @param miso SPI_MISO pin
- * @param sclk SPI_CLK pin
- * @param cs SPI_CS pin
- */
- SPI_STMPE610(PinName mosi, PinName miso, PinName sclk, PinName cs) ;
-
- ~SPI_STMPE610() ;
-
- /*
- * some member functions here (yet to be written)
- */
-
- SPI m_spi;
- DigitalOut m_cs ;
- int _mode ;
-
- void readRegs(int addr, uint8_t *data, int len) ;
- void writeRegs(uint8_t *data, int len) ;
- uint8_t read8(int addr) ;
- void write8(int addr, uint8_t data) ;
- uint16_t read16(int addr) ;
- void write16(int addr, uint16_t data) ;
-
- int getRAWPoint(uint16_t *x, uint16_t *y, uint16_t *z) ;
-
- private:
-} ;
-#endif /* SPI_STMPE610_H */
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SPI_STMPE610.lib Sat Aug 08 12:07:56 2015 +0000 @@ -0,0 +1,1 @@ +SPI_STMPE610#50965da0e845
--- a/SPI_TFT_ILI9341.lib Fri Oct 31 01:43:18 2014 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -http://developer.mbed.org/users/Rhyme/code/SPI_TFT_ILI9341/#61fa73a34834
--- a/TFT_fonts.lib Fri Oct 31 01:43:18 2014 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -http://mbed.org/users/dreschpe/code/TFT_fonts/#76774250fcec
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/TSI.lib Sat Aug 08 12:07:56 2015 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/emilmont/code/TSI/#507b1f67804b
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/UniGraphic.lib Sat Aug 08 12:07:56 2015 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/teams/GraphicsDisplay/code/UniGraphic/#1bdfb971b2c1
--- a/main.cpp Fri Oct 31 01:43:18 2014 +0000
+++ b/main.cpp Sat Aug 08 12:07:56 2015 +0000
@@ -21,13 +21,14 @@
#include "mbed.h"
#include "MMA8451Q.h"
+#include "TSISensor.h"
#include <math.h>
-#include "SPI_TFT_ILI9341.h"
+#include "ILI9341.h"
#include "SPI_STMPE610.h"
#include "Arial12x12.h"
#include "Arial24x23.h"
#include "Arial28x28.h"
-#include "font_big.h"
+#include "Arial43x48_numb.h"
#define PIN_XP PTB3
@@ -43,36 +44,46 @@
#define PIN_CS_SD PTA4
#define PIN_CS_TSC PTA13
#define PIN_TSC_INTR PTC9
+#define PIN_RESET_TFT PTB10
#define MMA8451_I2C_ADDRESS (0x1d<<1)
MMA8451Q *acc = 0 ;
+TSISensor tsi;
-// SPI_TFT_ILI9341(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName reset, PinName dc, const char* name ="TFT");
-SPI_TFT_ILI9341 TFT(PIN_MOSI, PIN_MISO, PIN_SCLK, PIN_CS_TFT, PIN_BL_TFT, PIN_DC_TFT) ;
+DigitalOut backlight(PTA12) ;
+ILI9341 TFT(SPI_8, 10000000,
+ PIN_MOSI, PIN_MISO, PIN_SCLK,
+ PIN_CS_TFT, PIN_RESET_TFT, PIN_DC_TFT, "Adafruit2.8") ;
SPI_STMPE610 TSC(PIN_MOSI, PIN_MISO, PIN_SCLK, PIN_CS_TSC) ;
-DigitalOut backlight(PTA12) ;
DigitalIn pinD7(PTC9) ;
int page = 0 ;
-int numPage = 3 ;
+int numPage = 5 ;
+
+extern void doMaze(void) ;
void initTFT(void)
{
//Configure the display driver
+ TFT.BusEnable(true) ;
+ TFT.FastWindow(true) ;
TFT.background(Black);
TFT.foreground(White);
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) ;
@@ -85,19 +96,20 @@
TFT.foreground(Black);
TFT.set_font((unsigned char*) Arial12x12);
TFT.foreground(Blue) ;
- TFT.locate(10, 180) ;
+ TFT.locate(30, 180) ;
TFT.printf("This program is running on") ;
- TFT.locate(10, 200) ;
+ TFT.locate(30, 200) ;
TFT.printf("freescale FRDM-KL25Z with") ;
- TFT.locate(10, 220) ;
- TFT.printf("a program developed on mbed") ;
+ TFT.locate(30, 220) ;
+ TFT.printf("a program developed in mbed") ;
TFT.foreground(Green) ;
- TFT.locate(10, 260) ;
+ TFT.locate(30, 260) ;
TFT.printf("To advance demo page, touch") ;
- TFT.locate(10, 280) ;
+ TFT.locate(30, 280) ;
TFT.printf("and hold right side of screen") ;
- TFT.locate(10, 300) ;
+ TFT.locate(30, 300) ;
TFT.printf("until the next screen starts") ;
+ TFT.BusEnable(false) ;
backlight = 1 ;
}
@@ -106,10 +118,13 @@
//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");
@@ -150,6 +165,7 @@
TFT.printf("simple graphics drawing") ;
TFT.locate(20, 290) ;
TFT.printf("capability is provided") ;
+ TFT.BusEnable(false) ;
backlight = 1 ;
}
@@ -179,7 +195,8 @@
unsigned short paneX[2] = {20, 235} ;
unsigned short paneH = 81 ;
- backlight = 0 ;
+ backlight = 1 ;
+ TFT.BusEnable(true) ;
TFT.background(Black) ;
TFT.foreground(White) ;
// TFT.cls() ;
@@ -233,6 +250,65 @@
pt = t ;
wait(0.01) ;
}
+ TFT.BusEnable(false) ;
+}
+
+void screen4(void)
+{
+ int dx, px ;
+ float delta = 0.0 ;
+ dx = 0 ;
+ px = 0 ;
+ 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.foreground(Blue) ;
+ TFT.locate(60, 10) ;
+ TFT.printf("<< TSI demo >>") ;
+ TFT.locate(30, 280) ;
+ TFT.printf("Use FRDM touch slider") ;
+ TFT.locate(30, 300) ;
+ TFT.printf("Touch right edge to end") ;
+
+ TFT.fillcircle(120, 160, 100, Green) ;
+ TFT.fillcircle(60, 160, 50, Black) ;
+ TFT.fillcircle(60, 160, 45, White) ;
+ TFT.fillcircle(180, 160, 50, Black) ;
+ TFT.fillcircle(180, 160, 45, White) ;
+ TFT.fillcircle(60, 160, 5, Black) ;
+ TFT.fillcircle(180, 160, 5, Black) ;
+ backlight = 1 ;
+
+ while(dx < 38) {
+ delta = (80.0 * (tsi.readPercentage()-0.5)) ;
+ dx = (int)(delta + 0.5) ;
+ TFT.fillcircle(60+px, 160, 5, White) ;
+ TFT.fillcircle(180+px, 160, 5, White) ;
+ TFT.fillcircle(60+dx, 160, 5, Black) ;
+ TFT.fillcircle(180+dx, 160, 5, Black) ;
+ px = dx ;
+ wait(0.1) ;
+ }
+ TFT.fillcircle(60+px, 160, 5, White) ;
+ TFT.fillcircle(180+px, 160, 5, White) ;
+ TFT.line(15, 160, 105, 160, Black) ;
+ TFT.line(135, 160, 225, 160, Black) ;
+ TFT.foreground(Yellow) ;
+ TFT.locate(30, 300) ;
+// TFT.printf("Use FRDM touch slider") ;
+ TFT.printf(" Wake Up! ") ;
+ TFT.locate(5, 300) ;
+ TFT.printf("<< Prev") ;
+ TFT.locate(180, 300) ;
+ TFT.printf("Next >>") ;
+ TFT.BusEnable(false) ;
}
void incPage(void)
@@ -254,13 +330,14 @@
int main()
{
uint16_t x, y, z ;
- int prevPage = 0 ;
+ int prevPage = 99 ;
+ bool waitTouch = false ;
acc = new MMA8451Q(PTE25, PTE24, MMA8451_I2C_ADDRESS) ;
initTFT() ;
- screen1() ;
+ // screen0() ;
printf("Program Started!\n\r") ;
@@ -271,36 +348,54 @@
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() ;
- wait(2) ;
+ waitTouch = false ;
+ break ;
+ case 3:
+// if (prevPage != page) {
+ screen4() ;
+ waitTouch = true ;
+// }
+ break ;
+ case 4:
+// if (prevPage != page) {
+ doMaze() ;
+ waitTouch = true ;
+// }
break ;
default:
page = 0 ;
break ;
}
prevPage = page ;
- printf("Screen Printed\n\r") ;
- TSC.getRAWPoint(&x, &y, &z) ;
- if ((x != 0)||(y != 0) || (z != 0)) {
- if (x < 1000) { // left
- decPage() ;
- } else if (x > 3000) { // right
- incPage() ;
+ do {
+ TSC.getRAWPoint(&x, &y, &z) ;
+ if ((x != 0)||(y != 0) || (z != 0)) {
+ if (x < 1000) { // left
+ decPage() ;
+ } else if (x > 3000) { // right
+ incPage() ;
+ }
+ waitTouch = false ;
}
- }
+ } while(waitTouch != false) ;
// wait(1) ;
}
}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/maze.cpp Sat Aug 08 12:07:56 2015 +0000
@@ -0,0 +1,255 @@
+/** maze_vt100 a simple maze using UniGraphic and MMA8451Q libraries
+ */
+#include "mbed.h"
+#include "MMA8451Q.h"
+#include <ILI9341.h>
+#include "Arial12x12.h"
+#include "Arial24x23.h"
+#include "Arial28x28.h"
+#include "Arial43x48_numb.h"
+#include "maze.h"
+
+#define CELL_W 10
+#define CELL_H 10
+#define CELL_S 5
+#define CELL_R 4
+#define LEFT_OFFSET 20
+#define TOP_OFFSET 20
+
+#define DIR_STAY 0
+#define DIR_UP 1
+#define DIR_DOWN 2
+#define DIR_RIGHT 3
+#define DIR_LEFT 4
+
+#define MMA8451_I2C_ADDRESS (0x1D<<1)
+
+typedef struct _pos {
+ int x ;
+ int y ;
+} pos_type ;
+
+extern MMA8451Q *acc ;
+extern ILI9341 TFT ;
+extern DigitalOut backlight ;
+float threshold = 0.2 ;
+
+/** Check if two pos_type values are same
+ * @param pos_type a
+ * @param pos_type b
+ * @returns if a and b are same position
+ */
+bool isSame(pos_type a, pos_type b)
+{
+ return((a.x == b.x)&&(a.y == b.y)) ;
+}
+
+/** Draw the maze defined in the "maze.h"
+ * @param pos_type *current actually the start point
+ * @param pos_type *goal the position of the goal
+ * @note those params are actually returned by this function
+ */
+void drawTFTMaze(pos_type *current, pos_type *goal)
+{
+ int x, y, i, j ;
+ TFT.BusEnable(true) ;
+
+ backlight = 0 ;
+ TFT.foreground(White) ;
+ TFT.background(Black) ;
+ TFT.cls() ;
+ for (j = 0 ; j < MAZE_H ; j++ ) {
+ for (i = 0 ; i < MAZE_W ; i++ ) {
+ x = i * CELL_W + LEFT_OFFSET ;
+ y = j * CELL_H + TOP_OFFSET ;
+ switch(maze[j][i]) {
+ case 0: // path
+ TFT.fillrect(x, y, x+CELL_W-1, y+CELL_H-1, Black) ;
+ break ;
+ case 1: // wall
+ TFT.fillrect(x, y, x+CELL_W-1, y+CELL_H-1, DarkGrey) ;
+ break ;
+ case 2: // Start point
+ TFT.fillcircle(x+CELL_S, y+CELL_S, CELL_R, Green) ;
+ current->x = i ;
+ current->y = j ;
+ break ;
+ case 3: // Goal
+ TFT.fillcircle(x+CELL_S, y+CELL_S, CELL_R, Red) ;
+ goal->x = i ;
+ goal->y = j ;
+ break ;
+ default: // should not be here
+ break ;
+ }
+ }
+ }
+ backlight = 1 ;
+ TFT.BusEnable(false) ;
+}
+
+/** Filter out too little move
+ * @param float in returned value from the acc
+ * @returns float result filtered value of in
+ */
+float filterVal(float in)
+{
+ float result = 0.0 ;
+ if ((-threshold > in)||(in > threshold)) {
+ result = in ;
+ }
+ return( result ) ;
+}
+
+/** Decide which direction to go
+ * @param float res[] acc value of x, y
+ * @returns int direction to move
+ */
+int getDirection(float res[])
+{
+ float dx, dy ;
+ int direction = DIR_STAY ;
+ dx = filterVal(res[0]) ;
+ dy = filterVal(res[1]) ;
+
+ if ((dx*dx) > (dy*dy)) { // holizontal move
+ if (dx > 0.0) {
+ direction = DIR_DOWN ;
+ } else if (dx < 0.0) {
+ direction = DIR_UP ;
+ }
+ } else { // vertical move
+ if (dy > 0.0) {
+ direction = DIR_RIGHT ;
+ } else if (dy < 0.0) {
+ direction = DIR_LEFT ;
+ }
+ }
+ return(direction) ;
+}
+
+/** Get next positon to move to
+ * @param pos_type current where we are now
+ * @param int direction which way we'd like to move
+ * @returns the candidate positon for the next move
+ */
+pos_type getNext(pos_type current, int direction)
+{
+ pos_type next = current ;
+ switch(direction) {
+ case DIR_STAY:
+ break ;
+ case DIR_UP:
+ if (next.y > 0) {
+ next.y-- ;
+ }
+ break ;
+ case DIR_DOWN:
+ if (next.y < (MAZE_H - 1)) {
+ next.y++ ;
+ }
+ break ;
+ case DIR_RIGHT:
+ if (next.x < (MAZE_W - 1)) {
+ next.x++ ;
+ }
+ break ;
+ case DIR_LEFT:
+ if (next.x > 0) {
+ next.x-- ;
+ }
+ break ;
+ default:
+ break ;
+ }
+ return( next ) ;
+}
+
+/** Notice of the goal
+ */
+void showGoal(void)
+{
+ int x0, y0, x1, y1 ;
+ x0 = ((MAZE_W/2)-4) * CELL_W + LEFT_OFFSET ;
+ y0 = ((MAZE_H/2)-1) * CELL_H + TOP_OFFSET ;
+ x1 = ((MAZE_W/2)+4) * CELL_W + LEFT_OFFSET ;
+ y1 = ((MAZE_H/2)+1) * CELL_H + TOP_OFFSET ;
+
+ TFT.BusEnable(true) ;
+// TFT.set_font((unsigned char*) Arial12x12);
+ TFT.set_font((unsigned char*) Terminal6x8) ;
+ TFT.fillrect(x0, y0, x1, y1, Blue) ;
+ TFT.rect(x0,y0,x1,y1, Yellow) ;
+ TFT.foreground(Red) ;
+ TFT.background(Blue) ;
+ x0 = ((MAZE_W/2) - 2) * CELL_W + LEFT_OFFSET ;
+ y0 = (MAZE_H/2) * CELL_H - 3 + TOP_OFFSET ;
+ TFT.locate(x0, y0) ;
+ TFT.printf("G O A L") ;
+ TFT.foreground(Yellow) ;
+ TFT.background(Black) ;
+ TFT.locate(30, 300) ;
+// TFT.printf("Use FRDM touch slider") ;
+ TFT.printf(" Retry! ") ;
+ TFT.locate(5, 300) ;
+ TFT.printf("<< Prev") ;
+ TFT.locate(180, 300) ;
+ TFT.printf("Next >>") ;
+ TFT.BusEnable(false) ;
+}
+
+/** Check if we can move to the next position
+ * @param pos_type next the position we'd like to move next
+ * @returns if the position is empty (movable)
+ */
+bool checkMove(pos_type next)
+{
+ bool result = false ;
+
+ switch(maze[next.y][next.x]) {
+ case POS_PATH:
+ case POS_GOAL:
+ result = true ;
+ break ;
+ case POS_START:
+ case POS_WALL:
+ default:
+ result = false ;
+ break ;
+ }
+ return( result ) ;
+}
+
+/** main a simple maze program
+ */
+void doMaze(void)
+{
+ float res[3] ;
+ pos_type current, next, goal ;
+ int direction = DIR_STAY ;
+ int x, y ;
+
+ drawTFTMaze(¤t, &goal) ;
+
+ for (;;) {
+ acc->getAccAllAxis(res) ;
+ direction = getDirection(res) ;
+ next = getNext(current, direction) ;
+ if ((!isSame(current, next)) && checkMove(next)) {
+ TFT.BusEnable(true) ;
+ x = current.x * CELL_W + LEFT_OFFSET ;
+ y = current.y * CELL_H + TOP_OFFSET ;
+ TFT.fillrect(x, y, x+CELL_W-1, y+CELL_H-1, Black) ;
+ x = next.x * CELL_W + LEFT_OFFSET ;
+ y = next.y * CELL_H + TOP_OFFSET ;
+ TFT.fillcircle(x+CELL_S, y+CELL_S, CELL_R, Green) ;
+ TFT.BusEnable(false) ;
+ current = next ;
+ if (isSame(next, goal)) {
+ break ; // goal in!
+ }
+ }
+ wait(0.1) ;
+ }
+ showGoal() ;
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/maze.h Sat Aug 08 12:07:56 2015 +0000
@@ -0,0 +1,36 @@
+#ifndef _MAZE_H_
+#define _MAZE_H_ defined
+
+#define MAZE_W 20
+#define MAZE_H 20
+
+#define POS_PATH 0
+#define POS_WALL 1
+#define POS_START 2
+#define POS_GOAL 3
+
+uint8_t maze[MAZE_H][MAZE_W] = {
+ {1,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
+ {1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
+ {1,0,1,0,1,0,1,1,1,1,0,1,1,1,1,1,1,1,0,1},
+ {1,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,1,1,0,1},
+ {1,1,1,1,1,0,1,0,1,1,1,1,1,1,1,0,1,1,0,1},
+ {1,0,0,0,1,0,1,0,1,0,0,0,0,0,1,0,1,1,0,1},
+ {1,0,1,1,1,0,1,0,1,1,0,1,0,1,1,0,1,1,0,1},
+ {1,0,1,0,0,0,1,0,1,1,0,1,0,1,0,0,1,1,0,1},
+ {1,0,1,0,1,1,1,0,0,1,0,1,0,1,0,1,1,1,0,1},
+ {1,0,1,0,1,1,1,1,1,1,0,1,0,1,0,0,0,1,0,1},
+ {1,0,0,0,0,0,0,0,0,0,0,1,0,1,1,1,0,1,0,1},
+ {1,0,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,0,1},
+ {1,0,1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1},
+ {1,0,1,0,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1},
+ {1,0,1,0,1,0,0,0,1,0,0,0,0,0,0,1,1,1,1,1},
+ {1,0,1,0,1,1,1,0,1,0,1,1,1,1,1,1,1,1,1,1},
+ {1,0,1,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,1},
+ {1,0,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,0,1},
+ {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1},
+ {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,3,1}
+} ;
+
+extern void doMaze(void) ;
+#endif // _MAZE_H_
\ No newline at end of file