Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed 4DGL-uLCD-SE SDFileSystem PinDetect
Revision 5:cf8ae4ca6f2b, committed 2022-04-25
- Comitter:
- mmcdoanld81
- Date:
- Mon Apr 25 01:52:31 2022 +0000
- Parent:
- 4:9a4d22a279b3
- Commit message:
- LAME-Michael McDonald
Changed in this revision
diff -r 9a4d22a279b3 -r cf8ae4ca6f2b MMA8452.cpp
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/MMA8452.cpp Mon Apr 25 01:52:31 2022 +0000
@@ -0,0 +1,538 @@
+// Authors: Ashley Mills, Nicholas Herriot
+/* Copyright (c) 2013 Vodafone, MIT License
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
+ * and associated documentation files (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute,
+ * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all copies or
+ * substantial portions of the Software.
+ *
+ * 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.
+ */
+
+#include "MMA8452.h"
+#include "mbed.h"
+
+#ifdef MMA8452_DEBUG
+// you need to define Serial pc(USBTX,USBRX) somewhere for the below line to make sense
+extern Serial pc;
+#define MMA8452_DBG(...) pc.printf(__VA_ARGS__); pc.printf("\r\n");
+#else
+#define MMA8452_DBG(...)
+#endif
+
+// Connect module at I2C address using I2C port pins sda and scl
+MMA8452::MMA8452(PinName sda, PinName scl, int frequency) : _i2c(sda, scl) , _frequency(frequency) {
+ MMA8452_DBG("Creating MMA8452");
+
+ // set I2C frequency
+ _i2c.frequency(_frequency);
+
+ // setup read and write addresses for convenience
+ _readAddress = MMA8452_ADDRESS | 0x01;
+ _writeAddress = MMA8452_ADDRESS & 0xFE;
+
+ // set some defaults
+ _bitDepth = BIT_DEPTH_UNKNOWN;
+ setBitDepth(BIT_DEPTH_12);
+ _dynamicRange = DYNAMIC_RANGE_UNKNOWN;
+ setDynamicRange(DYNAMIC_RANGE_2G);
+
+ MMA8452_DBG("Done");
+}
+
+
+// Destroys instance
+MMA8452::~MMA8452() {}
+
+// Setting the control register bit 1 to true to activate the MMA8452
+int MMA8452::activate() {
+ // perform write and return error code
+ return logicalORRegister(MMA8452_CTRL_REG_1,MMA8452_ACTIVE_MASK);
+}
+
+// Setting the control register bit 1 to 0 to standby the MMA8452
+int MMA8452::standby() {
+ // perform write and return error code
+ return logicalANDRegister(MMA8452_CTRL_REG_1,MMA8452_STANDBY_MASK);
+}
+
+// this reads a register, applies a bitmask with logical AND, sets a value with logical OR,
+// and optionally goes into and out of standby at the beginning and end of the function respectively
+int MMA8452::maskAndApplyRegister(char reg, char mask, char value, int toggleActivation) {
+ if(toggleActivation) {
+ if(standby()) {
+ return 1;
+ }
+ }
+
+ // read from register
+ char oldValue = 0;
+ if(readRegister(reg,&oldValue)) {
+ return 1;
+ }
+
+ // apply bitmask
+ oldValue &= mask;
+
+ // set value
+ oldValue |= value;
+
+ // write back to register
+ if(writeRegister(reg,oldValue)) {
+ return 1;
+ }
+
+ if(toggleActivation) {
+ if(activate()) {
+ return 1;
+ }
+ }
+ return 0;
+}
+
+int MMA8452::setDynamicRange(DynamicRange range, int toggleActivation) {
+ _dynamicRange = range;
+ return maskAndApplyRegister(
+ MMA8452_XYZ_DATA_CFG,
+ MMA8452_DYNAMIC_RANGE_MASK,
+ range,
+ toggleActivation
+ );
+}
+
+int MMA8452::setDataRate(DataRateHz dataRate, int toggleActivation) {
+ return maskAndApplyRegister(
+ MMA8452_CTRL_REG_1,
+ MMA8452_DATA_RATE_MASK,
+ dataRate<<MMA8452_DATA_RATE_MASK_SHIFT,
+ toggleActivation
+ );
+}
+
+int MMA8452::setBitDepth(BitDepth depth,int toggleActivation) {
+ _bitDepth = depth;
+ return maskAndApplyRegister(
+ MMA8452_CTRL_REG_1,
+ MMA8452_BIT_DEPTH_MASK,
+ depth<<MMA8452_BIT_DEPTH_MASK_SHIFT,
+ toggleActivation
+ );
+}
+
+char MMA8452::getMaskedRegister(int addr, char mask) {
+ char rval = 0;
+ if(readRegister(addr,&rval)) {
+ return 0;
+ }
+ return (rval&mask);
+}
+
+int MMA8452::isXYZReady() {
+ return getMaskedRegister(MMA8452_STATUS,MMA8452_STATUS_ZYXDR_MASK)>0;
+}
+
+int MMA8452::isXReady() {
+ return getMaskedRegister(MMA8452_STATUS,MMA8452_STATUS_XDR_MASK)>0;
+}
+
+int MMA8452::isYReady() {
+ return getMaskedRegister(MMA8452_STATUS,MMA8452_STATUS_YDR_MASK)>0;
+}
+
+int MMA8452::isZReady() {
+ return getMaskedRegister(MMA8452_STATUS,MMA8452_STATUS_ZDR_MASK)>0;
+}
+
+
+int MMA8452::getDeviceID(char *dst) {
+ return readRegister(MMA8452_WHO_AM_I,dst);
+}
+
+int MMA8452::getStatus(char* dst) {
+ return readRegister(MMA8452_STATUS,dst);
+}
+
+MMA8452::DynamicRange MMA8452::getDynamicRange() {
+ char rval = 0;
+ if(readRegister(MMA8452_XYZ_DATA_CFG,&rval)) {
+ return MMA8452::DYNAMIC_RANGE_UNKNOWN;
+ }
+ rval &= (MMA8452_DYNAMIC_RANGE_MASK^0xFF);
+ return (MMA8452::DynamicRange)rval;
+}
+
+MMA8452::DataRateHz MMA8452::getDataRate() {
+ char rval = 0;
+ if(readRegister(MMA8452_CTRL_REG_1,&rval)) {
+ return MMA8452::RATE_UNKNOWN;
+ }
+ // logical AND with inverse of mask
+ rval = rval&(MMA8452_DATA_RATE_MASK^0xFF);
+ // shift back into position
+ rval >>= MMA8452_DATA_RATE_MASK_SHIFT;
+ return (MMA8452::DataRateHz)rval;
+}
+
+// Reads xyz
+int MMA8452::readXYZRaw(char *dst) {
+ if(_bitDepth==BIT_DEPTH_UNKNOWN) {
+ return 1;
+ }
+ int readLen = 3;
+ if(_bitDepth==BIT_DEPTH_12) {
+ readLen = 6;
+ }
+ return readRegister(MMA8452_OUT_X_MSB,dst,readLen);
+}
+
+int MMA8452::readXRaw(char *dst) {
+ if(_bitDepth==BIT_DEPTH_UNKNOWN) {
+ return 1;
+ }
+ int readLen = 1;
+ if(_bitDepth==BIT_DEPTH_12) {
+ readLen = 2;
+ }
+ return readRegister(MMA8452_OUT_X_MSB,dst,readLen);
+}
+
+int MMA8452::readYRaw(char *dst) {
+ if(_bitDepth==BIT_DEPTH_UNKNOWN) {
+ return 1;
+ }
+ int readLen = 1;
+ if(_bitDepth==BIT_DEPTH_12) {
+ readLen = 2;
+ }
+ return readRegister(MMA8452_OUT_Y_MSB,dst,readLen);
+}
+
+int MMA8452::readZRaw(char *dst) {
+ if(_bitDepth==BIT_DEPTH_UNKNOWN) {
+ return 1;
+ }
+ int readLen = 1;
+ if(_bitDepth==BIT_DEPTH_12) {
+ readLen = 2;
+ }
+ return readRegister(MMA8452_OUT_Z_MSB,dst,readLen);
+}
+
+int MMA8452::readXYZCounts(int *x, int *y, int *z) {
+ char buf[6];
+ if(readXYZRaw((char*)&buf)) {
+ return 1;
+ }
+ if(_bitDepth==BIT_DEPTH_12) {
+ *x = twelveBitToSigned(&buf[0]);
+ *y = twelveBitToSigned(&buf[2]);
+ *z = twelveBitToSigned(&buf[4]);
+ } else {
+ *x = eightBitToSigned(&buf[0]);
+ *y = eightBitToSigned(&buf[1]);
+ *z = eightBitToSigned(&buf[2]);
+ }
+
+ return 0;
+}
+
+int MMA8452::readXCount(int *x) {
+ char buf[2];
+ if(readXRaw((char*)&buf)) {
+ return 1;
+ }
+ if(_bitDepth==BIT_DEPTH_12) {
+ *x = twelveBitToSigned(&buf[0]);
+ } else {
+ *x = eightBitToSigned(&buf[0]);
+ }
+ return 0;
+}
+
+int MMA8452::readYCount(int *y) {
+ char buf[2];
+ if(readYRaw((char*)&buf)) {
+ return 1;
+ }
+ if(_bitDepth==BIT_DEPTH_12) {
+ *y = twelveBitToSigned(&buf[0]);
+ } else {
+ *y = eightBitToSigned(&buf[0]);
+ }
+ return 0;
+}
+
+int MMA8452::readZCount(int *z) {
+ char buf[2];
+ if(readZRaw((char*)&buf)) {
+ return 1;
+ }
+ if(_bitDepth==BIT_DEPTH_12) {
+ *z = twelveBitToSigned(&buf[0]);
+ } else {
+ *z = eightBitToSigned(&buf[0]);
+ }
+ return 0;
+}
+
+double MMA8452::convertCountToGravity(int count, int countsPerG) {
+ return (double)count/(double)countsPerG;
+}
+
+int MMA8452::getCountsPerG() {
+ // assume starting with DYNAMIC_RANGE_2G and BIT_DEPTH_12
+ int countsPerG = 1024;
+ if(_bitDepth==BIT_DEPTH_8) {
+ countsPerG = 64;
+ }
+ switch(_dynamicRange) {
+ case DYNAMIC_RANGE_4G:
+ countsPerG /= 2;
+ break;
+ case DYNAMIC_RANGE_8G:
+ countsPerG /= 4;
+ break;
+ }
+ return countsPerG;
+}
+
+int MMA8452::readXYZGravity(double *x, double *y, double *z) {
+ int xCount = 0, yCount = 0, zCount = 0;
+ if(readXYZCounts(&xCount,&yCount,&zCount)) {
+ return 1;
+ }
+ int countsPerG = getCountsPerG();
+
+ *x = convertCountToGravity(xCount,countsPerG);
+ *y = convertCountToGravity(yCount,countsPerG);
+ *z = convertCountToGravity(zCount,countsPerG);
+ return 0;
+}
+
+int MMA8452::readXGravity(double *x) {
+ int xCount = 0;
+ if(readXCount(&xCount)) {
+ return 1;
+ }
+ int countsPerG = getCountsPerG();
+
+ *x = convertCountToGravity(xCount,countsPerG);
+ return 0;
+}
+
+int MMA8452::readYGravity(double *y) {
+ int yCount = 0;
+ if(readYCount(&yCount)) {
+ return 1;
+ }
+ int countsPerG = getCountsPerG();
+
+ *y = convertCountToGravity(yCount,countsPerG);
+ return 0;
+}
+
+int MMA8452::readZGravity(double *z) {
+ int zCount = 0;
+ if(readZCount(&zCount)) {
+ return 1;
+ }
+ int countsPerG = getCountsPerG();
+
+ *z = convertCountToGravity(zCount,countsPerG);
+ return 0;
+}
+
+// apply an AND mask to a register. read register value, apply mask, write it back
+int MMA8452::logicalANDRegister(char addr, char mask) {
+ char value = 0;
+ // read register value
+ if(readRegister(addr,&value)) {
+ return 0;
+ }
+ // apply mask
+ value &= mask;
+ return writeRegister(addr,value);
+}
+
+
+// apply an OR mask to a register. read register value, apply mask, write it back
+int MMA8452::logicalORRegister(char addr, char mask) {
+ char value = 0;
+ // read register value
+ if(readRegister(addr,&value)) {
+ return 0;
+ }
+ // apply mask
+ value |= mask;
+ return writeRegister(addr,value);
+}
+
+// apply an OR mask to a register. read register value, apply mask, write it back
+int MMA8452::logicalXORRegister(char addr, char mask) {
+ char value = 0;
+ // read register value
+ if(readRegister(addr,&value)) {
+ return 0;
+ }
+ // apply mask
+ value ^= mask;
+ return writeRegister(addr,value);
+}
+
+// Write register (The device must be placed in Standby Mode to change the value of the registers)
+int MMA8452::writeRegister(char addr, char data) {
+ // what this actually does is the following
+ // 1. tell I2C bus to start transaction
+ // 2. tell slave we want to write (slave address & write flag)
+ // 3. send the write address
+ // 4. send the data to write
+ // 5. tell I2C bus to end transaction
+
+ // we can wrap this up in the I2C library write function
+ char buf[2] = {0,0};
+ buf[0] = addr;
+ buf[1] = data;
+ return _i2c.write(MMA8452_ADDRESS, buf,2);
+ // note, could also do return writeRegister(addr,&data,1);
+}
+
+int MMA8452::eightBitToSigned(char *buf) {
+ return (int8_t)*buf;
+}
+
+int MMA8452::twelveBitToSigned(char *buf) {
+ // cheat by using the int16_t internal type
+ // all we need to do is convert to little-endian format and shift right
+ int16_t x = 0;
+ ((char*)&x)[1] = buf[0];
+ ((char*)&x)[0] = buf[1];
+ // note this only works because the below is an arithmetic right shift
+ return x>>4;
+}
+
+int MMA8452::writeRegister(char addr, char *data, int nbytes) {
+ // writing multiple bytes is a little bit annoying because
+ // the I2C library doesn't support sending the address separately
+ // so we just do it manually
+
+ // 1. tell I2C bus to start transaction
+ _i2c.start();
+ // 2. tell slave we want to write (slave address & write flag)
+ if(_i2c.write(_writeAddress)!=1) {
+ return 1;
+ }
+ // 3. send the write address
+ if(_i2c.write(addr)!=1) {
+ return 1;
+ }
+ // 4. send the data to write
+ for(int i=0; i<nbytes; i++) {
+ if(_i2c.write(data[i])!=1) {
+ return 1;
+ }
+ }
+ // 5. tell I2C bus to end transaction
+ _i2c.stop();
+ return 0;
+}
+
+int MMA8452::readRegister(char addr, char *dst, int nbytes) {
+ // this is a bit odd, but basically proceeds like this
+ // 1. Send a start command
+ // 2. Tell the slave we want to write (slave address & write flag)
+ // 3. Send the address of the register (addr)
+ // 4. Send another start command to delineate read portion
+ // 5. Tell the slave we want to read (slave address & read flag)
+ // 6. Read the register value bytes
+ // 7. Send a stop command
+
+ // we can wrap this process in the I2C library read and write commands
+ if(_i2c.write(MMA8452_ADDRESS,&addr,1,true)) {
+ return 1;
+ }
+ return _i2c.read(MMA8452_ADDRESS,dst,nbytes);
+}
+
+// most registers are 1 byte, so here is a convenience function
+int MMA8452::readRegister(char addr, char *dst) {
+ return readRegister(addr,dst,1);
+}
+
+MMA8452::BitDepth MMA8452::getBitDepth() {
+ return _bitDepth;
+}
+
+#ifdef MMA8452_DEBUG
+void MMA8452::debugRegister(char reg) {
+ // get register value
+ char v = 0;
+ if(readRegister(reg,&v)) {
+ MMA8452_DBG("Error reading specified register");
+ return;
+ }
+ // print out details
+ switch(reg) {
+ case MMA8452_CTRL_REG_1:
+ MMA8452_DBG("CTRL_REG_1 has value: 0x%x",v);
+ MMA8452_DBG(" 7 ALSP_RATE_1: %d",(v&0x80)>>7);
+ MMA8452_DBG(" 6 ALSP_RATE_0: %d",(v&0x40)>>6);
+ MMA8452_DBG(" 5 DR2: %d", (v&0x20)>>5);
+ MMA8452_DBG(" 4 DR1: %d", (v&0x10)>>4);
+ MMA8452_DBG(" 3 DR0: %d", (v&0x08)>>3);
+ MMA8452_DBG(" 2 LNOISE: %d", (v&0x04)>>2);
+ MMA8452_DBG(" 1 FREAD: %d", (v&0x02)>>1);
+ MMA8452_DBG(" 0 ACTIVE: %d", (v&0x01));
+ break;
+
+ case MMA8452_XYZ_DATA_CFG:
+ MMA8452_DBG("XYZ_DATA_CFG has value: 0x%x",v);
+ MMA8452_DBG(" 7 Unused: %d", (v&0x80)>>7);
+ MMA8452_DBG(" 6 0: %d", (v&0x40)>>6);
+ MMA8452_DBG(" 5 0: %d", (v&0x20)>>5);
+ MMA8452_DBG(" 4 HPF_Out: %d",(v&0x10)>>4);
+ MMA8452_DBG(" 3 0: %d", (v&0x08)>>3);
+ MMA8452_DBG(" 2 0: %d", (v&0x04)>>2);
+ MMA8452_DBG(" 1 FS1: %d", (v&0x02)>>1);
+ MMA8452_DBG(" 0 FS0: %d", (v&0x01));
+ switch(v&0x03) {
+ case 0:
+ MMA8452_DBG("Dynamic range: 2G");
+ break;
+ case 1:
+ MMA8452_DBG("Dynamic range: 4G");
+ break;
+ case 2:
+ MMA8452_DBG("Dynamic range: 8G");
+ break;
+ default:
+ MMA8452_DBG("Unknown dynamic range");
+ break;
+ }
+ break;
+
+ case MMA8452_STATUS:
+ MMA8452_DBG("STATUS has value: 0x%x",v);
+ MMA8452_DBG(" 7 ZYXOW: %d",(v&0x80)>>7);
+ MMA8452_DBG(" 6 ZOW: %d", (v&0x40)>>6);
+ MMA8452_DBG(" 5 YOW: %d", (v&0x20)>>5);
+ MMA8452_DBG(" 4 XOW: %d", (v&0x10)>>4);
+ MMA8452_DBG(" 3 ZYXDR: %d",(v&0x08)>>3);
+ MMA8452_DBG(" 2 ZDR: %d", (v&0x04)>>2);
+ MMA8452_DBG(" 1 YDR: %d", (v&0x02)>>1);
+ MMA8452_DBG(" 0 XDR: %d", (v&0x01));
+ break;
+
+ default:
+ MMA8452_DBG("Unknown register address: 0x%x",reg);
+ break;
+ }
+}
+#endif
\ No newline at end of file
diff -r 9a4d22a279b3 -r cf8ae4ca6f2b MMA8452.h
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/MMA8452.h Mon Apr 25 01:52:31 2022 +0000
@@ -0,0 +1,344 @@
+#pragma once
+
+// Authors: Ashley Mills, Nicholas Herriot
+/* Copyright (c) 2013 Vodafone, MIT License
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
+ * and associated documentation files (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute,
+ * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all copies or
+ * substantial portions of the Software.
+ *
+ * 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.
+ */
+
+// the SparkFun breakout board defaults to 1, set to 0 if SA0 jumper on the bottom of the board is set
+// see the Table 10. I2C Device Address Sequence in Freescale MMA8452Q pdf
+
+#include "mbed.h"
+
+#define MMA8452_DEBUG 1
+
+// More info on MCU Master address can be found on section 5.10.1 of http://www.freescale.com/webapp/sps/site/prod_summary.jsp?code=MMA8452Q
+#define SA0 1
+#if SA0
+ #define MMA8452_ADDRESS 0x3A // 0x1D<<1 // SA0 is high, 0x1C if low -
+#else
+ #define MMA8452_ADDRESS 0x38 // 0x1C<<1
+#endif
+
+// Register descriptions found in section 6 of pdf
+#define MMA8452_STATUS 0x00 // Type 'read' : Status of the data registers
+#define MMA8452_OUT_X_MSB 0x01 // Type 'read' : x axis - MSB of 2 byte sample
+#define MMA8452_OUT_X_LSB 0x02 // Type 'read' : x axis - LSB of 2 byte sample
+#define MMA8452_OUT_Y_MSB 0x03 // Type 'read' : y axis - MSB of 2 byte sample
+#define MMA8452_OUT_Y_LSB 0x04 // Type 'read' : y axis - LSB of 2 byte sample
+#define MMA8452_OUT_Z_MSB 0x05 // Type 'read' : z axis - MSB of 2 byte sample
+#define MMA8452_OUT_Z_LSB 0x06 // Type 'read' : z axis - LSB of 2 byte sample
+
+// register definitions
+#define MMA8452_XYZ_DATA_CFG 0x0E
+
+#define MMA8452_SYSMOD 0x0B // Type 'read' : This tells you if device is active, sleep or standy 0x00=STANDBY 0x01=WAKE 0x02=SLEEP
+#define MMA8452_WHO_AM_I 0x0D // Type 'read' : This should return the device id of 0x2A
+
+#define MMA8452_PL_STATUS 0x10 // Type 'read' : This shows portrait landscape mode orientation
+#define MMA8452_PL_CFG 0x11 // Type 'read/write' : This allows portrait landscape configuration
+#define MMA8452_PL_COUNT 0x12 // Type 'read' : This is the portraint landscape debounce counter
+#define MMA8452_PL_BF_ZCOMP 0x13 // Type 'read' :
+#define MMA8452_PL_THS_REG 0x14 // Type 'read' :
+
+#define MMA8452_FF_MT_CFG 0X15 // Type 'read/write' : Freefaul motion functional block configuration
+#define MMA8452_FF_MT_SRC 0X16 // Type 'read' : Freefaul motion event source register
+#define MMA8452_FF_MT_THS 0X17 // Type 'read' : Freefaul motion threshold register
+#define MMA8452_FF_COUNT 0X18 // Type 'read' : Freefaul motion debouce counter
+
+#define MMA8452_ASLP_COUNT 0x29 // Type 'read/write' : Counter settings for auto sleep
+#define MMA8452_CTRL_REG_1 0x2A // Type 'read/write' :
+#define MMA8452_CTRL_REG_2 0x2B // Type 'read/write' :
+#define MMA8452_CTRL_REG_3 0x2C // Type 'read/write' :
+#define MMA8452_CTRL_REG_4 0x2D // Type 'read/write' :
+#define MMA8452_CTRL_REG_5 0x2E // Type 'read/write' :
+
+// Defined in table 13 of the Freescale PDF
+/// xxx these all need to have better names
+#define STANDBY 0x00 // State value returned after a SYSMOD request, it can be in state STANDBY, WAKE or SLEEP
+#define WAKE 0x01 // State value returned after a SYSMOD request, it can be in state STANDBY, WAKE or SLEEP
+#define SLEEP 0x02 // State value returned after a SYSMOD request, it can be in state STANDBY, WAKE or SLEEP
+#define ACTIVE 0x01 // Stage value returned and set in Control Register 1, it can be STANDBY=00, or ACTIVE=01
+
+#define TILT_STATUS 0x03 // Tilt Status (Read only)
+#define SRST_STATUS 0x04 // Sample Rate Status Register (Read only)
+#define SPCNT_STATUS 0x05 // Sleep Count Register (Read/Write)
+#define INTSU_STATUS 0x06 // Interrupt Setup Register
+#define MODE_STATUS 0x07 // Mode Register (Read/Write)
+#define SR_STATUS 0x08 // Auto-Wake and Active Mode Portrait/Landscape Samples per Seconds Register (Read/Write)
+#define PDET_STATUS 0x09 // Tap/Pulse Detection Register (Read/Write)
+#define PD_STATUS 0xA // Tap/Pulse Debounce Count Register (Read/Write)
+
+// masks for enabling/disabling standby
+#define MMA8452_ACTIVE_MASK 0x01
+#define MMA8452_STANDBY_MASK 0xFE
+
+// mask for dynamic range reading and writing
+#define MMA8452_DYNAMIC_RANGE_MASK 0xFC
+
+// mask and shift for data rate reading and writing
+#define MMA8452_DATA_RATE_MASK 0xC7
+#define MMA8452_DATA_RATE_MASK_SHIFT 0x03
+
+// mask and shift for general reading and writing
+#define MMA8452_WRITE_MASK 0xFE
+#define MMA8452_READ_MASK 0x01
+
+// mask and shift for bit depth reading and writing
+#define MMA8452_BIT_DEPTH_MASK 0xFD
+#define MMA8452_BIT_DEPTH_MASK_SHIFT 0x01
+
+// status masks and shifts
+#define MMA8452_STATUS_ZYXDR_MASK 0x08
+#define MMA8452_STATUS_ZDR_MASK 0x04
+#define MMA8452_STATUS_YDR_MASK 0x02
+#define MMA8452_STATUS_XDR_MASK 0x01
+
+/**
+ * Wrapper for the MMA8452 I2C driven accelerometer.
+ */
+class MMA8452 {
+
+ public:
+
+ enum DynamicRange {
+ DYNAMIC_RANGE_2G=0x00,
+ DYNAMIC_RANGE_4G,
+ DYNAMIC_RANGE_8G,
+ DYNAMIC_RANGE_UNKNOWN
+ };
+
+ enum BitDepth {
+ BIT_DEPTH_12=0x00,
+ BIT_DEPTH_8, // 1 sets fast read mode, hence the inversion
+ BIT_DEPTH_UNKNOWN
+ };
+
+ enum DataRateHz {
+ RATE_800=0x00,
+ RATE_400,
+ RATE_200,
+ RATE_100,
+ RATE_50,
+ RATE_12_5,
+ RATE_6_25,
+ RATE_1_563,
+ RATE_UNKNOWN
+ };
+
+ /**
+ * Create an accelerometer object connected to the specified I2C pins.
+ *
+ * @param sda I2C data port
+ * @param scl I2C clock port
+ * @param frequency
+ *
+ */
+ MMA8452(PinName sda, PinName scl, int frequency);
+
+ /// Destructor
+ ~MMA8452();
+
+ /**
+ * Puts the MMA8452 in active mode.
+ * @return 0 on success, 1 on failure.
+ */
+ int activate();
+
+ /**
+ * Puts the MMA8452 in standby.
+ * @return 0 on success, 1 on failure.
+ */
+ int standby();
+
+ /**
+ * Read the device ID from the accelerometer (should be 0x2a)
+ *
+ * @param dst pointer to store the ID
+ * @return 0 on success, 1 on failure.
+ */
+ int getDeviceID(char* dst);
+
+ /**
+ * Read the MMA8452 status register.
+ *
+ * @param dst pointer to store the register value.
+ * @ return 0 on success, 1 on failure.
+ */
+ int getStatus(char* dst);
+
+ /**
+ * Read the raw x, y, an z registers of the MMA8452 in one operation.
+ * All three registers are read sequentially and stored in the provided buffer.
+ * The stored values are signed 2's complement left-aligned 12 or 8 bit integers.
+ *
+ * @param dst The destination buffer. Note that this needs to be 3 bytes for
+ * BIT_DEPTH_8 and 6 bytes for BIT_DEPTH_12. It is upto the caller to ensure this.
+ * @return 0 for success, and 1 for failure
+ * @sa setBitDepth
+ */
+ int readXYZRaw(char *dst);
+
+ /// Read the raw x register into the provided buffer. @sa readXYZRaw
+ int readXRaw(char *dst);
+ /// Read the raw y register into the provided buffer. @sa readXYZRaw
+ int readYRaw(char *dst);
+ /// Read the raw z register into the provided buffer. @sa readXYZRaw
+ int readZRaw(char *dst);
+
+ /**
+ * Read the x, y, and z signed counts of the MMA8452 axes.
+ *
+ * Count resolution is either 8 bits or 12 bits, and the range is either +-2G, +-4G, or +-8G
+ * depending on settings. The number of counts per G are 1024, 512, 256 for 2,4, and 8 G
+ * respectively at 12 bit resolution and 64, 32, 16 for 2, 4, and 8 G respectively at
+ * 8 bit resolution.
+ *
+ * This function queries the MMA8452 and returns the signed counts for each axes.
+ *
+ * @param x Pointer to integer to store x count
+ * @param y Pointer to integer to store y count
+ * @param z Pointer to integer to store z count
+ * @return 0 on success, 1 on failure.
+ */
+ int readXYZCounts(int *x, int *y, int *z);
+
+ /// Read the x axes signed count. @sa readXYZCounts
+ int readXCount(int *x);
+ /// Read the y axes signed count. @sa readXYZCounts
+ int readYCount(int *y);
+ /// Read the z axes signed count. @sa readXYZCounts
+ int readZCount(int *z);
+
+ /**
+ * Read the x, y, and z accelerations measured in G.
+ *
+ * The measurement resolution is controlled via setBitDepth which can
+ * be 8 or 12, and by setDynamicRange, which can be +-2G, +-4G, or +-8G.
+ *
+ * @param x A pointer to the double to store the x acceleration in.
+ * @param y A pointer to the double to store the y acceleration in.
+ * @param z A pointer to the double to store the z acceleration in.
+ *
+ * @return 0 on success, 1 on failure.
+ */
+ int readXYZGravity(double *x, double *y, double *z);
+
+ /// Read the x gravity in G into the provided double pointer. @sa readXYZGravity
+ int readXGravity(double *x);
+ /// Read the y gravity in G into the provided double pointer. @sa readXYZGravity
+ int readYGravity(double *y);
+ /// Read the z gravity in G into the provided double pointer. @sa readXYZGravity
+ int readZGravity(double *z);
+
+ /// Returns 1 if data has been internally sampled (is available) for all axes since last read, 0 otherwise.
+ int isXYZReady();
+ /// Returns 1 if data has been internally sampled (is available) for the x-axis since last read, 0 otherwise.
+ int isXReady();
+ /// Returns 1 if data has been internally sampled (is available) for the y-axis since last read, 0 otherwise.
+ int isYReady();
+ /// Returns 1 if data has been internally sampled (is available) for the z-axis since last read, 0 otherwise.
+ int isZReady();
+
+ /**
+ * Reads a single byte from the specified MMA8452 register.
+ *
+ * @param addr The internal register address.
+ * @param dst The destination buffer address.
+ * @return 1 on success, 0 on failure.
+ */
+ int readRegister(char addr, char *dst);
+
+ /**
+ * Reads n bytes from the specified MMA8452 register.
+ *
+ * @param addr The internal register address.
+ * @param dst The destination buffer address.
+ * @param nbytes The number of bytes to read.
+ * @return 1 on success, 0 on failure.
+ */
+ int readRegister(char addr, char *dst, int nbytes);
+
+ /**
+ * Write to the specified MMA8452 register.
+ *
+ * @param addr The internal register address
+ * @param data Data byte to write
+ */
+ int writeRegister(char addr, char data);
+
+ /**
+ * Write a data buffer to the specified MMA8452 register.
+ *
+ * @param addr The internal register address
+ * @param data Pointer to data buffer to write
+ * @param nbytes The length of the data buffer to write
+ */
+ int writeRegister(char addr, char *data, int nbytes);
+
+ int setDynamicRange(DynamicRange range, int toggleActivation=1);
+ int setBitDepth(BitDepth depth, int toggleActivation=1);
+ int setDataRate(DataRateHz dataRate, int toggleActivation=1);
+
+ DynamicRange getDynamicRange();
+ DataRateHz getDataRate();
+ BitDepth getBitDepth();
+
+ #ifdef MMA8452_DEBUG
+ void debugRegister(char reg);
+ #endif
+
+ private:
+ /**
+ * Reads the specified register, applies the mask with logical AND, logical ORs the value
+ * and writes back the result to the register. If toggleActivation is set to true then the
+ * device is put in standby before the operation, and activated at the end.
+ * Setting it to false is useful for setting options on a device that you want to keep in
+ * standby.
+ */
+ int maskAndApplyRegister(char reg, char mask, char value, int toggleActivation);
+
+ /// Reads the specified register, applies the mask with logical AND, and writes the result back.
+ int logicalANDRegister(char addr, char mask);
+ /// Reads the specified register, applies the mask with logical OR, and writes the result back.
+ int logicalORRegister(char addr, char mask);
+ /// Reads the specified register, applies the mask with logical XOR, and writes the result back.
+ int logicalXORRegister(char addr, char mask);
+
+ /// Converts the 12-bit two's complement number in buf to a signed integer. Returns the integer.
+ int twelveBitToSigned(char *buf);
+ /// Converts the 8-bit two's complement number in buf to a signed integer. Returns the integer.
+ int eightBitToSigned(char *buf);
+
+ /// Converts a count to a gravity using the supplied countsPerG. Returns the gravity.
+ double convertCountToGravity(int count, int countsPerG);
+
+ /// Reads the register at addr, applies the mask with logical AND, and returns the result.
+ char getMaskedRegister(int addr, char mask);
+
+ /// Get the counts per G for the current settings of bit depth and dynamic range.
+ int getCountsPerG();
+
+ I2C _i2c;
+ int _frequency;
+ int _readAddress;
+ int _writeAddress;
+
+ BitDepth _bitDepth;
+ DynamicRange _dynamicRange;
+};
+
\ No newline at end of file
diff -r 9a4d22a279b3 -r cf8ae4ca6f2b lame.cc
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/lame.cc Mon Apr 25 01:52:31 2022 +0000
@@ -0,0 +1,215 @@
+#include "mbed.h"
+#include "uLCD_4DGL.h"
+#include "PinDetect.h"
+#include "Speaker.h"
+#include "MMA8452.h"
+#include "lame.h"
+#include "sprites.h"
+
+
+void ScreenObject::changexPos(int input) {
+ if (xPosition < 110 && direction == true) {
+ xPosition += input;
+ if (xPosition >= 110) {
+ direction = false;
+ xPosition = 109;
+ }
+ }
+ else if (xPosition < 110 && direction == false) {
+ xPosition -= input;
+ if (xPosition <= 10) {
+ direction = true;
+ xPosition = 11;
+ }
+ }
+}
+//Clears the screen using the clear sprite, very useful cuz im lazy
+void ScreenObject::clearScreen(uLCD_4DGL& input) {
+ input.BLIT(xPosition, yPosition, 15, 8, clear_sprite);
+ xPosition = 1;
+ yPosition = 1;
+}
+
+//Implementation of setters and getters!
+void ScreenObject::setxPosition(int input) {
+ xPosition = input;
+}
+
+void ScreenObject::setyPosition(int input) {
+ yPosition = input;
+}
+
+void ScreenObject::setDirec(bool input) {
+ direction = input;
+}
+
+void ScreenObject::setHit(bool input){
+ hit = input;
+}
+
+bool ScreenObject::getHit(){
+ return hit;
+}
+
+int ScreenObject::getxPosition() {
+ return xPosition;
+}
+
+int ScreenObject::getyPosition() {
+ return yPosition;
+}
+
+//---Ship---
+Ship::Ship() {
+ setxPosition(64);
+ setyPosition(110);
+}
+
+void Ship::draw(uLCD_4DGL& input) {
+ input.BLIT(getxPosition(), getyPosition(), 27, 8, ship_sprite);
+}
+
+void Ship::update(uLCD_4DGL& input) {
+ input.BLIT(getxPosition(), getyPosition(), 27, 8, ship_sprite);
+}
+
+void Ship::moveRight() {
+ if (getxPosition() < 100)
+ {
+ setxPosition(getxPosition()+ 2);
+ }
+}
+
+void Ship::moveLeft() {
+ if (getxPosition() > 10)
+ {
+ setxPosition(getxPosition()- 2);
+ }
+}
+
+
+//---Bullet---
+Bullet::Bullet(): oneBullet(false) { }
+
+void Bullet::draw(uLCD_4DGL& input){
+ input.BLIT(getxPosition(), getyPosition(), 1, 3, bullet_sprite);
+ }
+void Bullet::update(uLCD_4DGL& input){
+ if (getyPosition() >= 4 && oneBullet == true) {
+ setyPosition(getyPosition() - 2);
+ input.BLIT(getxPosition(), getyPosition() + 2, 1, 3, nobullet_sprite);
+ input.BLIT(getxPosition(), getyPosition(), 1, 3, bullet_sprite);
+ if (getyPosition() < 4) {
+ input.BLIT(getxPosition(), getyPosition(), 1, 3, nobullet_sprite);
+ oneBullet = false;
+ }
+ }
+}
+
+void Bullet::fireBullet(Ship &input1, uLCD_4DGL& input2) {
+ setxPosition(input1.getxPosition()+ 8);
+ setyPosition(input1.getyPosition() - 4);
+ draw(input2);
+}
+
+void Bullet::setBullet(bool input){
+ oneBullet = input;
+}
+
+bool Bullet::getBullet(){
+ return oneBullet;
+}
+
+int Bullet::getxBullet() {
+ return getxPosition();
+}
+
+int Bullet::getyBullet() {
+ return getyPosition();
+}
+
+bool Bullet::hitTest(ScreenObject& input) {
+ setHit(((getxBullet() >= (input.getxPosition()-7)) && (getxBullet() <= (input.getxPosition() + 7))
+ && (getyBullet() >= (input.getyPosition()-5))
+ && (getyBullet() <= (input.getyPosition() + 5))));
+ return getHit(); //this guy was hit
+}
+
+//---Alice---
+AlienAlice::AlienAlice(int input) {
+ setxPosition(1 + rand() % 110);
+ setyPosition((10+10*(input-1)) + rand() % 2);
+ setDirec(true);
+}
+
+void AlienAlice::draw(uLCD_4DGL& input) {
+ input.BLIT(getxPosition(), getyPosition(), 15, 8, alienAlice_sprite);
+}
+
+void AlienAlice::update(uLCD_4DGL& input) {
+ changexPos(2);
+ input.BLIT(getxPosition(), getyPosition(), 15, 8, alienAlice_sprite);
+}
+
+
+//---Bob---
+AlienBob::AlienBob(int input) {
+ setxPosition(1 + rand() % 110);
+ setyPosition((10+10*(input-1)) + rand() % 2);
+ setDirec(true);
+}
+
+void AlienBob::draw(uLCD_4DGL& input) {
+ input.BLIT(getxPosition(), getyPosition(), 15, 8, alienBobUp_sprite);
+}
+
+void AlienBob::update(uLCD_4DGL& input) {
+ changexPos(1);
+ int res = getxPosition() % 2;
+ switch(res)
+ {
+ case(0):
+ {
+ input.BLIT(getxPosition(), getyPosition(), 15, 8, alienBobDown_sprite);
+ break;
+ }
+ case(1):
+ {
+ input.BLIT(getxPosition(), getyPosition(), 15, 8, alienBobUp_sprite);
+ break;
+ }
+ }
+}
+
+//---Jeff---
+AlienJeff::AlienJeff(int input) {
+ setxPosition(1 + rand() % 110);
+ setyPosition((10+10*(input-1)) + rand() % 2);
+ setDirec(true);
+}
+
+void AlienJeff::draw(uLCD_4DGL& input) {
+ input.BLIT(getxPosition(), getyPosition(), 15, 8, alienJeff_sprite);
+}
+
+void AlienJeff::update(uLCD_4DGL& input) {
+ changexPos(2);
+ input.BLIT(getxPosition(), getyPosition(), 15, 8, alienJeff_sprite);
+}
+
+//---Mike---
+AlienMike::AlienMike(int input) {
+ setxPosition(1 + rand() % 110);
+ setyPosition((10+10*(input-1)) + rand() % 2);
+ setDirec(true);
+}
+
+void AlienMike::draw(uLCD_4DGL& input) {
+ input.BLIT(getxPosition(), getyPosition(), 15, 8, alienMike_sprite);
+}
+
+void AlienMike::update(uLCD_4DGL& input) {
+ changexPos(2);
+ input.BLIT(getxPosition(), getyPosition(), 15, 8, alienMike_sprite);
+}
+
diff -r 9a4d22a279b3 -r cf8ae4ca6f2b lame.h
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/lame.h Mon Apr 25 01:52:31 2022 +0000
@@ -0,0 +1,83 @@
+#ifndef LAME_H
+#define LAME_H
+
+//This is my base class!
+class ScreenObject {
+public:
+ virtual void draw(uLCD_4DGL&) = 0; //pure virtual
+ virtual void update(uLCD_4DGL&) = 0; //pure virtual
+ void clearScreen(uLCD_4DGL&);
+ void changexPos(int);
+ void setxPosition(int);
+ void setyPosition(int);
+ void setDirec(bool);
+ void setHit(bool);
+ bool getHit();
+ int getxPosition();
+ int getyPosition();
+private:
+ int xPosition;
+ int yPosition;
+ bool direction;
+ bool hit;
+};
+
+//This is my ship class that inherits from my base class
+class Ship : public ScreenObject {
+public:
+ Ship();
+ void draw(uLCD_4DGL&);
+ void update(uLCD_4DGL&);
+ void moveRight();
+ void moveLeft();
+};
+
+//Bullet class with inheritance
+class Bullet : public ScreenObject {
+public:
+ Bullet();
+ void draw(uLCD_4DGL&);
+ void update(uLCD_4DGL&);
+ void fireBullet(Ship&, uLCD_4DGL&);
+ void setBullet(bool);
+ bool getBullet();
+ int getxBullet();
+ int getyBullet();
+ bool hitTest(ScreenObject&);
+private:
+ bool oneBullet;
+};
+
+//Alien alice be inheriting
+class AlienAlice : public ScreenObject {
+public:
+ AlienAlice(int);
+ void draw(uLCD_4DGL&);
+ void update(uLCD_4DGL&);
+};
+
+//Same with alien bob
+class AlienBob : public ScreenObject {
+public:
+ AlienBob(int);
+ void draw(uLCD_4DGL&);
+ void update(uLCD_4DGL&);
+};
+
+//Cant forget alien jeff
+class AlienJeff : public ScreenObject {
+public:
+ AlienJeff(int);
+ void draw(uLCD_4DGL&);
+ void update(uLCD_4DGL&);
+};
+
+//Finally alien Mike is inheriting stuff
+class AlienMike : public ScreenObject {
+public:
+ AlienMike(int);
+ void draw(uLCD_4DGL&);
+ void update(uLCD_4DGL&);
+};
+
+#endif
\ No newline at end of file
diff -r 9a4d22a279b3 -r cf8ae4ca6f2b main.cpp
--- a/main.cpp Thu Jan 23 16:47:05 2014 +0000
+++ b/main.cpp Mon Apr 25 01:52:31 2022 +0000
@@ -1,141 +1,176 @@
-// skeleton code for ECE 2036 thermostat lab
-// code must be added by students
+//Michael McDonald-LAME
+
#include "mbed.h"
-#include "TMP36.h"
-#include "SDFileSystem.h"
#include "uLCD_4DGL.h"
#include "PinDetect.h"
#include "Speaker.h"
-// must add your new class code to the project file Shiftbrite.h
-#include "Shiftbrite.h"
-
-// use class to setup temperature sensor pins
-TMP36 myTMP36(p15); //Analog in
+#include "MMA8452.h"
+#include "lame.h"
+#include <cstdlib>
+#include <ctime>
-// use class to setup microSD card filesystem
-SDFileSystem sd(p5, p6, p7, p8, "sd");
-
-// use class to setup the Color LCD
-uLCD_4DGL uLCD(p28, p27, p29); // create a global uLCD object
+PinDetect pbFire(p22);
+Serial pc(USBTX,USBRX);
+uLCD_4DGL uLCD(p9, p10, p11);
+MMA8452 acc(p28, p27, 40000);
+Speaker soundOut(p25);
-// use class to setup pushbuttons pins
-PinDetect pb1(p23);
-PinDetect pb2(p24);
-PinDetect pb3(p25);
-
-// use class to setup speaker pin
-Speaker mySpeaker(p21); //PWM out
+//Create my objects
+Timer timeObject;
+Ship shipObject;
+Bullet bulletObject;
-// use class to setup Shiftbrite pins
-Shiftbrite myShiftbrite(p9, p10, p11, p12, p13);// ei li di n/c ci
-
-// use class to setup Mbed's four on-board LEDs
-DigitalOut myLED1(LED1);
-DigitalOut myLED2(LED2);
-DigitalOut myLED3(LED3);
-DigitalOut myLED4(LED4);
-
-
+// Pushbutton Function
+void pbFire_hit_callback(void) {
+ soundOut.PlayNote(100, 0.05, 0.1);
+ bulletObject.setBullet(true);
+}
+// Accelerometer Functions
+void setupAccelerometer(){
+ acc.setBitDepth(MMA8452::BIT_DEPTH_12);
+ acc.setDynamicRange(MMA8452::DYNAMIC_RANGE_4G);
+ acc.setDataRate(MMA8452::RATE_100);
+}
+ double x = .01;
+ double y = .01;
+ double z = .01;
-//also setting any unused analog input pins to digital outputs reduces A/D noise a bit
-//see http://mbed.org/users/chris/notebook/Getting-best-ADC-performance/
-DigitalOut P16(p16);
-DigitalOut P17(p17);
-DigitalOut P18(p18);
-DigitalOut P19(p19);
-DigitalOut P20(p20);
+void accInput(){
+ acc.readXYZGravity(&x,&y,&z);
+ if ((y>0.1)){
+ shipObject.moveLeft();}
+ if ((y<-0.1)){
+ shipObject.moveRight();}
-
-
+};
-// Global variables used in callbacks and main program
-// C variables in interrupt routines should use volatile keyword
-int volatile heat_setting=78; // heat to temp
-int volatile cool_setting=68; // cool to temp
-bool volatile mode=false; // heat or cool mode
+int main() {
+
+ setupAccelerometer();
+ std::srand(time(0)); //seeded with time
-// Callback routine is interrupt activated by a debounced pb1 hit
-void pb1_hit_callback (void)
-{
-// ADD CODE HERE
-}
-// Callback routine is interrupt activated by a debounced pb2 hit
-void pb2_hit_callback (void)
-{
-// ADD CODE HERE
-}
-// Callback routine is interrupt activated by a debounced pb3 hit
-void pb3_hit_callback (void)
-{
-// ADD CODE HERE
-}
+ uLCD.display_control(LANDSCAPE_R);
+ uLCD.cls();
+ uLCD.baudrate(3000000);
+ wait(0.3);
+ uLCD.background_color(BLACK);
+
+ // Intro Screen Message
+ uLCD.text_width(2);
+ uLCD.text_height(2);
+ uLCD.printf("HELP!\nTHEY ARE\nATTACKING");
+ soundOut.PlayNote(100.0,0.15,0.2);
+ soundOut.PlayNote(200.0,0.15,0.2);
+ soundOut.PlayNote(300.0,0.15,0.2);
+ soundOut.PlayNote(400.0,0.15,0.2);
+ soundOut.PlayNote(300.0,0.15,0.2);
+ soundOut.PlayNote(200.0,0.15,0.2);
+ soundOut.PlayNote(100.0,0.15,0.2);
+ wait(2.0);
+
+ pbFire.mode(PullUp);
+ pbFire.attach_deasserted(&pbFire_hit_callback);
+ pbFire.setSampleFrequency();
+ wait(.01);
-int main()
-{
- float Current_temp=0.0;
-
- // Use internal pullups for the three pushbuttons
- pb1.mode(PullUp);
- pb2.mode(PullUp);
- pb3.mode(PullUp);
- // Delay for initial pullup to take effect
- wait(.01);
- // Setup Interrupt callback functions for a pb hit
- pb1.attach_deasserted(&pb1_hit_callback);
- pb2.attach_deasserted(&pb2_hit_callback);
- pb3.attach_deasserted(&pb3_hit_callback);
- // Start sampling pb inputs using interrupts
- pb1.setSampleFrequency();
- pb2.setSampleFrequency();
- pb3.setSampleFrequency();
- // pushbuttons now setup and running
-
+
+ uLCD.cls();
+ ScreenObject *alienObject[6];
+ alienObject[0] = new AlienBob(1);
+ alienObject[1] = new AlienMike(2);
+ alienObject[2] = new AlienJeff(3);
+ alienObject[3] = new AlienBob(4);
+ alienObject[4] = new AlienMike(5);
+ alienObject[5] = new AlienAlice(6);
+ for (int i = 0; i < 6; i++)
+ {
+ alienObject[i]->draw(uLCD);
+ alienObject[i]->setHit(false);
+ }
+
+
+ shipObject.draw(uLCD);
+ timeObject.start();
+ bool endGame = false;
+ int numberOfAliens = 6;
+ bool isHit[6] = {false};
+ while (endGame == false) {
+ accInput();
+ if (bulletObject.getBullet() == true) {
+ bulletObject.fireBullet(shipObject,uLCD);
+ soundOut.PlayNote(100, 0.05, 0.1);
+ soundOut.PlayNote(50, 0.05, 0.3);
+ }
+ //If the ship has a bullet, run this code
+ while (bulletObject.getBullet() == true) {
+ accInput();
+ shipObject.draw(uLCD);
+ bulletObject.update(uLCD);
+
+ for (int i = 0; i < 6; i++) {
+ if (numberOfAliens < 1) {
+ endGame = true;
+ break;
+ }
- // start I/O examples - DELETE THIS IN YOUR CODE..BUT WILL USE THESE I/O IDEAS ELSEWHERE
- // since all this compiles - the needed *.h files for these are in the project
- //
- Current_temp = myTMP36; //Read temp sensor
- printf("Hello PC World\n\r"); // need terminal application running on PC to see this output
- uLCD.printf("\n\rHello LCD World\n\r"); // LCD
- mySpeaker.PlayNote(500.0, 1.0, 1.0); // Speaker buzz
- myShiftbrite.write( 0, 50 ,0); // Green RGB LED
- // SD card write file example - prints error message on PC when running until SD card hooked up
- // Delete to avoid run time error
- mkdir("/sd/mydir", 0777); // set up directory and permissions
- FILE *fp = fopen("/sd/mydir/sdtest.txt", "w"); //open SD
- if(fp == NULL) {
- error("Could not open file for write\n");
- }
- fprintf(fp, "Hello SD Card World!"); // write SD
- fclose(fp); // close SD card
- //
- // end I/O examples
-
-
-
-
- // State machine code below will need changes and additions
- while (1) {
- {
- enum Statetype { Heat_off = 0, Heat_on };
- Statetype state = Heat_off;
- while(1) {
- switch (state) {
- case Heat_off:
- myLED4 = 0;
- state = Heat_on;
- break;
- case Heat_on:
- myLED4 = 1;
- state = Heat_off;
- break;
+ isHit[i] = (bulletObject.hitTest(*alienObject[i]));
+
+ if (isHit[i] == true){
+ alienObject[i]->setHit(true);
+ }
+ if (isHit[i] == true) {
+ --numberOfAliens;
+ alienObject[i]->clearScreen(uLCD);
+ soundOut.PlayNote(200, 0.1, 0.1);
+ soundOut.PlayNote(350, 0.1, 0.1);
+
+ uLCD.filled_circle(bulletObject.getxBullet(), bulletObject.getyBullet(), 8, BLACK);
+ bulletObject.setBullet(false);
+ }
+ else
+ if ((alienObject[i]->getHit()) == false) {
+ alienObject[i]->update(uLCD);
}
- wait(0.33);
- // heartbeat LED - common debug tool
- // blinks as long as code is running and not locked up
- myLED1=!myLED1;
}
+ wait(0.01);
+ }
+
+ while (bulletObject.getBullet() == false) {
+ accInput();
+ if (numberOfAliens < 1) {
+ endGame = true;
+ break;
+ }
+
+ shipObject.draw(uLCD);
+ for (int i = 0; i < 6; i++) {
+ if ((alienObject[i]->getHit()) == false){
+ alienObject[i]->update(uLCD);
+ }
+ }
+ wait(0.01);
}
}
-}
\ No newline at end of file
+
+ delete[] alienObject;
+ timeObject.stop();
+ float total = timeObject.read();
+
+ //Winning sound
+ soundOut.PlayNote(1200, 0.1, 0.1);
+ soundOut.PlayNote(900, 0.1, 0.1);
+ soundOut.PlayNote(600, 0.1, 0.1);
+ soundOut.PlayNote(300, 0.1, 0.1);
+ soundOut.PlayNote(100, 0.1, 0.1);
+ soundOut.PlayNote(300, 0.1, 0.1);
+ soundOut.PlayNote(600, 0.1, 0.1);
+ soundOut.PlayNote(900, 0.1, 0.1);
+ soundOut.PlayNote(1200, 0.1, 0.1);
+
+ //Win Screen
+ uLCD.cls();
+ uLCD.printf("\n\n\n\n Congratulations!!\n\n Earth Saved!\n\n\n (%f\n Seconds)",total);
+ wait(4.0);
+ exit(0);
+}//end main
diff -r 9a4d22a279b3 -r cf8ae4ca6f2b sprites.h
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/sprites.h Mon Apr 25 01:52:31 2022 +0000
@@ -0,0 +1,102 @@
+//Sprites for LAME
+#define _ 0x000000 // black
+#define X 0xFFFFFF // white
+#define O 0xEB4034 //red
+#define U 0x0345FC //blue
+#define Y 0x00F2FF //cyan
+
+// AlienAlice Sprites ----------------------------------------------------------
+int alienAlice_sprite[8 * 15] = {
+_,_,X,X,_,_,_,X,_,_,_,X,X,_,_,
+_,_,_,_,X,X,_,_,_,X,X,_,_,_,_,
+_,_,_,_,_,X,X,_,X,X,_,_,_,_,_,
+_,_,O,O,O,O,O,O,O,O,O,O,O,_,_,
+_,_,O,U,U,_,_,U,_,_,U,U,O,_,_,
+_,_,O,U,U,U,U,U,U,U,U,U,O,_,_,
+_,_,_,O,O,O,U,U,U,O,O,O,_,_,_,
+_,_,_,_,_,_,O,O,O,_,_,_,_,_,_,
+};
+
+// AlienBob Sprites ------------------------------------------------------------
+int alienBobDown_sprite[8 * 15] = {
+_,_,_,_,_,X,X,_,X,X,_,_,_,_,_,
+_,_,X,_,X,_,_,_,_,_,X,_,X,_,_,
+_,_,X,_,X,X,X,X,X,X,X,_,X,_,_,
+_,_,X,X,X,X,X,X,X,X,X,X,X,_,_,
+_,_,_,X,X,O,X,X,X,O,X,X,_,_,_,
+_,_,_,_,X,X,X,X,X,X,X,_,_,_,_,
+_,_,_,_,_,X,_,_,_,X,_,_,_,_,_,
+_,_,_,_,X,_,_,_,_,_,X,_,_,_,_,
+};
+
+int alienBobUp_sprite[8 * 15] = {
+_,_,_,X,_,_,_,_,_,_,_,X,_,_,_,
+_,_,_,_,X,_,_,_,_,_,X,_,_,_,_,
+_,_,_,_,X,X,X,X,X,X,X,_,_,_,_,
+_,_,X,X,X,X,X,X,X,X,X,X,X,_,_,
+_,_,X,X,X,O,X,X,X,O,X,X,X,_,_,
+_,_,X,_,X,X,X,X,X,X,X,_,X,_,_,
+_,_,X,_,_,X,_,_,_,X,_,_,X,_,_,
+_,_,_,_,X,_,_,_,_,_,X,_,_,_,_,
+};
+
+
+int alienJeff_sprite[8 * 15] = {
+_,_,_,O,_,_,_,_,_,_,_,O,_,_,_,
+_,_,_,O,_,_,U,_,U,_,_,O,_,_,_,
+_,_,_,O,_,X,O,X,O,X,_,O,_,_,_,
+_,_,_,O,_,X,X,X,X,X,_,O,_,_,_,
+_,_,_,O,O,O,X,X,X,O,O,O,_,_,_,
+_,_,_,_,_,U,U,U,U,U,_,_,_,_,_,
+_,_,_,O,O,O,U,U,U,O,O,O,_,_,_,
+_,_,_,O,_,_,_,U,_,_,_,O,_,_,_,
+};
+
+
+int alienMike_sprite[8 * 15] = {
+_,_,_,Y,_,_,_,_,_,_,_,Y,_,_,_,
+_,_,_,Y,_,_,Y,_,Y,_,_,Y,_,_,_,
+_,_,_,_,Y,Y,Y,Y,Y,Y,Y,_,_,_,_,
+_,_,U,_,_,Y,O,Y,O,Y,_,_,U,_,_,
+_,_,U,U,U,U,Y,Y,Y,U,U,U,U,_,_,
+_,_,_,_,U,_,Y,Y,Y,_,U,_,_,_,_,
+_,_,_,_,_,_,Y,Y,Y,_,_,_,_,_,_,
+_,_,_,_,_,_,_,Y,_,_,_,_,_,_,_,
+};
+
+// Ship Sprites ----------------------------------------------------------------
+int ship_sprite[8 * 27] = {
+_,_,_,_,_,_,_,_,_,_,O,O,O,_,O,O,O,_,_,_,_,_,_,_,_,_,_,
+_,_,_,_,_,_,_,_,_,_,X,X,X,_,X,X,X,_,_,_,_,_,_,_,_,_,_,
+_,_,_,_,_,X,X,X,X,X,X,X,X,X,X,X,X,X,X,X,X,X,_,_,_,_,_,
+_,_,_,_,_,_,X,X,X,X,X,X,X,X,X,X,X,X,X,X,X,_,_,_,_,_,_,
+_,_,_,_,_,_,_,X,X,_,_,X,X,X,X,X,_,_,X,X,_,_,_,_,_,_,_,
+_,_,_,_,_,_,_,X,X,_,_,_,X,X,X,_,_,_,X,X,_,_,_,_,_,_,_,
+_,_,_,_,_,_,_,O,O,_,_,_,X,X,X,_,_,_,O,O,_,_,_,_,_,_,_,
+_,_,_,_,_,_,_,_,_,_,_,_,_,O,_,_,_,_,_,_,_,_,_,_,_,_,_,
+};
+
+// Clear Sprites ---------------------------------------------------------------
+int clear_sprite[8 * 15] = {
+ _,_,_,_,_,_,_,_,_,_,_,_,_,_,_,
+ _,_,_,_,_,_,_,_,_,_,_,_,_,_,_,
+ _,_,_,_,_,_,_,_,_,_,_,_,_,_,_,
+ _,_,_,_,_,_,_,_,_,_,_,_,_,_,_,
+ _,_,_,_,_,_,_,_,_,_,_,_,_,_,_,
+ _,_,_,_,_,_,_,_,_,_,_,_,_,_,_,
+ _,_,_,_,_,_,_,_,_,_,_,_,_,_,_,
+ _,_,_,_,_,_,_,_,_,_,_,_,_,_,_,
+};
+
+// Bullet Sprites --------------------------------------------------------------
+int bullet_sprite[3 * 1] = {
+ X,
+ X,
+ O,
+};
+
+int nobullet_sprite[3 * 1] = {
+ _,
+ _,
+ _,
+};
\ No newline at end of file