Faizan Khan / Mbed 2 deprecated 4180-lab4

Dependencies:   4DGL-uLCD-SE mbed SDFileSystem wave_player

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers stack.cpp Source File

stack.cpp

00001 #include "mbed.h"
00002 #include "uLCD_4DGL.h"
00003 #include "stack.h"
00004 #include "food.h"
00005 #include <algorithm>
00006 #include <vector>
00007 
00008 #define BREAD 1
00009 #define LETTUCE 2
00010 #define CHEESE 3
00011 #define TOMATO 4
00012 #define BADLETTUCE 5
00013 #define BADCHEESE 6
00014 #define BADTOMATO 7
00015 
00016 #define MAX(i) std::max(i,0)
00017 #define MIN(i) std::min(i,107)
00018 
00019 
00020 Stack::Stack(int location, uLCD_4DGL * uLCD) {
00021     x = location;
00022     Food bread(uLCD);
00023     stackOfFood.push_back(bread);
00024     lcd = uLCD;
00025     top = 122;
00026 }
00027 
00028 void Stack::move(int dx) {
00029     // constrain 
00030     erase();
00031     if (dx < 0) x = MAX(x + dx);
00032     else x = MIN(x + dx);
00033     draw();
00034 }
00035 
00036 int Stack::size() {
00037     return stackOfFood.size();
00038 }
00039 
00040 void Stack::clear() {
00041     erase();
00042     stackOfFood.clear();
00043     Food bread(lcd);
00044     stackOfFood.push_back(bread);
00045     draw(); 
00046 }
00047 
00048 void Stack::erase() {
00049     // ERASE
00050     lcd->filled_rectangle(x, 125, x + 15, top, BLACK);
00051     lcd->filled_rectangle(x - 1, 126, x + 17, 127, BLACK);
00052 }
00053 
00054 void Stack::draw() {
00055     // DRAW (loop thru food vector)
00056     lcd->filled_rectangle(x - 1, 126, x + 17, 127, WHITE);
00057     int j = 122;
00058     for (int i = 0; i < stackOfFood.size(); i++) {
00059         stackOfFood[i].x = x;
00060         stackOfFood[i].y = j;
00061         stackOfFood[i].draw();
00062         j -= 4;
00063     }
00064 }
00065 
00066 void Stack::add(Food * collided) {
00067     collided->x = x;
00068     collided->y = top;
00069     top -= 4;
00070     stackOfFood.push_back(*collided);
00071     draw();
00072 }