Finished the game

Dependencies:   mbed

Welcome to Snake

See that the LCD is on and press start to play

The snake is in the middle of the screen, move the analogue up, down, right, left to move the snake in those directions. Note that snake doesn't travel diagonally.

Move the snake to get as much food as possible, there is plenty of food as you will find out

Beware, snake will die if you run its head into the walls, or into its body

Take care to keep moving snake fast, its body will catch up with its head and cause it to die!!!!!!

Have fun, and get that snake as long as possible :)

Committer:
el18lg
Date:
Sat May 23 17:21:24 2020 +0000
Revision:
1:bdafa20e71a0
Started my map, making my way onto my snake programme;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
el18lg 1:bdafa20e71a0 1 /* FXOS8700CQ Library
el18lg 1:bdafa20e71a0 2
el18lg 1:bdafa20e71a0 3 Sample code from ELEC2645 - demonstrates how to create a library
el18lg 1:bdafa20e71a0 4 for the K64F on-board accelerometer and magnetometer
el18lg 1:bdafa20e71a0 5
el18lg 1:bdafa20e71a0 6 (c) Craig A. Evans, University of Leeds, Jan 2017
el18lg 1:bdafa20e71a0 7
el18lg 1:bdafa20e71a0 8 */
el18lg 1:bdafa20e71a0 9
el18lg 1:bdafa20e71a0 10 #include "FXOS8700CQ.h"
el18lg 1:bdafa20e71a0 11
el18lg 1:bdafa20e71a0 12 // constructor is called when the object is created - use it to set pins and frequency
el18lg 1:bdafa20e71a0 13 FXOS8700CQ::FXOS8700CQ(PinName sda, PinName scl)
el18lg 1:bdafa20e71a0 14 {
el18lg 1:bdafa20e71a0 15 i2c = new I2C(sda,scl); // create new I2C instance and initialise
el18lg 1:bdafa20e71a0 16 }
el18lg 1:bdafa20e71a0 17
el18lg 1:bdafa20e71a0 18 // destructor is called when the object goes out of scope
el18lg 1:bdafa20e71a0 19 FXOS8700CQ::~FXOS8700CQ()
el18lg 1:bdafa20e71a0 20 {
el18lg 1:bdafa20e71a0 21 delete i2c; // free memory
el18lg 1:bdafa20e71a0 22 }
el18lg 1:bdafa20e71a0 23
el18lg 1:bdafa20e71a0 24 // based on 13.4 in datasheet - 200 Hz hybrid mode (both acc and mag)
el18lg 1:bdafa20e71a0 25 void FXOS8700CQ::init()
el18lg 1:bdafa20e71a0 26 {
el18lg 1:bdafa20e71a0 27 // i2c fast-mode - 10.1.1 data sheet
el18lg 1:bdafa20e71a0 28 i2c->frequency(400000); // I2C Fast Mode - 400kHz
el18lg 1:bdafa20e71a0 29
el18lg 1:bdafa20e71a0 30 // the device has an ID number so we check the value to ensure the correct
el18lg 1:bdafa20e71a0 31 // drive is on the i2c bus
el18lg 1:bdafa20e71a0 32 char data = read_byte_from_reg(FXOS8700CQ_WHO_AM_I);
el18lg 1:bdafa20e71a0 33 if (data != FXOS8700CQ_WHO_AM_I_VAL) { // if correct ID not found, hang and flash error message
el18lg 1:bdafa20e71a0 34 error("Incorrect ID!");
el18lg 1:bdafa20e71a0 35 }
el18lg 1:bdafa20e71a0 36
el18lg 1:bdafa20e71a0 37 // write 0000 0000 = 0x00 to accelerometer control register 1 to place
el18lg 1:bdafa20e71a0 38 // FXOS8700CQ into standby
el18lg 1:bdafa20e71a0 39 // [7-1] = 0000 000
el18lg 1:bdafa20e71a0 40 // [0]: active=0
el18lg 1:bdafa20e71a0 41 data = 0x00;
el18lg 1:bdafa20e71a0 42 send_byte_to_reg(data,FXOS8700CQ_CTRL_REG1);
el18lg 1:bdafa20e71a0 43
el18lg 1:bdafa20e71a0 44 // write 0001 1111 = 0x1F to magnetometer control register 1
el18lg 1:bdafa20e71a0 45 // [7]: m_acal=0: auto calibration disabled
el18lg 1:bdafa20e71a0 46 // [6]: m_rst=0: no one-shot magnetic reset
el18lg 1:bdafa20e71a0 47 // [5]: m_ost=0: no one-shot magnetic measurement
el18lg 1:bdafa20e71a0 48 // [4-2]: m_os=111=7: 8x oversampling (for 200Hz) to reduce magnetometer noise
el18lg 1:bdafa20e71a0 49 // [1-0]: m_hms=11=3: select hybrid mode with accel and magnetometer active
el18lg 1:bdafa20e71a0 50 data = 0x1F;
el18lg 1:bdafa20e71a0 51 send_byte_to_reg(data,FXOS8700CQ_M_CTRL_REG1);
el18lg 1:bdafa20e71a0 52
el18lg 1:bdafa20e71a0 53 // write 0010 0000 = 0x20 to magnetometer control register 2
el18lg 1:bdafa20e71a0 54 // [7]: reserved
el18lg 1:bdafa20e71a0 55 // [6]: reserved
el18lg 1:bdafa20e71a0 56 // [5]: hyb_autoinc_mode=1 to map the magnetometer registers to follow
el18lg 1:bdafa20e71a0 57 // the accelerometer registers
el18lg 1:bdafa20e71a0 58 // [4]: m_maxmin_dis=0 to retain default min/max latching even though not used
el18lg 1:bdafa20e71a0 59 // [3]: m_maxmin_dis_ths=0
el18lg 1:bdafa20e71a0 60 // [2]: m_maxmin_rst=0
el18lg 1:bdafa20e71a0 61 // [1-0]: m_rst_cnt=00 to enable magnetic reset each cycle
el18lg 1:bdafa20e71a0 62 data = 0x20;
el18lg 1:bdafa20e71a0 63 send_byte_to_reg(data,FXOS8700CQ_M_CTRL_REG2);
el18lg 1:bdafa20e71a0 64
el18lg 1:bdafa20e71a0 65 // write 0000 0001= 0x01 to XYZ_DATA_CFG register
el18lg 1:bdafa20e71a0 66 // [7]: reserved
el18lg 1:bdafa20e71a0 67 // [6]: reserved
el18lg 1:bdafa20e71a0 68 // [5]: reserved
el18lg 1:bdafa20e71a0 69 // [4]: hpf_out=0
el18lg 1:bdafa20e71a0 70 // [3]: reserved
el18lg 1:bdafa20e71a0 71 // [2]: reserved
el18lg 1:bdafa20e71a0 72 // [1-0]: fs=01 for accelerometer range of +/-4g range with 0.488mg/LSB
el18lg 1:bdafa20e71a0 73 data = 0x01;
el18lg 1:bdafa20e71a0 74 send_byte_to_reg(data,FXOS8700CQ_XYZ_DATA_CFG);
el18lg 1:bdafa20e71a0 75
el18lg 1:bdafa20e71a0 76 // write 0000 1101 = 0x0D to accelerometer control register 1
el18lg 1:bdafa20e71a0 77 // [7-6]: aslp_rate=00
el18lg 1:bdafa20e71a0 78 // [5-3]: dr=001 for 200Hz data rate (when in hybrid mode)
el18lg 1:bdafa20e71a0 79 // [2]: lnoise=1 for low noise mode
el18lg 1:bdafa20e71a0 80 // [1]: f_read=0 for normal 16 bit reads
el18lg 1:bdafa20e71a0 81 // [0]: active=1 to take the part out of standby and enable sampling
el18lg 1:bdafa20e71a0 82 data = 0x0D;
el18lg 1:bdafa20e71a0 83 send_byte_to_reg(data,FXOS8700CQ_CTRL_REG1);
el18lg 1:bdafa20e71a0 84
el18lg 1:bdafa20e71a0 85 }
el18lg 1:bdafa20e71a0 86
el18lg 1:bdafa20e71a0 87 Data FXOS8700CQ::get_values()
el18lg 1:bdafa20e71a0 88 {
el18lg 1:bdafa20e71a0 89 // 13 bytes - status plus 6 channels (2 bytes each)
el18lg 1:bdafa20e71a0 90 // x,y,z for accelerometer and magnetometer
el18lg 1:bdafa20e71a0 91 char data[FXOS8700CQ_READ_LEN];
el18lg 1:bdafa20e71a0 92 read_bytes_from_reg(FXOS8700CQ_STATUS,FXOS8700CQ_READ_LEN,data);
el18lg 1:bdafa20e71a0 93
el18lg 1:bdafa20e71a0 94 // copy the 14 bit accelerometer byte data into 16 bit words
el18lg 1:bdafa20e71a0 95 int acc_x = (int16_t)(((data[1] << 8) | data[2]))>> 2;
el18lg 1:bdafa20e71a0 96 int acc_y = (int16_t)(((data[3] << 8) | data[4]))>> 2;
el18lg 1:bdafa20e71a0 97 int acc_z = (int16_t)(((data[5] << 8) | data[6]))>> 2;
el18lg 1:bdafa20e71a0 98
el18lg 1:bdafa20e71a0 99 // copy the magnetometer byte data into 16 bit words
el18lg 1:bdafa20e71a0 100 int mag_x = (int16_t) (data[7] << 8) | data[8];
el18lg 1:bdafa20e71a0 101 int mag_y = (int16_t) (data[9] << 8) | data[10];
el18lg 1:bdafa20e71a0 102 int mag_z = (int16_t) (data[11] << 8) | data[12];
el18lg 1:bdafa20e71a0 103
el18lg 1:bdafa20e71a0 104 Data values; // struct to hold values
el18lg 1:bdafa20e71a0 105
el18lg 1:bdafa20e71a0 106 // 0.488 mg/LSB in 4 g mode (8.1 data sheet)
el18lg 1:bdafa20e71a0 107 values.ax = 0.488e-3*acc_x;
el18lg 1:bdafa20e71a0 108 values.ay = 0.488e-3*acc_y;
el18lg 1:bdafa20e71a0 109 values.az = 0.488e-3*acc_z;
el18lg 1:bdafa20e71a0 110
el18lg 1:bdafa20e71a0 111 // the magnetometer sensitivity is fixed at 0.1 μT/LSB
el18lg 1:bdafa20e71a0 112 values.mx = 0.1e-6*mag_x;
el18lg 1:bdafa20e71a0 113 values.my = 0.1e-6*mag_y;
el18lg 1:bdafa20e71a0 114 values.mz = 0.1e-6*mag_z;
el18lg 1:bdafa20e71a0 115
el18lg 1:bdafa20e71a0 116 return values;
el18lg 1:bdafa20e71a0 117 }
el18lg 1:bdafa20e71a0 118
el18lg 1:bdafa20e71a0 119 void FXOS8700CQ::send_byte_to_reg(char byte,char reg)
el18lg 1:bdafa20e71a0 120 {
el18lg 1:bdafa20e71a0 121 char data[2];
el18lg 1:bdafa20e71a0 122 data[0] = reg;
el18lg 1:bdafa20e71a0 123 data[1] = byte;
el18lg 1:bdafa20e71a0 124 // send the register address, followed by the data
el18lg 1:bdafa20e71a0 125 int nack = i2c->write(FXOS8700CQ_ADDR,data,2);
el18lg 1:bdafa20e71a0 126 if (nack)
el18lg 1:bdafa20e71a0 127 error("No acknowledgement received!"); // if we don't receive acknowledgement, send error message
el18lg 1:bdafa20e71a0 128
el18lg 1:bdafa20e71a0 129 }
el18lg 1:bdafa20e71a0 130
el18lg 1:bdafa20e71a0 131 // reads a byte from a specific register
el18lg 1:bdafa20e71a0 132 char FXOS8700CQ::read_byte_from_reg(char reg)
el18lg 1:bdafa20e71a0 133 {
el18lg 1:bdafa20e71a0 134 int nack = i2c->write(FXOS8700CQ_ADDR,&reg,1,true); // send the register address to the slave
el18lg 1:bdafa20e71a0 135 // true as need to send repeated start condition (5.10.1 datasheet)
el18lg 1:bdafa20e71a0 136 // http://www.i2c-bus.org/repeated-start-condition/
el18lg 1:bdafa20e71a0 137 if (nack)
el18lg 1:bdafa20e71a0 138 error("No acknowledgement received!"); // if we don't receive acknowledgement, send error message
el18lg 1:bdafa20e71a0 139
el18lg 1:bdafa20e71a0 140 char rx;
el18lg 1:bdafa20e71a0 141 nack = i2c->read(FXOS8700CQ_ADDR,&rx,1); // read a byte from the register and store in buffer
el18lg 1:bdafa20e71a0 142 if (nack)
el18lg 1:bdafa20e71a0 143 error("No acknowledgement received!"); // if we don't receive acknowledgement, send error message
el18lg 1:bdafa20e71a0 144
el18lg 1:bdafa20e71a0 145 return rx;
el18lg 1:bdafa20e71a0 146 }
el18lg 1:bdafa20e71a0 147
el18lg 1:bdafa20e71a0 148 // reads a series of bytes, starting from a specific register
el18lg 1:bdafa20e71a0 149 void FXOS8700CQ::read_bytes_from_reg(char reg,int number_of_bytes,char bytes[])
el18lg 1:bdafa20e71a0 150 {
el18lg 1:bdafa20e71a0 151 int nack = i2c->write(FXOS8700CQ_ADDR,&reg,1,true); // send the slave write address and the configuration register address
el18lg 1:bdafa20e71a0 152 // true as need to send repeated start condition (5.10.1 datasheet)
el18lg 1:bdafa20e71a0 153 // http://www.i2c-bus.org/repeated-start-condition/
el18lg 1:bdafa20e71a0 154
el18lg 1:bdafa20e71a0 155 if (nack)
el18lg 1:bdafa20e71a0 156 error("No acknowledgement received!"); // if we don't receive acknowledgement, send error message
el18lg 1:bdafa20e71a0 157
el18lg 1:bdafa20e71a0 158 nack = i2c->read(FXOS8700CQ_ADDR,bytes,number_of_bytes); // read bytes
el18lg 1:bdafa20e71a0 159 if (nack)
el18lg 1:bdafa20e71a0 160 error("No acknowledgement received!"); // if we don't receive acknowledgement, send error message
el18lg 1:bdafa20e71a0 161
el18lg 1:bdafa20e71a0 162 }