-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassignment2.cpp
executable file
·247 lines (204 loc) · 6.38 KB
/
assignment2.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/* CMPUT 296/114 - Assignment 2 - Due 2012-11-06
By: Ford, Adam
Lee, Austin
This assignment has been done under the full collaboration model,
and any extra resources are cited in the code below.
*/
#include <Adafruit_GFX.h> // Core graphics library
#include <Adafruit_ST7735.h> // Hardware-specific library
#include <SPI.h>
#include <SD.h>
#include "mem_syms.h"
#include "wiring_conventions.h"
#include "lcd_image.h"
#include "restaurant.h"
#include "joystick.h"
#include "conversion.h"
#include "distance.h"
#define RESTAURANTS_COUNT 1066
Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST);
Sd2Card card;
lcd_image_t map_image = { "yeg-sm.lcd", 128, 128 };
uint16_t verticalMidpoint;
/*
* Loads small Restaurant Distance structs for the restaurants
* that meet the minimum rating into a supplied array,
* and returns the numbers of restaurants loaded
*/
uint16_t loadDistances(uint16_t horiz, uint16_t vert, uint8_t minimumRating, RestDist *distances) {
uint16_t skippedRestaurants = 0;
uint16_t distancesIndex = 0;
for(int i = 0; i < RESTAURANTS_COUNT; i++) {
Restaurant rest;
getRestaurant(i, &rest, &card);
if(rest.rating >= minimumRating) {
uint16_t rest_horiz = lon_to_x(rest.longitude_scaled);
uint16_t rest_vert = lat_to_y(rest.latitude_scaled);
uint16_t distance = manhattanDist(horiz, vert, rest_horiz, rest_vert);
RestDist restDist = { i, distance };
distances[distancesIndex] = restDist;
distancesIndex++;
} else {
skippedRestaurants++;
}
}
return RESTAURANTS_COUNT - skippedRestaurants;
}
/*
* Sorts an array of RestDists by their distance field,
* in ascending order
*/
void sortDistances(RestDist *distances, uint16_t length) {
for(int i = 0; i < length - 1; i++) {
int16_t smallesti = i;
for(int j = i + 1; j < length; j++) {
if(distances[j].dist < distances[smallesti].dist) {
smallesti = j;
}
}
RestDist original = distances[i];
distances[i] = distances[smallesti];
distances[smallesti] = original;
}
}
/*
* Writes out at most 20 restaurants on the screen,
* starting at the index passed in
*/
void writeOutRestaurants(uint16_t startingIndex, RestDist *distances, uint16_t length) {
tft.fillScreen(0);
tft.setCursor(0, 0);
tft.setTextColor(0xFFFF);
tft.setTextWrap(false);
for (int i = startingIndex; i < 20 + startingIndex; i++) {
if(i >= length) {
break;
}
Restaurant r;
getRestaurant(distances[i].index, &r, &card);
tft.print(i + 1);
tft.print(". ");
tft.print(r.name);
tft.print("\n");
}
tft.print("\n");
}
/*
* Loads, sorts and displays the closest restaurants to location on map
* passed in, filtered by rating.
*/
void displayClosestRestaurants(uint16_t horiz, uint16_t vert, uint8_t minimumRating) {
RestDist distances[RESTAURANTS_COUNT];
uint16_t restaurantCount = loadDistances(horiz, vert, minimumRating, distances);
Serial.print("# of Restauraunts loaded: ");
Serial.println(restaurantCount);
sortDistances(distances, restaurantCount);
int16_t startingIndex = 0;
int8_t scrollScale = 10;
writeOutRestaurants(startingIndex, distances, restaurantCount);
// wait for button to be pressed
while(!isButtonPressed()) {
// following code implements scrolling up or down
// the list of restaurants loaded previously
uint16_t currentVertical = getVertical();
int16_t shiftValue = 0;
if(currentVertical > verticalMidpoint) {
shiftValue = scrollScale;
} else if (currentVertical < verticalMidpoint) {
shiftValue = -scrollScale;
}
if(shiftValue) {
uint16_t endIndex = startingIndex + 20;
// If we're scrolling up or we're not already at the end of the list,
// go ahead and scroll
if(shiftValue < 0 || endIndex < restaurantCount) {
startingIndex = startingIndex + shiftValue;
if(startingIndex < 0) {
// Don't want to scroll past the top!
startingIndex = 0;
} else {
// Don't want to scroll past the end!
if (startingIndex >= restaurantCount) {
startingIndex = restaurantCount - 1;
}
writeOutRestaurants((uint16_t) startingIndex, distances, restaurantCount);
}
}
}
delay(100);
}
// wait for button to be released
while(isButtonPressed()) { }
}
void drawMap() {
// clear to yellow
tft.fillScreen(tft.Color565(0xff, 0xff, 0x00));
lcd_image_draw(&map_image, &tft, 0, 0, 0, 0, 128, 128);
}
void setup(void) {
Serial.begin(9600);
pinMode(JOYSTICK_BUTTON, INPUT);
pinMode(RATING_LED_0, OUTPUT);
pinMode(RATING_LED_1, OUTPUT);
pinMode(RATING_LED_2, OUTPUT);
pinMode(RATING_LED_3, OUTPUT);
pinMode(RATING_LED_4, OUTPUT);
digitalWrite(JOYSTICK_BUTTON, HIGH);
tft.initR(INITR_REDTAB);
Serial.print("Initializing SD card...");
if (!SD.begin(SD_CS)) {
Serial.println("failed!");
return;
}
Serial.println("OK!");
drawMap();
// test out reading blocks from the SD card
if (!card.init(SPI_HALF_SPEED, SD_CS)) {
Serial.println("Raw SD Initialization has failed");
while (1) {}; // Just wait, stuff exploded.
}
verticalMidpoint = getVertical();
}
// Maps the value from the potentiometer to a value
// between 0 to 5 to determine how many LEDs should
// be lit up
uint8_t getRatingFilter() {
uint16_t input = analogRead(RATING_DIAL);
return map(input, 0, 1023, 0, 5);
}
void lightUpRatingLEDs(uint8_t ratingFilter) {
digitalWrite(RATING_LED_0, LOW);
digitalWrite(RATING_LED_1, LOW);
digitalWrite(RATING_LED_2, LOW);
digitalWrite(RATING_LED_3, LOW);
digitalWrite(RATING_LED_4, LOW);
if(ratingFilter >= 1) {
digitalWrite(RATING_LED_0, HIGH);
}
if(ratingFilter >= 2) {
digitalWrite(RATING_LED_1, HIGH);
}
if(ratingFilter >= 3) {
digitalWrite(RATING_LED_2, HIGH);
}
if(ratingFilter >= 4) {
digitalWrite(RATING_LED_3, HIGH);
}
if(ratingFilter >= 5) {
digitalWrite(RATING_LED_4, HIGH);
}
}
void loop() {
uint8_t ratingFilter = getRatingFilter();
uint16_t horiz = getHorizontal();
uint16_t vert = getVertical();
lightUpRatingLEDs(ratingFilter);
tft.drawPixel(horiz, vert, 0);
if(isButtonPressed()) {
// wait for button to be released
while(isButtonPressed()) { }
displayClosestRestaurants(horiz, vert, ratingFilter * 2);
drawMap();
}
lcd_image_draw(&map_image, &tft, horiz, vert, horiz, vert, 1, 1);
}