![](/media/cache/group/hexiweare_logo_main_black.jpg.50x50_q85.jpg)
Simple catch the dot game
Dependencies: FXOS8700 Hexi_KW40Z NeatGUI
Fork of Hexi_Bubble_Game by
Move your dot exactly over the middle of the random stationary dot (within the time limit) and the led will light and the Hexiwear will vibrate.
main.cpp@2:085d270789f7, 2016-09-23 (annotated)
- Committer:
- Capper
- Date:
- Fri Sep 23 19:01:10 2016 +0000
- Revision:
- 2:085d270789f7
- Parent:
- 1:9bbb606da2d0
HexiGame V1.0.0 23Sep2016
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Capper | 2:085d270789f7 | 1 | //HexiGame.cpp |
Capper | 2:085d270789f7 | 2 | //R. Scott Coppersmith |
Capper | 2:085d270789f7 | 3 | //Build 23 September 2016 |
Capper | 0:d7a9c9f2bcc1 | 4 | #include "mbed.h" |
Capper | 0:d7a9c9f2bcc1 | 5 | #include <SSD1351_SPI.h> |
Capper | 2:085d270789f7 | 6 | #include <HexiGame.h> |
Capper | 0:d7a9c9f2bcc1 | 7 | #include "FXOS8700.h" |
Capper | 2:085d270789f7 | 8 | #include "Hexi_KW40Z.h" |
Capper | 2:085d270789f7 | 9 | |
Capper | 2:085d270789f7 | 10 | #define LED_ON 0 |
Capper | 2:085d270789f7 | 11 | #define LED_OFF 1 |
Capper | 2:085d270789f7 | 12 | |
Capper | 2:085d270789f7 | 13 | void StartHaptic(void); |
Capper | 2:085d270789f7 | 14 | void StopHaptic(void const *n); |
Capper | 2:085d270789f7 | 15 | AnalogIn analog(PTB2); |
Capper | 2:085d270789f7 | 16 | Serial pc(USBTX, USBRX); |
Capper | 2:085d270789f7 | 17 | unsigned int m_z = 22222; |
Capper | 2:085d270789f7 | 18 | unsigned int m_w = 98764; |
Capper | 2:085d270789f7 | 19 | unsigned int rgen (void) |
Capper | 2:085d270789f7 | 20 | { |
Capper | 2:085d270789f7 | 21 | |
Capper | 2:085d270789f7 | 22 | m_z = 36969 * (m_z & 65535) + (m_z >>16); |
Capper | 2:085d270789f7 | 23 | m_w = 18000 * (m_w & 65535) + (m_w >>16); |
Capper | 2:085d270789f7 | 24 | return ((m_z <<16) + m_w); |
Capper | 2:085d270789f7 | 25 | } |
Capper | 0:d7a9c9f2bcc1 | 26 | |
Capper | 0:d7a9c9f2bcc1 | 27 | //SSD1351_SPI ( mosi, miso, sclk, cs, dc ) ; |
Capper | 0:d7a9c9f2bcc1 | 28 | SSD1351_SPI OLED96x96(PTB22,PTB23,PTB21,PTB20,PTD15); |
Capper | 0:d7a9c9f2bcc1 | 29 | // Pin connections for Hexiwear |
Capper | 0:d7a9c9f2bcc1 | 30 | FXOS8700 accel(PTC11, PTC10); |
Capper | 0:d7a9c9f2bcc1 | 31 | // Storage for the data from the sensor |
Capper | 0:d7a9c9f2bcc1 | 32 | float accel_data[3]; float accel_rms=0.0; |
Capper | 0:d7a9c9f2bcc1 | 33 | DigitalOut led1(LED1); |
Capper | 0:d7a9c9f2bcc1 | 34 | DigitalOut led2(LED2); |
Capper | 0:d7a9c9f2bcc1 | 35 | DigitalOut led3(LED3); |
Capper | 0:d7a9c9f2bcc1 | 36 | DigitalOut BOOSTEN(PTC13); //oled power enable |
Capper | 2:085d270789f7 | 37 | DigitalOut haptic(PTB9); |
Capper | 0:d7a9c9f2bcc1 | 38 | int count = 0; |
Capper | 1:9bbb606da2d0 | 39 | int count2 = 0; |
Capper | 2:085d270789f7 | 40 | int count3 = 0; |
Capper | 0:d7a9c9f2bcc1 | 41 | extern const uint8_t Main_screen_bmp[ 36864 ]; |
Capper | 0:d7a9c9f2bcc1 | 42 | int argb = 0xff000000; |
Capper | 0:d7a9c9f2bcc1 | 43 | int rgbd = 0x00000000; |
Capper | 0:d7a9c9f2bcc1 | 44 | int xposg = 0; |
Capper | 0:d7a9c9f2bcc1 | 45 | int yposg = 0; |
Capper | 2:085d270789f7 | 46 | int randx = 0; |
Capper | 2:085d270789f7 | 47 | int randy = 0; |
Capper | 0:d7a9c9f2bcc1 | 48 | int main() { |
Capper | 0:d7a9c9f2bcc1 | 49 | led1 = 1; |
Capper | 0:d7a9c9f2bcc1 | 50 | led2 = 1; |
Capper | 0:d7a9c9f2bcc1 | 51 | led3 = 1; |
Capper | 2:085d270789f7 | 52 | pc.printf("HexiGame \r\n"); |
Capper | 0:d7a9c9f2bcc1 | 53 | // Configure Accelerometer FXOS8700 |
Capper | 0:d7a9c9f2bcc1 | 54 | accel.accel_config(); |
Capper | 0:d7a9c9f2bcc1 | 55 | BOOSTEN = 1; |
Capper | 0:d7a9c9f2bcc1 | 56 | OLED96x96.open(); |
Capper | 0:d7a9c9f2bcc1 | 57 | OLED96x96.state(Display::DISPLAY_ON); |
Capper | 0:d7a9c9f2bcc1 | 58 | OLED96x96.fillRect(16,0,111,96,0xff000000);//alpha, BGR |
Capper | 0:d7a9c9f2bcc1 | 59 | for(int xpos=0;xpos<97;xpos++) |
Capper | 0:d7a9c9f2bcc1 | 60 | { |
Capper | 0:d7a9c9f2bcc1 | 61 | for(int ypos=0;ypos<96;ypos++) |
Capper | 0:d7a9c9f2bcc1 | 62 | { |
Capper | 0:d7a9c9f2bcc1 | 63 | rgbd = ((Main_screen_bmp[count+3]<<24)|(Main_screen_bmp[count]<<16)|(Main_screen_bmp[count+1]<<8)|(Main_screen_bmp[count+2])); |
Capper | 0:d7a9c9f2bcc1 | 64 | OLED96x96.drawPixel(ypos+16,xpos,rgbd); |
Capper | 0:d7a9c9f2bcc1 | 65 | count=count+4; |
Capper | 0:d7a9c9f2bcc1 | 66 | } |
Capper | 0:d7a9c9f2bcc1 | 67 | } |
Capper | 2:085d270789f7 | 68 | Thread::wait(6000); |
Capper | 0:d7a9c9f2bcc1 | 69 | while(1) |
Capper | 0:d7a9c9f2bcc1 | 70 | { |
Capper | 0:d7a9c9f2bcc1 | 71 | accel.acquire_accel_data_g(accel_data); |
Capper | 2:085d270789f7 | 72 | xposg=(accel_data[1]*(-50.0)); |
Capper | 2:085d270789f7 | 73 | if (xposg > 36) |
Capper | 2:085d270789f7 | 74 | xposg = 36; |
Capper | 2:085d270789f7 | 75 | if (xposg < -36) |
Capper | 2:085d270789f7 | 76 | xposg = -36; |
Capper | 2:085d270789f7 | 77 | yposg=accel_data[0]*50.0; |
Capper | 2:085d270789f7 | 78 | if (yposg > 36) |
Capper | 2:085d270789f7 | 79 | yposg = 36; |
Capper | 2:085d270789f7 | 80 | if (yposg < -36) |
Capper | 2:085d270789f7 | 81 | yposg = -36; |
Capper | 2:085d270789f7 | 82 | OLED96x96.drawCircle(61-xposg,47+yposg,5,0xff00ff00); |
Capper | 0:d7a9c9f2bcc1 | 83 | OLED96x96.drawCircle(61-xposg,47+yposg,4,0xff00ff00); |
Capper | 0:d7a9c9f2bcc1 | 84 | OLED96x96.drawCircle(61-xposg,47+yposg,3,0xff00ff00); |
Capper | 2:085d270789f7 | 85 | OLED96x96.drawCircle(61-xposg,47+yposg,2,0xff00ff00); |
Capper | 2:085d270789f7 | 86 | OLED96x96.drawCircle(61-xposg,47+yposg,1,0xff00ff00); |
Capper | 2:085d270789f7 | 87 | Thread::wait(10); |
Capper | 2:085d270789f7 | 88 | OLED96x96.drawCircle(61-xposg,47+yposg,5,0xff000000); |
Capper | 0:d7a9c9f2bcc1 | 89 | OLED96x96.drawCircle(61-xposg,47+yposg,4,0xff000000); |
Capper | 0:d7a9c9f2bcc1 | 90 | OLED96x96.drawCircle(61-xposg,47+yposg,3,0xff000000); |
Capper | 0:d7a9c9f2bcc1 | 91 | OLED96x96.drawCircle(61-xposg,47+yposg,2,0xff000000); |
Capper | 2:085d270789f7 | 92 | OLED96x96.drawCircle(61-xposg,47+yposg,1,0xff000000); |
Capper | 2:085d270789f7 | 93 | if ((61-xposg == 40+randx) and (47+yposg == 40+randy)) |
Capper | 2:085d270789f7 | 94 | { |
Capper | 2:085d270789f7 | 95 | pc.printf("Hit!\r\n"); |
Capper | 2:085d270789f7 | 96 | led1 = LED_ON; |
Capper | 2:085d270789f7 | 97 | haptic = 1; |
Capper | 2:085d270789f7 | 98 | count3 = 0; |
Capper | 2:085d270789f7 | 99 | } |
Capper | 2:085d270789f7 | 100 | count3++; |
Capper | 1:9bbb606da2d0 | 101 | count2++; |
Capper | 2:085d270789f7 | 102 | if ((count3 > 20)and (haptic == 1)) |
Capper | 2:085d270789f7 | 103 | { |
Capper | 2:085d270789f7 | 104 | count2 = 101; |
Capper | 2:085d270789f7 | 105 | haptic = 0; |
Capper | 2:085d270789f7 | 106 | } |
Capper | 1:9bbb606da2d0 | 107 | if (count2 > 100) |
Capper | 1:9bbb606da2d0 | 108 | { //blank display to avoid image burn |
Capper | 2:085d270789f7 | 109 | haptic = 0; |
Capper | 2:085d270789f7 | 110 | count3 = 0; |
Capper | 2:085d270789f7 | 111 | led1 = LED_OFF; |
Capper | 1:9bbb606da2d0 | 112 | OLED96x96.fillRect(16,0,111,96,0xff000000);//alpha, BGR |
Capper | 1:9bbb606da2d0 | 113 | count2 = 0; |
Capper | 2:085d270789f7 | 114 | randx = rgen()%40; |
Capper | 2:085d270789f7 | 115 | pc.printf("randx = %d\r\n",randx); |
Capper | 2:085d270789f7 | 116 | randy = rgen()%40; |
Capper | 2:085d270789f7 | 117 | pc.printf("randy = %d\r\n",randy); |
Capper | 2:085d270789f7 | 118 | OLED96x96.drawCircle(40+randx,40+randy,3,0xffff00ff); |
Capper | 2:085d270789f7 | 119 | OLED96x96.drawCircle(40+randx,40+randy,2,0xffff00ff); |
Capper | 2:085d270789f7 | 120 | OLED96x96.drawCircle(40+randx,40+randy,1,0xff00ff00); |
Capper | 1:9bbb606da2d0 | 121 | } |
Capper | 1:9bbb606da2d0 | 122 | //BOOSTEN = 0; //turn off display after about 1 minute to avoid image burn |
Capper | 0:d7a9c9f2bcc1 | 123 | } |
Capper | 0:d7a9c9f2bcc1 | 124 | |
Capper | 0:d7a9c9f2bcc1 | 125 | } |
Capper | 0:d7a9c9f2bcc1 | 126 |