R. Scott Coppersmith
Published © MIT

HexiGame

Catch the dot and Win Win Win!https://developer.mbed.org/teams/Hexiwear/code/HexiGame/

IntermediateFull instructions provided2 hours141
HexiGame

Things used in this project

Hardware components

Hexiwear
NXP Hexiwear
×1

Story

Read more

Code

HexiGame on mbed

C/C++
//HexiGame.cpp
//R. Scott Coppersmith
//Build 23 September 2016
#include "mbed.h"
#include <SSD1351_SPI.h>
#include <HexiGame.h>
#include "FXOS8700.h"
#include "Hexi_KW40Z.h"
 
#define LED_ON      0
#define LED_OFF     1
   
void StartHaptic(void);
void StopHaptic(void const *n);
AnalogIn analog(PTB2);
Serial pc(USBTX, USBRX);
unsigned int m_z = 22222;
unsigned int m_w = 98764;
unsigned int rgen (void)
{

    m_z = 36969 * (m_z & 65535) + (m_z >>16);
    m_w = 18000 * (m_w & 65535) + (m_w >>16);
    return ((m_z <<16) + m_w);
}

//SSD1351_SPI ( mosi, miso, sclk, cs, dc ) ;  
SSD1351_SPI OLED96x96(PTB22,PTB23,PTB21,PTB20,PTD15); 
// Pin connections for Hexiwear
FXOS8700 accel(PTC11, PTC10);
// Storage for the data from the sensor
float accel_data[3]; float accel_rms=0.0;
DigitalOut led1(LED1);
DigitalOut led2(LED2);
DigitalOut led3(LED3);
DigitalOut BOOSTEN(PTC13);  //oled power enable
DigitalOut haptic(PTB9);
int count = 0;
int count2 = 0;
int count3 = 0;
extern const uint8_t  Main_screen_bmp[ 36864 ];
int argb = 0xff000000; 
int rgbd = 0x00000000;
int xposg = 0;
int yposg = 0;
int randx = 0;
int randy = 0;
int main() {
    led1 = 1;
    led2 = 1;
    led3 = 1;
    pc.printf("HexiGame \r\n");
    // Configure Accelerometer FXOS8700
    accel.accel_config();
    BOOSTEN = 1;
    OLED96x96.open();
    OLED96x96.state(Display::DISPLAY_ON);
    OLED96x96.fillRect(16,0,111,96,0xff000000);//alpha, BGR
    for(int xpos=0;xpos<97;xpos++)
    {
        for(int ypos=0;ypos<96;ypos++)
        {
            rgbd = ((Main_screen_bmp[count+3]<<24)|(Main_screen_bmp[count]<<16)|(Main_screen_bmp[count+1]<<8)|(Main_screen_bmp[count+2]));
            OLED96x96.drawPixel(ypos+16,xpos,rgbd);
            count=count+4;
        }
    } 
    Thread::wait(6000);
    while(1)
    {    
        accel.acquire_accel_data_g(accel_data);
        xposg=(accel_data[1]*(-50.0));
        if (xposg > 36)
            xposg = 36;
        if (xposg < -36)
            xposg = -36;
        yposg=accel_data[0]*50.0;
        if (yposg > 36)
            yposg = 36;
        if (yposg < -36)
            yposg = -36;
        OLED96x96.drawCircle(61-xposg,47+yposg,5,0xff00ff00);
        OLED96x96.drawCircle(61-xposg,47+yposg,4,0xff00ff00);
        OLED96x96.drawCircle(61-xposg,47+yposg,3,0xff00ff00);
        OLED96x96.drawCircle(61-xposg,47+yposg,2,0xff00ff00);
        OLED96x96.drawCircle(61-xposg,47+yposg,1,0xff00ff00); 
        Thread::wait(10);
        OLED96x96.drawCircle(61-xposg,47+yposg,5,0xff000000);
        OLED96x96.drawCircle(61-xposg,47+yposg,4,0xff000000);
        OLED96x96.drawCircle(61-xposg,47+yposg,3,0xff000000);
        OLED96x96.drawCircle(61-xposg,47+yposg,2,0xff000000); 
        OLED96x96.drawCircle(61-xposg,47+yposg,1,0xff000000);
        if ((61-xposg == 40+randx) and (47+yposg == 40+randy))
        {
          pc.printf("Hit!\r\n");
          led1 = LED_ON;
          haptic = 1;
          count3 = 0;
        }
        count3++;
        count2++;
        if ((count3 > 20)and (haptic == 1))
        {
          count2 = 101;  
          haptic = 0;
        }
        if (count2 > 100)
        {   //blank display to avoid image burn
            haptic = 0;
            count3 = 0;
            led1 = LED_OFF;
            OLED96x96.fillRect(16,0,111,96,0xff000000);//alpha, BGR
            count2 = 0;
            randx = rgen()%40;
            pc.printf("randx = %d\r\n",randx);
            randy = rgen()%40;
            pc.printf("randy = %d\r\n",randy);
            OLED96x96.drawCircle(40+randx,40+randy,3,0xffff00ff);
            OLED96x96.drawCircle(40+randx,40+randy,2,0xffff00ff);
            OLED96x96.drawCircle(40+randx,40+randy,1,0xff00ff00);
        }
            //BOOSTEN = 0;  //turn off display after about 1 minute to avoid image burn
    }

}

Credits

R. Scott Coppersmith

R. Scott Coppersmith

15 projects • 24 followers
Research Engineer, Embedded systems designer
Thanks to mbed.org.

Comments