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: 4DGL-uLCD-SE EthernetInterface Game_Synchronizer LCD_fonts MMA8452 SDFileSystem mbed-rtos mbed wave_player
Fork of 2035_Tanks_Shell by
Revision 28:fdaa7ecfbd80, committed 2016-11-01
- Comitter:
- eriklomas
- Date:
- Tue Nov 01 18:59:29 2016 +0000
- Parent:
- 27:bd55ab4d137c
- Commit message:
- 4180;
Changed in this revision
--- a/Bullet/bullet.cpp Fri Oct 30 11:08:31 2015 +0000
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,44 +0,0 @@
-#include "uLCD_4DGL.h"
-#include "bullet.h"
-#include "game_synchronizer.h"
-#include "globals.h"
-#include "math.h"
-
-extern Game_Synchronizer sync;
-
-// Initialize the bullet. Don't have to do much here.
-// Keep a pointer to this bullet's tank.
-// Set the speed, and default the bullet to not in_flight.
-Bullet::Bullet(Tank* t) {
- tank = t;
- //speed = ???;
- in_flight = false;
-}
-
-// If in_flight, do nothing. Otherwise,
-// set the in_flight flag, and initialize values needed for
-// the trajectory calculations. (x0, y0), (vx0, vy0), time
-// Hint: tank->barrel_end(...) is useful here.
-void Bullet::shoot(void) {
- in_flight = true;
-}
-
-// If the bullet is in flight, calculate its new position
-// after a time delta dt.
-void Bullet::update_position(float dt) {
-
-}
-
-int Bullet::time_step(float dt) {
- // If the bullet hasn't hit anything,
- // redraw the bullet at its new location.
- // If it has hit something (obstacle, tank, edge of the screen),
- // set the in_flight flag back to false, explode the nearby area,
- // and return one of the following codes.
- //
- // return codes:
- // BULLET_NO_COLLISION: no collision
- // BULLET_OFF_SCREEN: off the side of the screen
- // Otherwise, return the color you've hit in 16bpp format.
- return BULLET_NO_COLLISION;
-}
\ No newline at end of file
--- a/Bullet/bullet.h Fri Oct 30 11:08:31 2015 +0000
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,53 +0,0 @@
-#ifndef BULLET_H__
-#define BULLET_H__
-
-#include "tank.h"
-
-#define BULLET_NO_COLLISION -1
-#define BULLET_OFF_SCREEN -2
-
-class Bullet{
- public:
-
- // (x0, y0) is the position of the bullet at the start of its trajectory.
- int x0, y0;
-
- // The components of the bullet's initial velocity.
- float vx0, vy0;
-
- // The current position of the bullet.
- int x, y;
-
- // How fast is the bullet?
- int speed;
-
- // Keep track of the time since the bullet was shot.
- float time;
-
- // Keep track of whether the bullet is currently in flight.
- // If it is in_flight, update its position. Otherwise, ignore it.
- // Set in_flight when the bullet is shot.
- // Reset in_flight when the bullet hits something.
- bool in_flight;
-
- // Keep a pointer to the tank that shot this bullet. You'll need
- // it to access information about the tank.
- Tank* tank;
-
- // Create a bullet!
- Bullet(Tank* t);
-
- // Shoot the bullet!
- void shoot(void);
-
- // given a time delta, calculate the new (x, y) coords of the bullet.
- void update_position(float dt);
-
- // Once per frame, plug in the time since the last frame.
- // Clear the old bullet and redraw it in the new place.
- // Do collision detection here. Return a code describing what has
- // (or hasn't) been hit.
- int time_step(float dt);
-};
-
-#endif
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/LCD_fonts.lib Tue Nov 01 18:59:29 2016 +0000 @@ -0,0 +1,1 @@ +http://developer.mbed.org/users/dreschpe/code/LCD_fonts/#d0b7d7bf1f56
--- a/Tank/tank.cpp Fri Oct 30 11:08:31 2015 +0000
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,65 +0,0 @@
-#include "tank.h"
-#include "globals.h"
-#include "math.h"
-#include "game_synchronizer.h"
-
-extern Game_Synchronizer sync;
-
-// sx is the x-coord of the bottom left corner of the tank
-// sy is the y-coord of the same corner
-// width is the width of the tank
-// height is the height of the tank
-Tank::Tank(int sx, int sy, int width, int height, int color) {
- x = sx; y = sy;
- w = width; h = height;
- tank_color = color;
- barrel_theta = PI/4.0;
- barrel_length = w;
- wheel_rad = 2.0;
- draw();
-}
-
-// Return the minimum x-coord of your tank's bounding box.
-int Tank::min_x(void) {
- return 0;
-}
-
-// Return the minimum y-coord of your tank's bounding box.
-int Tank::min_y(void) {
- return 0;
-}
-
-// Return the maximum x-coord of your tank's bounding box.
-int Tank::max_x(void) {
- return 0;
-}
-
-// Return the maximum y-coord of your tank's bounding box.
-int Tank::max_y(void) {
- return 1;
-}
-
-void Tank::barrel_end(int* bx, int* by) {
- // Set the x and y coords of the end of the barrel.
- // *bx = ???
- // *by = ???
-}
-
-void Tank::reposition(int dx, int dy, float dtheta) {
- // Blank out the old tank position, and
- // Move the tank dx pixels in the x direction.
- // Move the tank dy pixels in the y direction.
- // Move the tank barrel by an angle dtheta. Don't allow it to go below parallel.
-
- // Do collision detection to prevent the tank from hitting things.
- // (obstacles, side of the screen, other tanks, etc.)
-}
-
-
-// Example tank draw function. We expect you to get creative on this one!
-void Tank::draw() {
- sync.line(x + w/2.0, y+h+wheel_rad, x + w/2.0 + barrel_length*cos(barrel_theta), y+h+wheel_rad + barrel_length*sin(barrel_theta), BLACK);
- sync.filled_rectangle(x, y+wheel_rad, x+w, y+h+wheel_rad, tank_color);
- sync.filled_circle(x+wheel_rad, y+wheel_rad, wheel_rad, BLACK);
- sync.filled_circle(x+w-wheel_rad, y+wheel_rad, wheel_rad, BLACK);
-}
\ No newline at end of file
--- a/Tank/tank.h Fri Oct 30 11:08:31 2015 +0000
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,40 +0,0 @@
-#ifndef TANK_H__
-#define TANK_H__
-
-// This class describes a tank. You may need to add
-// additional member variables (and maybe even member functions)
-// to draw your super cool new tank.
-
-class Tank {
- public:
-
- int x, y; // Keep track of your tank's position.
- int w; // Tank width.
- int h; // Tank height.
- int tank_color; // Tank color. Duh :)
- float barrel_theta; // Barrel angle.
- int barrel_length; // Length of the barrel.
- int wheel_rad; // Radius of the wheels.
-
-
- // Construct a tank given its starting position (sx, sy),
- // its width and height, and its color.
- Tank(int sx, int sy, int width, int height, int color);
-
- // Calculate the bounding box of your tank for collision checking.
- int min_x(void);
- int min_y(void);
- int max_x(void);
- int max_y(void);
-
- // Calculate the position of the end of the barrel.
- void barrel_end(int* bx, int* by);
-
- // Reposition the tank!
- void reposition(int dx, int dy, float dtheta);
-
- // Draw the tank!
- void draw();
-};
-
-#endif
\ No newline at end of file
--- a/globals.h Fri Oct 30 11:08:31 2015 +0000 +++ b/globals.h Tue Nov 01 18:59:29 2016 +0000 @@ -5,6 +5,17 @@ #define GND_COLOR 0x66CD00 #define TANK_RED 0xCD0000 #define TANK_BLUE 0x000080 +#define LAVA_COLOR 0xFF0000 +#define YELLOW 0xFFF200 +#define TREE_COLOR 0x008000 +#define BULLET_COLOR 0X666666 +#define STONE_COLOR 0xc0c0c0 +#define LIGHTBLACK 0x404040 + +#define ex1 0xff3030 +#define ex2 0xee2c2c //explosion colors +#define ex3 0xcd2626 +#define ex4 0x8b1a1a #define U_BUTTON 0 #define R_BUTTON 1
--- a/main.cpp Fri Oct 30 11:08:31 2015 +0000
+++ b/main.cpp Tue Nov 01 18:59:29 2016 +0000
@@ -5,20 +5,13 @@
#include "SDFileSystem.h"
#include "wave_player.h"
#include "game_synchronizer.h"
-#include "tank.h"
-#include "bullet.h"
#include "globals.h"
-
+#include "playSound.h"
+#include "rtos.h"
+#include "uLCD_4DGL.h"
+#define VOLUME 0.03
+#define BPM 100.0
-DigitalOut led1(LED1);
-DigitalOut led2(LED2);
-DigitalOut led3(LED3);
-DigitalOut led4(LED4);
-
-DigitalIn pb_u(p21); // Up Button
-DigitalIn pb_r(p22); // Right Button
-DigitalIn pb_d(p23); // Down Button
-DigitalIn pb_l(p24); // Left Button
Serial pc(USBTX, USBRX); // Serial connection to PC. Useful for debugging!
MMA8452 acc(p28, p27, 100000); // Accelerometer (SDA, SCL, Baudrate)
@@ -26,199 +19,526 @@
SDFileSystem sd(p5, p6, p7, p8, "sd"); // SD (mosi, miso, sck, cs)
AnalogOut DACout(p18); // speaker
wave_player player(&DACout); // wav player
-Game_Synchronizer sync(PLAYER1); // Game_Synchronizer (PLAYER)
-Timer frame_timer; // Timer
-// Global variables go here.
+DigitalIn pb_u(p22); // Up Button
+DigitalIn pb_r(p24); // Right Button
+DigitalIn pb_d(p23); // Down Button
+DigitalIn pb_l(p21); // Left Button
+DigitalIn pb_c(p25);
+
+PwmOut pwm_pin(p26);
-int winner = -1;
-int whose_turn = PLAYER1;
-
+//InterruptIn button(p21);
-// Ask the user whether to run the game in Single- or Multi-Player mode.
-// Note that this function uses uLCD instead of sync because it is only
-// writing to the local (Player1) lcd. Sync hasn't been initialized yet,
-// so you can't use it anyway. For the same reason, you must access
-// the buttons directly e.g. if( !pb_r ) { do something; }.
+int ND=0, NU=0, NL=0, NR=0;
+int dx=6;
+int dy=6;
+int a,b,c,d;
+int i,z;
+int cc;
+int w;
-// return MULTI_PLAYER if the user selects multi-player.
-// return SINGLE_PLAYER if the user selects single-player.
-int game_menu(void) {
-
- uLCD.baudrate(BAUD_3000000);
+int N,E,S,W,N1,N2,E1,E2,S1,S2,W1,W2;
+
+int CONVERT_24_TO_16_BPP(int col_24) {
+ int p = col_24 & 0xFF;
+ int g = (col_24 >> 8) & 0xFF;
+ int r = (col_24 >> 16)& 0xFF;
- // the locate command tells the screen where to place the text.
- uLCD.locate(0,15);
- uLCD.puts("Replace me");
+ r >>= 3;
+ g >>= 2;
+ p >>= 3;
+
+ return r<<11 | g<<5 | p;
+}
- // Button Example:
- // Use !pb_r to access the player1 right button value.
-
- wait(2); // Eventually you can take this out.
-
- // Eventually you should return SINGLE_PLAYER or MULTI_PLAYER
- // depending on the user's choice.
- return SINGLE_PLAYER;
+bool pixel_eq(int color1, int color2) {
+ return (CONVERT_24_TO_16_BPP(color1) == color2 ||
+ CONVERT_24_TO_16_BPP(color2) == color1 ||
+ color1 == color2);
}
-// Initialize the world map. I've provided a basic map here,
-// but as part of the assignment you must create more
-// interesting map(s).
-// Note that calls to sync.function() will run function()
-// on both players' LCDs (assuming you are in multiplayer mode).
-// In single player mode, only your lcd will be modified. (Makes sense, right?)
-void map_init() {
-
- // Fill the entire screen with sky blue.
- sync.background_color(SKY_COLOR);
-
- // Call the clear screen function to force the LCD to redraw the screen
- // with the new background color.
- sync.cls();
-
- // Draw the ground in green.
- sync.filled_rectangle(0,0,128,20, GND_COLOR);
-
- // Draw some obstacles. They don't have to be black,
- // but they shouldn't be the same color as the sky or your tanks.
- // Get creative here. You could use brown and grey to draw a mountain
- // or something cool like that.
- sync.filled_rectangle(59, 20, 69, 60, BLACK);
-
- // Before you write text on the screens, tell the LCD where to put it.
- sync.locate(0,15);
-
- // Set the text background color to match the sky. Just for looks.
- sync.textbackground_color(SKY_COLOR);
-
- // Display the game title.
- char title[] = " Title";
- sync.puts(title, sizeof(title));
-
- // Flush the draw buffer and execute all the accumulated draw commands.
- sync.update();
+/*
+void thread1(void const *args)
+{
+ while(true) { // thread loop
+ z=rand()%8;
+ pc.printf("%d ",z);
+ Thread::wait(500); // wait 0.5s
+ if (z==0 || z==4 || z==7){ //Move Left
+ cc=cx-dx;
+ a=uLCD.read_pixel(cc, cy);
+ b = pixel_eq(a, BLACK);
+ if (b==1){
+ uLCD.filled_rectangle(cx-3,cy-3,cx+3,cy+3, BLACK);
+ cx=cx-dx;
+ uLCD.filled_rectangle(cx-3,cy-3,cx+3,cy+3, YELLOW);
+ pc.printf("C=%d\n",cc);
+ }
+ t = pixel_eq(a, STONE_COLOR);
+
+ }
+ if (z==1 || z==5 ||z==6){ //Move Right
+ cc=cx+dx;
+ a=uLCD.read_pixel(cc, cy);
+ b = pixel_eq(a, BLACK);
+ if (b==1){
+ uLCD.filled_rectangle(cx-3,cy-3,cx+3,cy+3, BLACK);
+ cx=cx+dx;
+ uLCD.filled_rectangle(cx-3,cy-3,cx+3,cy+3, YELLOW);
+ pc.printf("C=%d\n",cc);
+ }
+ t = pixel_eq(a, STONE_COLOR);
+
+ }
+ if (z==2|| z==4 || z==5){ //Move up
+ cc=cy-dy;
+ a=uLCD.read_pixel(cx, cc);
+ b = pixel_eq(a, BLACK);
+ if (b==1){
+ uLCD.filled_rectangle(cx-3,cy-3,cx+3,cy+3, BLACK);
+ cy=cy-dy;
+ uLCD.filled_rectangle(cx-3,cy-3,cx+3,cy+3, YELLOW);
+ pc.printf("C=%d\n",cc);
+ }
+ t = pixel_eq(a, STONE_COLOR);
+
+ }
+ if (z==3 || z==6 || z==7){ //Move Down
+ cc=cy+dy;
+ a=uLCD.read_pixel(cx, cc);
+ b = pixel_eq(a, BLACK);
+ if (b==1){
+ uLCD.filled_rectangle(cx-3,cy-3,cx+3,cy+3, BLACK);
+ cy=cy+dy;
+ uLCD.filled_rectangle(cx-3,cy-3,cx+3,cy+3, YELLOW);
+ pc.printf("C=%d\n",cc);
+ }
+ t = pixel_eq(a, STONE_COLOR);
+
+ }
+ }
}
+*/
-// Initialize the game hardware.
-// Call game_menu to find out which mode to play the game in (Single- or Multi-Player)
-// Initialize the game synchronizer.
-void game_init(void) {
-
- led1 = 0; led2 = 0; led3 = 0; led4 = 0;
-
- pb_u.mode(PullUp);
- pb_r.mode(PullUp);
- pb_d.mode(PullUp);
- pb_l.mode(PullUp);
-
- pc.printf("\033[2J\033[0;0H"); // Clear the terminal screen.
- pc.printf("I'm alive! Player 1\n"); // Let us know you made it this far.
- int mode = game_menu();
- sync.init(&uLCD, &acc, &pb_u, &pb_r, &pb_d, &pb_l, mode); // Connect to the other player.
- map_init();
- pc.printf("Initialized...\n"); // Let us know you finished initializing.
-}
+/*int cl=1;
-// Display some kind of game over screen which lets us know who won.
-// Play a cool sound!
-void game_over() {
-
+void flip(void) {
+ c=x-dx;
+ a=uLCD.read_pixel(c, y);
+ b = TREE_COLOR;
+ b = pixel_eq(a, TREE_COLOR);
+ pc.printf("a= %d",a);
+ pc.printf("b= %d",b);
+ while (b==1){
+ c=c-dx;
+ a=uLCD.read_pixel(c, y);
+ b = pixel_eq(a, TREE_COLOR);
+ pc.printf("loop:b=%d",b);
+ }
+ b = pixel_eq(a, LIGHTBLACK);
+ pc.printf("a2= %d",a);
+ pc.printf("b2:%d",b);
+ if (b==1){
+ goto Main;
+ }
+
+ a=uLCD.read_pixel(x-dx, y);
+ b = pixel_eq(a, BLACK);
+ cl=1;
+Main:
+}*/
+
+void thread1(void const *args)
+{
+ while(true) { // thread loop
+ playSound("/sd/wavfiles/minecraft0.wav");
+ }
}
-int main (void) {
-
- int* p1_buttons;
- int* p2_buttons;
-
- float ax1, ay1, az1;
- float ax2, ay2, az2;
-
- game_init();
-
- // Create your tanks.
- Tank t1(4, 21, 12, 8, TANK_RED); // (min_x, min_y, width, height, color)
- Tank t2(111, 21, 12, 8, TANK_BLUE); // (min_x, min_y, width, height, color)
-
- // For each tank, create a bullet.
- Bullet b1(&t1);
- Bullet b2(&t2);
-
- frame_timer.start();
-
- // Your code starts here...
- while(true) {
+ int main(){
+Start: uLCD.cls();
+ pb_l.mode(PullUp);
+ pb_r.mode(PullUp);
+ pb_u.mode(PullUp);
+ pb_d.mode(PullUp);
+ pb_c.mode(PullUp);
+
+ int x=64;
+ int y=64;
+ int cx=16;
+ int cy=64;
+ int count=0;
+ int t=0;
+ /*
+ button.mode(PullUp);
+ wait(.01);
+ button.fall(&flip);
+ */
+ //Main outer ring
+ uLCD.filled_rectangle(0,0,128,6, LIGHTBLACK); //MAKE STONE COLOR (LIGHT GRAY)
+ uLCD.filled_rectangle(0,0,6,128, LIGHTBLACK); //STONE COLOR
+ uLCD.filled_rectangle(0,122,128,128, LIGHTBLACK); //STONE COLOR
+ uLCD.filled_rectangle(122,0,128,128, LIGHTBLACK); //STONE COLOR
+
+ //inner ring
+ uLCD.filled_rectangle(26,26,102,102,TREE_COLOR);
+
+ //mouse
+ uLCD.filled_rectangle(61,61,67,67, STONE_COLOR);
+
+ //cat
+ uLCD.filled_rectangle(13,61,19,67, YELLOW);
+
+ Thread t1(thread1); //start thread4
+ pc.printf("before main");
+
+Main: while(1) {
+
+ ND=0;
+ NU=0;
+ NL=0;
+ NR=0;
+ pc.printf("aftre main");
+ if (!pb_c){
+ uLCD.filled_circle(x,y,.5, ex1);
+ //wait(.5);
+ uLCD.filled_circle(x,y,1.0, ex2);
+ //wait(.5);
+ uLCD.filled_circle(x,y,1.5, ex3);
+ //wait(.5);
+ uLCD.filled_circle(x,y,2.0, ex4);
+ uLCD.filled_circle(x,y,2.5, ex1);
+ uLCD.filled_circle(x,y,3.0, ex2);
+ uLCD.filled_circle(x,y,3.5, ex3);
+ uLCD.filled_circle(x,y,4.0, ex4);
+ uLCD.filled_circle(x,y,4.5, ex1);
+ uLCD.filled_circle(x,y,5.0, ex2);
+ uLCD.filled_circle(x,y,5.5, ex3);
+ uLCD.filled_circle(x,y,6.0, ex4);
+ uLCD.filled_circle(x,y,6.5, ex1);
+ uLCD.filled_circle(x,y,7.0, ex2);
+ uLCD.filled_circle(x,y,7.5, ex3);
+ uLCD.filled_circle(x,y,8.0, ex4);
+
+ uLCD.filled_circle(x,y,8.0, BLACK);
+ uLCD.filled_circle(x,y,7.5, ex3);
+ //wait(.5);
+ uLCD.filled_circle(x,y,7.5, BLACK);
+ uLCD.filled_circle(x,y,7.0, ex2);
+ //wait(.5);
+ uLCD.filled_circle(x,y,7.0, BLACK);
+ uLCD.filled_circle(x,y,6.5, ex1);
+ uLCD.filled_circle(x,y,6.5, BLACK);
+ uLCD.filled_circle(x,y,6.0, ex3);
+ //wait(.5);
+ uLCD.filled_circle(x,y,6.0, BLACK);
+ uLCD.filled_circle(x,y,5.5, ex2);
+ //wait(.5);
+ uLCD.filled_circle(x,y,5.5, BLACK);
+ uLCD.filled_circle(x,y,5.0, ex1);
+ uLCD.filled_circle(x,y,5.0, BLACK);
+ uLCD.filled_circle(x,y,4.5, ex3);
+ //wait(.5);
+ uLCD.filled_circle(x,y,4.5, BLACK);
+ uLCD.filled_circle(x,y,4.0, ex2);
+ //wait(.5);
+ uLCD.filled_circle(x,y,4.0, BLACK);
+ uLCD.filled_circle(x,y,3.5, ex1);
+ uLCD.filled_circle(x,y,3.5, BLACK);
+ uLCD.filled_circle(x,y,3.0, ex3);
+ //wait(.5);
+ uLCD.filled_circle(x,y,3.0, BLACK);
+ uLCD.filled_circle(x,y,2.5, ex2);
+ //wait(.5);
+ uLCD.filled_circle(x,y,2.5, BLACK);
+ uLCD.filled_circle(x,y,2.0, ex1); //
+ // wait(.5);
+ uLCD.filled_circle(x,y,2.0, BLACK);
+ uLCD.filled_circle(x,y,1.5, ex3);
+ //wait(.5);
+ uLCD.filled_circle(x,y,1.5, BLACK);
+ uLCD.filled_circle(x,y,1.0, ex2);
+ //wait(.5);
+ uLCD.filled_circle(x,y,1.0, BLACK);
+ uLCD.filled_circle(x,y,.5, ex1);
+ //wait(.5);
+ uLCD.filled_circle(x,y,.5, BLACK);
+ playSound("/sd/wavfiles/hit.wav");
+ t=1;
+ //wait(1);
+ }
+
+ if (t==1){
+ uLCD.cls();
+ uLCD.locate(4,2);
+ uLCD.puts("GAME OVER");
+ playSound("/sd/wavfiles/gameover.wav");
+ t=0;
+ wait(1);
+ uLCD.cls();
+ while (1){
+ uLCD.media_init();
+ uLCD.set_sector_address(0x003B, 0x5001);
+ uLCD.display_image(0,0);
+ if (!pb_l){
+ goto Start;
+ }
+ }
- // Get a pointer to the buttons for both sides.
- // From now on, you can access the buttons for player x using
- //
- // px_buttons[U_BUTTON]
- // px_buttons[R_BUTTON]
- // px_buttons[D_BUTTON]
- // px_buttons[L_BUTTON]
-
- p1_buttons = sync.get_p1_buttons();
- p2_buttons = sync.get_p2_buttons();
+ }
+ /*
+ if (cl==1){
+ cl=0;
+ uLCD.filled_rectangle(x-3,y-3,x+3,y+3, BLACK);
+ x=x-dx;
+ uLCD.filled_rectangle(x-3,y-3,x+3,y+3, STONE_COLOR);
+ pc.printf("C=%d\n",c);
+ if (b==0){
+ uLCD.filled_rectangle(c-3,y-3,c+3,y+3,TREE_COLOR);
+ }
+ }*/
+ if (!pb_l){ //Move Left
+ c=x-dx;
+ a=uLCD.read_pixel(c, y);
+ b = TREE_COLOR;
+ b = pixel_eq(a, TREE_COLOR);
+ pc.printf("a= %d",a);
+ pc.printf("b= %d",b);
+ while (b==1){
+ c=c-dx;
+ a=uLCD.read_pixel(c, y);
+ b = pixel_eq(a, TREE_COLOR);
+ pc.printf("loop:b=%d",b);
+ }
+ b = pixel_eq(a, LIGHTBLACK);
+ w= pixel_eq(a, YELLOW);
+ pc.printf("a2= %d",a);
+ pc.printf("b2:%d",b);
+ if (b==1||w==1){
+ goto Main;
+ }
+
+ a=uLCD.read_pixel(x-dx, y);
+ b = pixel_eq(a, BLACK);
+
+ uLCD.filled_rectangle(x-3,y-3,x+3,y+3, BLACK);
+ x=x-dx;
+ uLCD.filled_rectangle(x-3,y-3,x+3,y+3, STONE_COLOR);
+ pc.printf("C=%d\n",c);
+ if (b==0){
+ uLCD.filled_rectangle(c-3,y-3,c+3,y+3,TREE_COLOR);
+ }
+
+ }
+ if (!pb_r){ //Move Right
+ c=x+dx;
+ a=uLCD.read_pixel(c, y);
+ b = TREE_COLOR;
+ b = pixel_eq(a, TREE_COLOR);
+ pc.printf("a= %d",a);
+ pc.printf("b= %d",b);
+ while (b==1){
+ c=c+dx;
+ a=uLCD.read_pixel(c, y);
+ b = pixel_eq(a, TREE_COLOR);
+ pc.printf("loop:b=%d",b);
+ }
+ b = pixel_eq(a, LIGHTBLACK);
+ w= pixel_eq(a, YELLOW);
+ pc.printf("a2= %d",a);
+ pc.printf("b2:%d",b);
+ if (b==1||w==1){
+ goto Main;
+ }
+ a=uLCD.read_pixel(x+dx, y);
+ b = pixel_eq(a, BLACK);
+
+ uLCD.filled_rectangle(x-3,y-3,x+3,y+3, BLACK);
+ x=x+dx;
+ uLCD.filled_rectangle(x-3,y-3,x+3,y+3, STONE_COLOR);
+ pc.printf("C=%d\n",c);
+
+ if (b==0){
+ uLCD.filled_rectangle(c-3,y-3,c+3,y+3,TREE_COLOR);
+ }
+ }
+ if (!pb_d){ //Move Right
+ c=y+dy;
+ a=uLCD.read_pixel(x, c);
+ b = TREE_COLOR;
+ b = pixel_eq(a, TREE_COLOR);
+ pc.printf("a= %d",a);
+ pc.printf("b= %d",b);
+ while (b==1){
+ c=c+dy;
+ a=uLCD.read_pixel(x, c);
+ b = pixel_eq(a, TREE_COLOR);
+ pc.printf("loop:b=%d",b);
+ }
+ b = pixel_eq(a, LIGHTBLACK);
+ w= pixel_eq(a, YELLOW);
+ pc.printf("a2= %d",a);
+ pc.printf("b2:%d",b);
+ if (b==1||w==1){
+ goto Main;
+ }
+ a=uLCD.read_pixel(x, y+dy);
+ b = pixel_eq(a, BLACK);
+
+ uLCD.filled_rectangle(x-3,y-3,x+3,y+3, BLACK);
+ y=y+dy;
+ uLCD.filled_rectangle(x-3,y-3,x+3,y+3, STONE_COLOR);
+ pc.printf("C=%d\n",c);
+
+ if (b==0){
+ uLCD.filled_rectangle(x-3,c-3,x+3,c+3,TREE_COLOR);
+ }
+ }
+ if (!pb_u){ //Move Right
+ c=y-dy;
+ a=uLCD.read_pixel(x, c);
+ b = TREE_COLOR;
+ b = pixel_eq(a, TREE_COLOR);
+ pc.printf("a= %d",a);
+ pc.printf("b= %d",b);
+ while (b==1){
+ c=c-dy;
+ a=uLCD.read_pixel(x, c);
+ b = pixel_eq(a, TREE_COLOR);
+ pc.printf("loop:b=%d",b);
+ }
+ b = pixel_eq(a, LIGHTBLACK);
+ w= pixel_eq(a, YELLOW);
+ pc.printf("a2= %d",a);
+ pc.printf("b2:%d",b);
+ if (b==1||w==1){
+ goto Main;
+ }
+ a=uLCD.read_pixel(x, y-dy);
+ b = pixel_eq(a, BLACK);
+
+ uLCD.filled_rectangle(x-3,y-3,x+3,y+3, BLACK);
+ y=y-dy;
+ uLCD.filled_rectangle(x-3,y-3,x+3,y+3, STONE_COLOR);
+ pc.printf("C=%d\n",c);
+
+ if (b==0){
+ uLCD.filled_rectangle(x-3,c-3,x+3,c+3,TREE_COLOR);
+ pc.printf("b2=0 loop");
+ }
+ }
- led1 = p1_buttons[0] ^ p2_buttons[0];
- led2 = p1_buttons[1] ^ p2_buttons[1];
- led3 = p1_buttons[2] ^ p2_buttons[2];
- led4 = p1_buttons[3] ^ p2_buttons[3];
+ //Cat Code
+ z=rand()%8;
+ pc.printf("%d ",z);
+ wait(.005);
+ if (z==0 || z==4 || z==7){ //Move Left
+
+ cc=cx-dx;
+ a=uLCD.read_pixel(cc, cy);
+ NL = pixel_eq(a, BLACK);
+ if (NL==1){
+ uLCD.filled_rectangle(cx-3,cy-3,cx+3,cy+3, BLACK);
+ cx=cx-dx;
+ uLCD.filled_rectangle(cx-3,cy-3,cx+3,cy+3, YELLOW);
+ pc.printf("C=%d\n",cc);
+ }
+
+ t = pixel_eq(a, STONE_COLOR);
+
+ }
+ if (z==1 || z==5 ||z==6){ //Move Right
+ cc=cx+dx;
+ a=uLCD.read_pixel(cc, cy);
+ NR = pixel_eq(a, BLACK);
+ if (NR==1){
+ uLCD.filled_rectangle(cx-3,cy-3,cx+3,cy+3, BLACK);
+ cx=cx+dx;
+ uLCD.filled_rectangle(cx-3,cy-3,cx+3,cy+3, YELLOW);
+ pc.printf("C=%d\n",cc);
+ }
- // Get the accelerometer values.
- sync.get_p1_accel_data(&ax1, &ay1, &az1);
- sync.get_p2_accel_data(&ax2, &ay2, &az2);
+ t = pixel_eq(a, STONE_COLOR);
- if(whose_turn == PLAYER1) {
-
- // Accelerometer example
- if(ax1 > ACC_THRESHOLD) {
- // Move the tank to the right if the accelerometer is tipped far enough to the right.
- t1.reposition(+1, 0, 0);
+ }
+ if (z==2|| z==4 || z==5){ //Move up
+ cc=cy-dy;
+ a=uLCD.read_pixel(cx, cc);
+ NU = pixel_eq(a, BLACK);
+ if (NU==1){
+ uLCD.filled_rectangle(cx-3,cy-3,cx+3,cy+3, BLACK);
+ cy=cy-dy;
+ uLCD.filled_rectangle(cx-3,cy-3,cx+3,cy+3, YELLOW);
+ pc.printf("C=%d\n",cc);
+ }
+
+ t = pixel_eq(a, STONE_COLOR);
+
+ }
+ if (z==3 || z==6 || z==7){ //Move Down
+ cc=cy+dy;
+ a=uLCD.read_pixel(cx, cc);
+ ND = pixel_eq(a, BLACK);
+ if (ND==1){
+ uLCD.filled_rectangle(cx-3,cy-3,cx+3,cy+3, BLACK);
+ cy=cy+dy;
+ uLCD.filled_rectangle(cx-3,cy-3,cx+3,cy+3, YELLOW);
+ pc.printf("C=%d\n",cc);
+ }
+
+ t = pixel_eq(a, STONE_COLOR);
+
}
- // Button example
- if(p1_buttons[D_BUTTON]) {
- b1.shoot();
- }
-
- float dt = frame_timer.read();
- int intersection_code = b1.time_step(dt);
-
- if(intersection_code != BULLET_NO_COLLISION || intersection_code == BULLET_OFF_SCREEN) {
- pc.printf("Now it's P2's turn!\n");
- whose_turn = PLAYER2;
- }
-
- // If you shot yourself, you lost.
- if(sync.pixel_eq(intersection_code, t1.tank_color)) {
- sync.update(); // Is this necessary?
- winner = PLAYER2;
- break;
- }
-
- // If you shot the other guy, you won!
- if(sync.pixel_eq(intersection_code, t2.tank_color)) {
- sync.update();
- winner = PLAYER1;
- break;
- }
- } else if(whose_turn == PLAYER2) {
-
- // I gave you a lot of the logic for Player1. It's up to you to figure out Player2!
- // If you are in SINGLE_PLAYER mode, you should use the p1 inputs to control p2.
- // In MULTI_PLAYER mode, you should use the p2 inputs to control p2.
- //
- // Hint:
- // sync.play_mode will tell you if you are in MULTI_PLAYER or SINGLE_PLAYER mode.
- //
-
- }
-
- frame_timer.reset();
- sync.update();
- }
-
- game_over();
-
-}
\ No newline at end of file
+ //CHECK PICS AROUND CAT
+ N=uLCD.read_pixel(cx, cy+6);
+ N1 = pixel_eq(N, TREE_COLOR);
+ N2 = pixel_eq(N, LIGHTBLACK);
+ if (N1==1||N2==1){
+ NU=2;
+ }
+ S=uLCD.read_pixel(cx, cy-6);
+ S1 = pixel_eq(S, TREE_COLOR);
+ S2 = pixel_eq(S, LIGHTBLACK);
+ if (S1==1||S2==1){
+ ND=2;
+ }
+ E=uLCD.read_pixel(cx+6, cy);
+ E1 = pixel_eq(E, TREE_COLOR);
+ E2 = pixel_eq(E, LIGHTBLACK);
+ if (E1==1||E2==1){
+ NR=2;
+ }
+ W=uLCD.read_pixel(cx-6, cy);
+ W1 = pixel_eq(W, TREE_COLOR);
+ W2 = pixel_eq(W, LIGHTBLACK);
+ if (W1==1||W2==1){
+ NL=2;
+ }
+
+ pc.printf("NR=%d",NR);
+ pc.printf("NL=%d", NL);
+ pc.printf("NU=%d", NU);
+ pc.printf("ND=%d", ND);
+ if (NL==2 && NR==2 && NU==2 && ND==2){
+ uLCD.filled_rectangle(cx-3,cy-3,cx+3,cy+3, TANK_RED);
+ count++;
+ if (count==1){
+ wait(3);
+ uLCD.cls();
+ uLCD.locate(4,2);
+ uLCD.puts("Winner!!");
+ t=0;
+ wait(3);
+ while (1){
+ if (!pb_l){
+ goto Start;
+ }
+ }
+ }
+
+ }
+ }
+ }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/mpr121.cpp Tue Nov 01 18:59:29 2016 +0000
@@ -0,0 +1,221 @@
+/*
+Copyright (c) 2011 Anthony Buckton (abuckton [at] blackink [dot} net {dot} au)
+
+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 <mbed.h>
+#include <sstream>
+#include <string>
+#include <list>
+
+#include <mpr121.h>
+
+Mpr121::Mpr121(I2C *i2c, Address i2cAddress)
+{
+ this->i2c = i2c;
+
+ address = i2cAddress;
+
+ // Configure the MPR121 settings to default
+ this->configureSettings();
+}
+
+
+void Mpr121::configureSettings()
+{
+ // Put the MPR into setup mode
+ this->write(ELE_CFG,0x00);
+
+ // Electrode filters for when data is > baseline
+ unsigned char gtBaseline[] = {
+ 0x01, //MHD_R
+ 0x01, //NHD_R
+ 0x00, //NCL_R
+ 0x00 //FDL_R
+ };
+
+ writeMany(MHD_R,gtBaseline,4);
+
+ // Electrode filters for when data is < baseline
+ unsigned char ltBaseline[] = {
+ 0x01, //MHD_F
+ 0x01, //NHD_F
+ 0xFF, //NCL_F
+ 0x02 //FDL_F
+ };
+
+ writeMany(MHD_F,ltBaseline,4);
+
+ // Electrode touch and release thresholds
+ unsigned char electrodeThresholds[] = {
+ E_THR_T, // Touch Threshhold
+ E_THR_R // Release Threshold
+ };
+
+ for(int i=0; i<12; i++){
+ int result = writeMany((ELE0_T+(i*2)),electrodeThresholds,2);
+ }
+
+ // Proximity Settings
+ unsigned char proximitySettings[] = {
+ 0xff, //MHD_Prox_R
+ 0xff, //NHD_Prox_R
+ 0x00, //NCL_Prox_R
+ 0x00, //FDL_Prox_R
+ 0x01, //MHD_Prox_F
+ 0x01, //NHD_Prox_F
+ 0xFF, //NCL_Prox_F
+ 0xff, //FDL_Prox_F
+ 0x00, //NHD_Prox_T
+ 0x00, //NCL_Prox_T
+ 0x00 //NFD_Prox_T
+ };
+ writeMany(MHDPROXR,proximitySettings,11);
+
+ unsigned char proxThresh[] = {
+ PROX_THR_T, // Touch Threshold
+ PROX_THR_R // Release Threshold
+ };
+ writeMany(EPROXTTH,proxThresh,2);
+
+ this->write(FIL_CFG,0x04);
+
+ // Set the electrode config to transition to active mode
+ this->write(ELE_CFG,0x0c);
+}
+
+void Mpr121::setElectrodeThreshold(int electrode, unsigned char touch, unsigned char release){
+
+ if(electrode > 11) return;
+
+ // Get the current mode
+ unsigned char mode = this->read(ELE_CFG);
+
+ // Put the MPR into setup mode
+ this->write(ELE_CFG,0x00);
+
+ // Write the new threshold
+ this->write((ELE0_T+(electrode*2)), touch);
+ this->write((ELE0_T+(electrode*2)+1), release);
+
+ //Restore the operating mode
+ this->write(ELE_CFG, mode);
+}
+
+
+unsigned char Mpr121::read(int key){
+
+ unsigned char data[2];
+
+ //Start the command
+ i2c->start();
+
+ // Address the target (Write mode)
+ int ack1= i2c->write(address);
+
+ // Set the register key to read
+ int ack2 = i2c->write(key);
+
+ // Re-start for read of data
+ i2c->start();
+
+ // Re-send the target address in read mode
+ int ack3 = i2c->write(address+1);
+
+ // Read in the result
+ data[0] = i2c->read(0);
+
+ // Reset the bus
+ i2c->stop();
+
+ return data[0];
+}
+
+
+int Mpr121::write(int key, unsigned char value){
+
+ //Start the command
+ i2c->start();
+
+ // Address the target (Write mode)
+ int ack1= i2c->write(address);
+
+ // Set the register key to write
+ int ack2 = i2c->write(key);
+
+ // Read in the result
+ int ack3 = i2c->write(value);
+
+ // Reset the bus
+ i2c->stop();
+
+ return (ack1+ack2+ack3)-3;
+}
+
+
+int Mpr121::writeMany(int start, unsigned char* dataSet, int length){
+ //Start the command
+ i2c->start();
+
+ // Address the target (Write mode)
+ int ack= i2c->write(address);
+ if(ack!=1){
+ return -1;
+ }
+
+ // Set the register key to write
+ ack = i2c->write(start);
+ if(ack!=1){
+ return -1;
+ }
+
+ // Write the date set
+ int count = 0;
+ while(ack==1 && (count < length)){
+ ack = i2c->write(dataSet[count]);
+ count++;
+ }
+ // Stop the cmd
+ i2c->stop();
+
+ return count;
+}
+
+
+bool Mpr121::getProximityMode(){
+ if(this->read(ELE_CFG) > 0x0c)
+ return true;
+ else
+ return false;
+}
+
+void Mpr121::setProximityMode(bool mode){
+ this->write(ELE_CFG,0x00);
+ if(mode){
+ this->write(ELE_CFG,0x30); //Sense proximity from ALL pads
+ } else {
+ this->write(ELE_CFG,0x0c); //Sense touch, all 12 pads active.
+ }
+}
+
+
+int Mpr121::readTouchData(){
+ return this->read(0x00);
+}
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/mpr121.h Tue Nov 01 18:59:29 2016 +0000
@@ -0,0 +1,157 @@
+/*
+Copyright (c) 2011 Anthony Buckton (abuckton [at] blackink [dot} net {dot} au)
+
+
+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.
+
+ Parts written by Jim Lindblom of Sparkfun
+ Ported to mbed by A.Buckton, Feb 2011
+*/
+
+#ifndef MPR121_H
+#define MPR121_H
+
+//using namespace std;
+
+class Mpr121
+{
+
+public:
+ // i2c Addresses, bit-shifted
+ enum Address { ADD_VSS = 0xb4,// ADD->VSS = 0x5a <-wiring on Sparkfun board
+ ADD_VDD = 0xb6,// ADD->VDD = 0x5b
+ ADD_SCL = 0xb8,// ADD->SDA = 0x5c
+ ADD_SDA = 0xba // ADD->SCL = 0x5d
+ };
+
+ // Real initialiser, takes the i2c address of the device.
+ Mpr121(I2C *i2c, Address i2cAddress);
+
+ bool getProximityMode();
+
+ void setProximityMode(bool mode);
+
+ int readTouchData();
+
+ unsigned char read(int key);
+
+ int write(int address, unsigned char value);
+ int writeMany(int start, unsigned char* dataSet, int length);
+
+ void setElectrodeThreshold(int electrodeId, unsigned char touchThreshold, unsigned char releaseThreshold);
+
+protected:
+ // Configures the MPR with standard settings. This is permitted to be overwritten by sub-classes.
+ void configureSettings();
+
+private:
+ // The I2C bus instance.
+ I2C *i2c;
+
+ // i2c address of this mpr121
+ Address address;
+};
+
+
+// MPR121 Register Defines
+#define MHD_R 0x2B
+#define NHD_R 0x2C
+#define NCL_R 0x2D
+#define FDL_R 0x2E
+#define MHD_F 0x2F
+#define NHD_F 0x30
+#define NCL_F 0x31
+#define FDL_F 0x32
+#define NHDT 0x33
+#define NCLT 0x34
+#define FDLT 0x35
+// Proximity sensing controls
+#define MHDPROXR 0x36
+#define NHDPROXR 0x37
+#define NCLPROXR 0x38
+#define FDLPROXR 0x39
+#define MHDPROXF 0x3A
+#define NHDPROXF 0x3B
+#define NCLPROXF 0x3C
+#define FDLPROXF 0x3D
+#define NHDPROXT 0x3E
+#define NCLPROXT 0x3F
+#define FDLPROXT 0x40
+// Electrode Touch/Release thresholds
+#define ELE0_T 0x41
+#define ELE0_R 0x42
+#define ELE1_T 0x43
+#define ELE1_R 0x44
+#define ELE2_T 0x45
+#define ELE2_R 0x46
+#define ELE3_T 0x47
+#define ELE3_R 0x48
+#define ELE4_T 0x49
+#define ELE4_R 0x4A
+#define ELE5_T 0x4B
+#define ELE5_R 0x4C
+#define ELE6_T 0x4D
+#define ELE6_R 0x4E
+#define ELE7_T 0x4F
+#define ELE7_R 0x50
+#define ELE8_T 0x51
+#define ELE8_R 0x52
+#define ELE9_T 0x53
+#define ELE9_R 0x54
+#define ELE10_T 0x55
+#define ELE10_R 0x56
+#define ELE11_T 0x57
+#define ELE11_R 0x58
+// Proximity Touch/Release thresholds
+#define EPROXTTH 0x59
+#define EPROXRTH 0x5A
+// Debounce configuration
+#define DEB_CFG 0x5B
+// AFE- Analogue Front End configuration
+#define AFE_CFG 0x5C
+// Filter configuration
+#define FIL_CFG 0x5D
+// Electrode configuration - transistions to "active mode"
+#define ELE_CFG 0x5E
+
+#define GPIO_CTRL0 0x73
+#define GPIO_CTRL1 0x74
+#define GPIO_DATA 0x75
+#define GPIO_DIR 0x76
+#define GPIO_EN 0x77
+#define GPIO_SET 0x78
+#define GPIO_CLEAR 0x79
+#define GPIO_TOGGLE 0x7A
+// Auto configration registers
+#define AUTO_CFG_0 0x7B
+#define AUTO_CFG_U 0x7D
+#define AUTO_CFG_L 0x7E
+#define AUTO_CFG_T 0x7F
+
+// Threshold defaults
+// Electrode touch threshold
+#define E_THR_T 0x0F
+// Electrode release threshold
+#define E_THR_R 0x0A
+// Prox touch threshold
+#define PROX_THR_T 0x02
+// Prox release threshold
+#define PROX_THR_R 0x02
+
+#endif
