Final project repo for ECE 495

Dependencies:   Adafruit_GFX_MBED Adafruit_ILI9341 BurstSPI DS1820 mbed mbed-rtos ltc2991_lib

Committer:
bdk9
Date:
Thu Dec 08 20:00:41 2016 +0000
Revision:
2:0a07f99e32c9
Parent:
0:7ba4e0775670
Child:
3:a1b5d7541c69
demo prep

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bdk9 0:7ba4e0775670 1
bdk9 0:7ba4e0775670 2 #include "mbed.h"
bdk9 0:7ba4e0775670 3 #include "rtos.h"
bdk9 0:7ba4e0775670 4 #include "DS1820.h"
bdk9 0:7ba4e0775670 5 #include "TemperatureScreen.h"
bdk9 0:7ba4e0775670 6 #include "VoltageScreen.h"
bdk9 0:7ba4e0775670 7 #include "CurrentScreen.h"
bdk9 0:7ba4e0775670 8 #include "Display.h"
bdk9 0:7ba4e0775670 9
bdk9 2:0a07f99e32c9 10 #define NUM_DS1820 1
bdk9 2:0a07f99e32c9 11 #define PIN_DS1820 PC_0
bdk9 0:7ba4e0775670 12
bdk9 0:7ba4e0775670 13 // DEVICES
bdk9 0:7ba4e0775670 14 DS1820* thermometers[NUM_DS1820];
bdk9 2:0a07f99e32c9 15 Adafruit_ILI9341 tft(PA_13, PA_15, PA_14);
bdk9 0:7ba4e0775670 16 Display disp;
bdk9 0:7ba4e0775670 17
bdk9 0:7ba4e0775670 18 // IO
bdk9 0:7ba4e0775670 19 RawSerial pc(USBTX, USBRX);
bdk9 0:7ba4e0775670 20 DigitalOut led(LED1);
bdk9 0:7ba4e0775670 21 InterruptIn display_interrupt(PC_13);
bdk9 0:7ba4e0775670 22
bdk9 0:7ba4e0775670 23 // THREADS
bdk9 0:7ba4e0775670 24 Thread display_thread(osPriorityNormal, DEFAULT_STACK_SIZE, NULL);
bdk9 0:7ba4e0775670 25 Thread slow_data_thread(osPriorityNormal, DEFAULT_STACK_SIZE, NULL);
bdk9 0:7ba4e0775670 26
bdk9 0:7ba4e0775670 27 // DATA VARIABLES
bdk9 0:7ba4e0775670 28 double temperatures[NUM_DS1820];
bdk9 0:7ba4e0775670 29 double old_temperatures[NUM_DS1820];
bdk9 0:7ba4e0775670 30
bdk9 0:7ba4e0775670 31 // DRAWING VARIABLES
bdk9 0:7ba4e0775670 32 int margin;
bdk9 0:7ba4e0775670 33 int axes_x_cur;
bdk9 0:7ba4e0775670 34 int axes_x_min, axes_x_max;
bdk9 0:7ba4e0775670 35 int axes_y_bot, axes_y_top;
bdk9 0:7ba4e0775670 36 int temp_colors[3];
bdk9 0:7ba4e0775670 37 int temp_label_y_pos[3];
bdk9 0:7ba4e0775670 38 int temp_str_y_pos[3];
bdk9 0:7ba4e0775670 39 bool plotFlag;
bdk9 0:7ba4e0775670 40
bdk9 0:7ba4e0775670 41
bdk9 0:7ba4e0775670 42 // FUNCTION DECLARATIONS
bdk9 0:7ba4e0775670 43 int main();
bdk9 0:7ba4e0775670 44
bdk9 0:7ba4e0775670 45 // setup
bdk9 0:7ba4e0775670 46 void ds1820_init();
bdk9 0:7ba4e0775670 47 // thread drivers
bdk9 0:7ba4e0775670 48 void display_cycle();
bdk9 0:7ba4e0775670 49 void slow_data_task();
bdk9 2:0a07f99e32c9 50 void fast_data_task();
bdk9 0:7ba4e0775670 51 // data read functions
bdk9 0:7ba4e0775670 52 void read_temps();
bdk9 0:7ba4e0775670 53 // ISRs
bdk9 0:7ba4e0775670 54 void display_swap();
bdk9 0:7ba4e0775670 55 void parse_command();
bdk9 0:7ba4e0775670 56 // helpers
bdk9 0:7ba4e0775670 57 int scale_temp(double val, int min_domain, int max_domain, int min_range, int max_range);
bdk9 0:7ba4e0775670 58 char *int2bin(int x);
bdk9 0:7ba4e0775670 59
bdk9 0:7ba4e0775670 60 void display_swap() {
bdk9 0:7ba4e0775670 61 if (display_interrupt)
bdk9 0:7ba4e0775670 62 disp.next_screen();
bdk9 0:7ba4e0775670 63 }
bdk9 0:7ba4e0775670 64
bdk9 0:7ba4e0775670 65 void display_cycle() {
bdk9 0:7ba4e0775670 66 while(1) {
bdk9 0:7ba4e0775670 67 disp.update();
bdk9 0:7ba4e0775670 68 Thread::wait(1000);
bdk9 0:7ba4e0775670 69 }
bdk9 0:7ba4e0775670 70 }
bdk9 0:7ba4e0775670 71
bdk9 0:7ba4e0775670 72 void read_temps() {
bdk9 0:7ba4e0775670 73 double temp;
bdk9 0:7ba4e0775670 74 thermometers[0]->convertTemperature(false, DS1820::all_devices);
bdk9 0:7ba4e0775670 75 for (int i=0; i<NUM_DS1820; i++) {
bdk9 0:7ba4e0775670 76 temp = (double) thermometers[i]->temperature();
bdk9 0:7ba4e0775670 77 if (temp > 0 && temp < 150) {
bdk9 0:7ba4e0775670 78 old_temperatures[i] = temperatures[i];
bdk9 0:7ba4e0775670 79 temperatures[i] = temp;
bdk9 0:7ba4e0775670 80 }
bdk9 0:7ba4e0775670 81 }
bdk9 0:7ba4e0775670 82 }
bdk9 0:7ba4e0775670 83
bdk9 0:7ba4e0775670 84 void slow_data_task() {
bdk9 0:7ba4e0775670 85 //get initial reading
bdk9 0:7ba4e0775670 86 read_temps();
bdk9 0:7ba4e0775670 87
bdk9 0:7ba4e0775670 88 while(1) {
bdk9 0:7ba4e0775670 89 read_temps();
bdk9 0:7ba4e0775670 90 Thread::wait(500);
bdk9 0:7ba4e0775670 91 }
bdk9 0:7ba4e0775670 92 }
bdk9 0:7ba4e0775670 93
bdk9 0:7ba4e0775670 94 // Discover DS1820 probes on pin defined by PIN_DS1820
bdk9 0:7ba4e0775670 95 void ds1820_init() {
bdk9 0:7ba4e0775670 96 // Initialize the thermometer array to DS1820 objects
bdk9 0:7ba4e0775670 97 int num_devices = 0;
bdk9 0:7ba4e0775670 98 while(DS1820::unassignedProbe(PIN_DS1820)) {
bdk9 0:7ba4e0775670 99 thermometers[num_devices] = new DS1820(PIN_DS1820);
bdk9 0:7ba4e0775670 100 num_devices++;
bdk9 0:7ba4e0775670 101 if (num_devices == NUM_DS1820)
bdk9 0:7ba4e0775670 102 break;
bdk9 0:7ba4e0775670 103 }
bdk9 0:7ba4e0775670 104 pc.printf("Found %d device(s)\r\n\n", num_devices);
bdk9 0:7ba4e0775670 105 }
bdk9 0:7ba4e0775670 106
bdk9 0:7ba4e0775670 107 char *int2bin(int x) {
bdk9 0:7ba4e0775670 108 static char b[33];
bdk9 0:7ba4e0775670 109 b[0] = '\0';
bdk9 0:7ba4e0775670 110 uint32_t z;
bdk9 0:7ba4e0775670 111 for (z = 2147483648; z > 0; z >>= 1) {
bdk9 0:7ba4e0775670 112 strcat(b, ((x & z) == z) ? "1" : "0");
bdk9 0:7ba4e0775670 113 }
bdk9 0:7ba4e0775670 114 return b;
bdk9 0:7ba4e0775670 115 }
bdk9 0:7ba4e0775670 116
bdk9 2:0a07f99e32c9 117 char buff[64];
bdk9 0:7ba4e0775670 118 int loc = 0;
bdk9 0:7ba4e0775670 119
bdk9 0:7ba4e0775670 120 void execute_command(char *cmd) {
bdk9 0:7ba4e0775670 121 pc.printf("%s\n", cmd);
bdk9 0:7ba4e0775670 122 }
bdk9 0:7ba4e0775670 123
bdk9 0:7ba4e0775670 124 void parse_command() {
bdk9 0:7ba4e0775670 125 buff[loc] = USART2->DR;
bdk9 0:7ba4e0775670 126 loc += 1;
bdk9 2:0a07f99e32c9 127 if (buff[loc-1] == '\n') {
bdk9 0:7ba4e0775670 128 execute_command(buff);
bdk9 2:0a07f99e32c9 129 memset(&buff[0], 0, sizeof(buff));
bdk9 0:7ba4e0775670 130 loc = 0;
bdk9 0:7ba4e0775670 131 }
bdk9 0:7ba4e0775670 132 }
bdk9 0:7ba4e0775670 133
bdk9 0:7ba4e0775670 134
bdk9 0:7ba4e0775670 135 int main() {
bdk9 2:0a07f99e32c9 136
bdk9 2:0a07f99e32c9 137 tft.begin();
bdk9 2:0a07f99e32c9 138 tft.fillScreen(RED);
bdk9 2:0a07f99e32c9 139 wait_ms(1000);
bdk9 2:0a07f99e32c9 140 tft.fillScreen(BLACK);
bdk9 0:7ba4e0775670 141 // Setup serial interrupts, temperature sensors
bdk9 0:7ba4e0775670 142 pc.attach(&parse_command);
bdk9 0:7ba4e0775670 143 ds1820_init();
bdk9 0:7ba4e0775670 144
bdk9 0:7ba4e0775670 145 // initialize display
bdk9 0:7ba4e0775670 146 tft.begin();
bdk9 0:7ba4e0775670 147 tft.fillScreen(BLACK);
bdk9 0:7ba4e0775670 148 tft.setRotation(1);
bdk9 0:7ba4e0775670 149
bdk9 0:7ba4e0775670 150 // setup display screens
bdk9 0:7ba4e0775670 151 TemperatureScreen ts(0, &tft);
bdk9 0:7ba4e0775670 152 VoltageScreen vs(1, &tft);
bdk9 0:7ba4e0775670 153 CurrentScreen cs(2, &tft);
bdk9 0:7ba4e0775670 154 Screen *s[3] = {&ts, &vs, &cs};
bdk9 0:7ba4e0775670 155 disp.set_screens(s, 3);
bdk9 0:7ba4e0775670 156
bdk9 0:7ba4e0775670 157 // setup display switcher
bdk9 0:7ba4e0775670 158 display_interrupt.rise(&display_swap);
bdk9 0:7ba4e0775670 159
bdk9 0:7ba4e0775670 160 // Kick off our threads
bdk9 0:7ba4e0775670 161 slow_data_thread.start(slow_data_task);
bdk9 0:7ba4e0775670 162 wait_ms(1000);
bdk9 0:7ba4e0775670 163 display_thread.start(display_cycle);
bdk9 0:7ba4e0775670 164 }
bdk9 0:7ba4e0775670 165
bdk9 0:7ba4e0775670 166
bdk9 0:7ba4e0775670 167
bdk9 0:7ba4e0775670 168
bdk9 0:7ba4e0775670 169
bdk9 0:7ba4e0775670 170
bdk9 0:7ba4e0775670 171
bdk9 0:7ba4e0775670 172 // TEMPERATURE DISPLAY
bdk9 0:7ba4e0775670 173 TemperatureScreen::TemperatureScreen(int id, Adafruit_ILI9341 *tft) : Screen(id, tft) { }
bdk9 0:7ba4e0775670 174
bdk9 0:7ba4e0775670 175 void TemperatureScreen::init() {
bdk9 0:7ba4e0775670 176 margin = 10;
bdk9 0:7ba4e0775670 177
bdk9 0:7ba4e0775670 178 axes_x_min = 120;
bdk9 0:7ba4e0775670 179 axes_x_max = _tft->width() - margin;
bdk9 0:7ba4e0775670 180 axes_y_bot = _tft->height() - margin;
bdk9 0:7ba4e0775670 181 axes_y_top = 40;
bdk9 0:7ba4e0775670 182
bdk9 0:7ba4e0775670 183 temp_colors[0] = CYAN;
bdk9 0:7ba4e0775670 184 temp_colors[1] = YELLOW;
bdk9 0:7ba4e0775670 185 temp_colors[2] = GREEN;
bdk9 0:7ba4e0775670 186 temp_label_y_pos[0] = margin+9;
bdk9 0:7ba4e0775670 187 temp_label_y_pos[1] = margin+88;
bdk9 0:7ba4e0775670 188 temp_label_y_pos[2] = margin+168;
bdk9 0:7ba4e0775670 189 temp_str_y_pos[0] = margin+40;
bdk9 0:7ba4e0775670 190 temp_str_y_pos[1] = margin+120;
bdk9 0:7ba4e0775670 191 temp_str_y_pos[2] = margin+200;
bdk9 0:7ba4e0775670 192
bdk9 0:7ba4e0775670 193 // draw background
bdk9 0:7ba4e0775670 194 _tft->fillScreen(BLACK);
bdk9 0:7ba4e0775670 195 _tft->setTextSize(2);
bdk9 0:7ba4e0775670 196 _tft->setTextColor(WHITE);
bdk9 0:7ba4e0775670 197 _tft->setCursor(155, margin);
bdk9 0:7ba4e0775670 198 _tft->print("TEMPERATURE");
bdk9 0:7ba4e0775670 199
bdk9 0:7ba4e0775670 200 _tft->fillRect(margin, margin, axes_x_min-2*margin, 30, temp_colors[0]);
bdk9 0:7ba4e0775670 201 _tft->setCursor(margin+33, temp_label_y_pos[0]);
bdk9 0:7ba4e0775670 202 _tft->setTextColor(BLACK);
bdk9 0:7ba4e0775670 203 _tft->print("CPU");
bdk9 0:7ba4e0775670 204
bdk9 0:7ba4e0775670 205 _tft->fillRect(margin, margin+80, axes_x_min-2*margin, 30, temp_colors[1]);
bdk9 0:7ba4e0775670 206 _tft->setCursor(margin+20, temp_label_y_pos[1]);
bdk9 0:7ba4e0775670 207 _tft->setTextColor(BLACK);
bdk9 0:7ba4e0775670 208 _tft->print("MOTOR");
bdk9 0:7ba4e0775670 209
bdk9 0:7ba4e0775670 210 _tft->fillRect(margin, margin+160, axes_x_min-2*margin, 30, temp_colors[2]);
bdk9 0:7ba4e0775670 211 _tft->setCursor(margin+10, temp_label_y_pos[2]);
bdk9 0:7ba4e0775670 212 _tft->setTextColor(BLACK);
bdk9 0:7ba4e0775670 213 _tft->print("AMBIENT");
bdk9 0:7ba4e0775670 214 _tft->setTextSize(2);
bdk9 0:7ba4e0775670 215
bdk9 0:7ba4e0775670 216 // x-axis
bdk9 0:7ba4e0775670 217 _tft->drawFastHLine(axes_x_min, axes_y_bot, axes_x_max-axes_x_min, WHITE);
bdk9 0:7ba4e0775670 218 // y-axis
bdk9 0:7ba4e0775670 219 _tft->drawFastVLine(axes_x_min, axes_y_top, axes_y_bot-axes_y_top, WHITE);
bdk9 0:7ba4e0775670 220
bdk9 0:7ba4e0775670 221 axes_x_min += 1;
bdk9 0:7ba4e0775670 222 axes_x_max -= 1;
bdk9 0:7ba4e0775670 223 axes_y_bot -= 1;
bdk9 0:7ba4e0775670 224 axes_y_top += 1;
bdk9 0:7ba4e0775670 225 axes_x_cur = axes_x_min;
bdk9 0:7ba4e0775670 226 }
bdk9 0:7ba4e0775670 227
bdk9 0:7ba4e0775670 228 int scale_temp(double val, int min_domain, int max_domain, int min_range, int max_range) {
bdk9 0:7ba4e0775670 229 return (int) max_range - ((val - (min_domain)) / (max_domain - min_domain)) * (max_range - min_range);
bdk9 0:7ba4e0775670 230 }
bdk9 0:7ba4e0775670 231
bdk9 0:7ba4e0775670 232 void TemperatureScreen::update() {
bdk9 0:7ba4e0775670 233 if (plotFlag) {
bdk9 0:7ba4e0775670 234 _tft->fillRect(axes_x_min, axes_y_top, _tft->width()-axes_x_min, axes_y_bot-axes_y_top, BLACK);
bdk9 0:7ba4e0775670 235 plotFlag = false;
bdk9 0:7ba4e0775670 236 }
bdk9 0:7ba4e0775670 237 int y_new, y_old;
bdk9 0:7ba4e0775670 238 for (int i=0; i<NUM_DS1820; i++) {
bdk9 0:7ba4e0775670 239 _tft->fillRect(margin, temp_str_y_pos[i], axes_x_min-2*margin, 30, BLACK);
bdk9 0:7ba4e0775670 240 char str[10];
bdk9 0:7ba4e0775670 241 sprintf(str, "%.2f C", temperatures[i]);
bdk9 0:7ba4e0775670 242 _tft->setCursor(margin, temp_str_y_pos[i]);
bdk9 0:7ba4e0775670 243 _tft->setTextColor(temp_colors[i]);
bdk9 0:7ba4e0775670 244 _tft->print(str);
bdk9 0:7ba4e0775670 245 y_new = scale_temp(temperatures[i], 0, 150, axes_y_top, axes_y_bot);
bdk9 0:7ba4e0775670 246 y_old = scale_temp(old_temperatures[i], 0, 150, axes_y_top, axes_y_bot);
bdk9 0:7ba4e0775670 247 _tft->drawLine(axes_x_cur, y_old, axes_x_cur+3, y_new, temp_colors[i]);
bdk9 0:7ba4e0775670 248 }
bdk9 0:7ba4e0775670 249 axes_x_cur += 3;
bdk9 0:7ba4e0775670 250 if (axes_x_cur >= axes_x_max) {
bdk9 0:7ba4e0775670 251 plotFlag = true;
bdk9 0:7ba4e0775670 252 axes_x_cur = axes_x_min;
bdk9 0:7ba4e0775670 253 }
bdk9 0:7ba4e0775670 254 }
bdk9 0:7ba4e0775670 255
bdk9 0:7ba4e0775670 256
bdk9 0:7ba4e0775670 257
bdk9 0:7ba4e0775670 258 // VOLTAGE DISPLAY
bdk9 0:7ba4e0775670 259 VoltageScreen::VoltageScreen(int id, Adafruit_ILI9341 *tft) : Screen(id, tft) { }
bdk9 0:7ba4e0775670 260
bdk9 0:7ba4e0775670 261 void VoltageScreen::init() {
bdk9 0:7ba4e0775670 262 _tft->fillScreen(RED);
bdk9 0:7ba4e0775670 263 }
bdk9 0:7ba4e0775670 264
bdk9 0:7ba4e0775670 265 void VoltageScreen::update() {
bdk9 0:7ba4e0775670 266 }
bdk9 0:7ba4e0775670 267
bdk9 0:7ba4e0775670 268
bdk9 0:7ba4e0775670 269
bdk9 0:7ba4e0775670 270
bdk9 0:7ba4e0775670 271 // CURRENT DISPLAY
bdk9 0:7ba4e0775670 272 CurrentScreen::CurrentScreen(int id, Adafruit_ILI9341 *tft) : Screen(id, tft) { }
bdk9 0:7ba4e0775670 273
bdk9 0:7ba4e0775670 274 void CurrentScreen::init() {
bdk9 0:7ba4e0775670 275 _tft->fillScreen(BLUE);
bdk9 0:7ba4e0775670 276 }
bdk9 0:7ba4e0775670 277
bdk9 0:7ba4e0775670 278 void CurrentScreen::update() {
bdk9 0:7ba4e0775670 279 }