Dependencies:   KS0108 mbed

Committer:
kagelump
Date:
Mon Jun 18 16:47:00 2012 +0000
Revision:
0:b669c97da2c0

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kagelump 0:b669c97da2c0 1 // Display Test
kagelump 0:b669c97da2c0 2 #include "KS0108.h"
kagelump 0:b669c97da2c0 3 #include "calsolimage.h"
kagelump 0:b669c97da2c0 4 #include "MyPhone_20.h"
kagelump 0:b669c97da2c0 5 #include "MyPhone_40.h"
kagelump 0:b669c97da2c0 6 #include "rtos.h"
kagelump 0:b669c97da2c0 7
kagelump 0:b669c97da2c0 8 // Declare each of the things you're going to use
kagelump 0:b669c97da2c0 9
kagelump 0:b669c97da2c0 10 // The display
kagelump 0:b669c97da2c0 11 KS0108 display(p23,p21,p22,p20,p24,p25,p17,p18,p19,p30,p29,p28,p27,p26);
kagelump 0:b669c97da2c0 12 // The button on the breadboard
kagelump 0:b669c97da2c0 13 DigitalIn topButton(p13);
kagelump 0:b669c97da2c0 14 // For printing things to the computer
kagelump 0:b669c97da2c0 15 Serial pc(USBTX,USBRX);
kagelump 0:b669c97da2c0 16
kagelump 0:b669c97da2c0 17 // Timers
kagelump 0:b669c97da2c0 18 Timer timer;
kagelump 0:b669c97da2c0 19 Timer testTimer;
kagelump 0:b669c97da2c0 20
kagelump 0:b669c97da2c0 21 //Methods
kagelump 0:b669c97da2c0 22 void readFromCAN();
kagelump 0:b669c97da2c0 23 void setupScreen();
kagelump 0:b669c97da2c0 24 void updateBattery(int newVoltage);
kagelump 0:b669c97da2c0 25 void updateBatteryGraphic(int newPercentage, int percentage);
kagelump 0:b669c97da2c0 26 void printCenteredInt(int newInt, int oldInt, int startX, int startY, int charWidth, int charHeight, int charSpacing, int maxDigits);
kagelump 0:b669c97da2c0 27 void printCenteredInt(int newInt, int oldInt, int leftLimit, int rightLimit, int startY, int charWidth, int charHeight, int charSpacing, int maxDigits);
kagelump 0:b669c97da2c0 28 void printRightJustifiedInt(int newInt, int oldInt, int startX, int startY, int charWidth, int charHeight, int charSpacing, int maxDigits);
kagelump 0:b669c97da2c0 29
kagelump 0:b669c97da2c0 30 // Constants
kagelump 0:b669c97da2c0 31
kagelump 0:b669c97da2c0 32 //Maximum voltage for battery = 151.2 V (4.2V per cell, 36 cells total)
kagelump 0:b669c97da2c0 33 const double MaxVoltage = 151.2;
kagelump 0:b669c97da2c0 34 //Minimum voltage for battery = 108 V (3V per cell, 36 cells total)
kagelump 0:b669c97da2c0 35 const double MinVoltage = 108;
kagelump 0:b669c97da2c0 36
kagelump 0:b669c97da2c0 37 // Mutex and screen state
kagelump 0:b669c97da2c0 38 Mutex screen_mutex;
kagelump 0:b669c97da2c0 39 CAN can(p9, p10);
kagelump 0:b669c97da2c0 40 int demo = 0;
kagelump 0:b669c97da2c0 41
kagelump 0:b669c97da2c0 42
kagelump 0:b669c97da2c0 43 //Speed: int from 0 to 999, in mph
kagelump 0:b669c97da2c0 44 int speed = 0, newSpeed = 0;
kagelump 0:b669c97da2c0 45 //Voltage: int from MinVoltage to MaxVoltage, in volts
kagelump 0:b669c97da2c0 46 int voltage = MinVoltage, newVoltage = MinVoltage;
kagelump 0:b669c97da2c0 47 //Current: int from 0 to 9999, in milliAmps
kagelump 0:b669c97da2c0 48 int amperage = 0, newAmperage = 0;
kagelump 0:b669c97da2c0 49
kagelump 0:b669c97da2c0 50
kagelump 0:b669c97da2c0 51 //Union type to make float to char conversion easier (used to read CAN messages)
kagelump 0:b669c97da2c0 52 typedef union _data
kagelump 0:b669c97da2c0 53 {
kagelump 0:b669c97da2c0 54 float f[2];
kagelump 0:b669c97da2c0 55 char s[8];
kagelump 0:b669c97da2c0 56 } floatchar;
kagelump 0:b669c97da2c0 57
kagelump 0:b669c97da2c0 58 //Union type to make int to char conversion easier (used to read CAN messages)
kagelump 0:b669c97da2c0 59 typedef union _data2
kagelump 0:b669c97da2c0 60 {
kagelump 0:b669c97da2c0 61 uint16_t i[4];
kagelump 0:b669c97da2c0 62 char s[8];
kagelump 0:b669c97da2c0 63 } intchar;
kagelump 0:b669c97da2c0 64
kagelump 0:b669c97da2c0 65
kagelump 0:b669c97da2c0 66 /* Input thread - Modifies state variables based on input */
kagelump 0:b669c97da2c0 67 void input_runner(void const *argument) {
kagelump 0:b669c97da2c0 68 while (true) {
kagelump 0:b669c97da2c0 69 // If top button is pressed, don't accept other input
kagelump 0:b669c97da2c0 70 if (!topButton) {
kagelump 0:b669c97da2c0 71 // Debounce wait
kagelump 0:b669c97da2c0 72 Thread::wait(100);
kagelump 0:b669c97da2c0 73 if (!topButton) {
kagelump 0:b669c97da2c0 74 screen_mutex.lock();
kagelump 0:b669c97da2c0 75 demo++;
kagelump 0:b669c97da2c0 76 if (demo > 1) {
kagelump 0:b669c97da2c0 77 demo = 0;
kagelump 0:b669c97da2c0 78 }
kagelump 0:b669c97da2c0 79 screen_mutex.unlock();
kagelump 0:b669c97da2c0 80 }
kagelump 0:b669c97da2c0 81 // Don't accept other input while button is pressed
kagelump 0:b669c97da2c0 82 while(!topButton) {
kagelump 0:b669c97da2c0 83 Thread::wait(100);
kagelump 0:b669c97da2c0 84 }
kagelump 0:b669c97da2c0 85 }
kagelump 0:b669c97da2c0 86 }
kagelump 0:b669c97da2c0 87 }
kagelump 0:b669c97da2c0 88
kagelump 0:b669c97da2c0 89
kagelump 0:b669c97da2c0 90 // This is where your programs starts running
kagelump 0:b669c97da2c0 91 int main()
kagelump 0:b669c97da2c0 92 {
kagelump 0:b669c97da2c0 93 // Initialize variables**
kagelump 0:b669c97da2c0 94 int current_demo = demo;
kagelump 0:b669c97da2c0 95 topButton.mode(PullUp);
kagelump 0:b669c97da2c0 96 // Display CalSol logo
kagelump 0:b669c97da2c0 97 display.FullScreenBMP(pic);
kagelump 0:b669c97da2c0 98 Thread::wait(200);
kagelump 0:b669c97da2c0 99
kagelump 0:b669c97da2c0 100 timer.start();
kagelump 0:b669c97da2c0 101
kagelump 0:b669c97da2c0 102 // Start the button thread
kagelump 0:b669c97da2c0 103 Thread button_thread(input_runner);
kagelump 0:b669c97da2c0 104
kagelump 0:b669c97da2c0 105 // Main thread
kagelump 0:b669c97da2c0 106 while(1) {
kagelump 0:b669c97da2c0 107 // First, check if the state has changed, if so, wipe the screen
kagelump 0:b669c97da2c0 108 int changed = 0;
kagelump 0:b669c97da2c0 109 screen_mutex.lock();
kagelump 0:b669c97da2c0 110 if (current_demo != demo)
kagelump 0:b669c97da2c0 111 {
kagelump 0:b669c97da2c0 112 changed = 1;
kagelump 0:b669c97da2c0 113 }
kagelump 0:b669c97da2c0 114 current_demo = demo;
kagelump 0:b669c97da2c0 115 screen_mutex.unlock();
kagelump 0:b669c97da2c0 116
kagelump 0:b669c97da2c0 117 // Wipe the screen
kagelump 0:b669c97da2c0 118 if (changed)
kagelump 0:b669c97da2c0 119 {
kagelump 0:b669c97da2c0 120 display.ClearScreen();
kagelump 0:b669c97da2c0 121 changed = 0;
kagelump 0:b669c97da2c0 122 }
kagelump 0:b669c97da2c0 123
kagelump 0:b669c97da2c0 124 // Now run the actual program
kagelump 0:b669c97da2c0 125 switch(current_demo)
kagelump 0:b669c97da2c0 126 {
kagelump 0:b669c97da2c0 127 // Demo 0: Just display the CalSol logo
kagelump 0:b669c97da2c0 128 case 0:
kagelump 0:b669c97da2c0 129 if(timer.read_ms() > 500)
kagelump 0:b669c97da2c0 130 {
kagelump 0:b669c97da2c0 131 display.FullScreenBMP(pic);
kagelump 0:b669c97da2c0 132 timer.reset();
kagelump 0:b669c97da2c0 133 }
kagelump 0:b669c97da2c0 134 break;
kagelump 0:b669c97da2c0 135
kagelump 0:b669c97da2c0 136 // Demo 1: Display a mock speedometer, with battery level
kagelump 0:b669c97da2c0 137 case 1:
kagelump 0:b669c97da2c0 138 //Initialize variables
kagelump 0:b669c97da2c0 139 speed = newSpeed = 60;
kagelump 0:b669c97da2c0 140 amperage = newAmperage = 900;
kagelump 0:b669c97da2c0 141 voltage = newVoltage = MaxVoltage;
kagelump 0:b669c97da2c0 142
kagelump 0:b669c97da2c0 143 setupScreen();
kagelump 0:b669c97da2c0 144 testTimer.start();
kagelump 0:b669c97da2c0 145
kagelump 0:b669c97da2c0 146 //Display values as they are being changed
kagelump 0:b669c97da2c0 147 while (true)
kagelump 0:b669c97da2c0 148 {
kagelump 0:b669c97da2c0 149 /* This part tests the printing
kagelump 0:b669c97da2c0 150
kagelump 0:b669c97da2c0 151 //Randomly selects values between appropriate ranges.
kagelump 0:b669c97da2c0 152 speed = (int)(rand()%200);
kagelump 0:b669c97da2c0 153 amperage = (int)(rand()%1000);
kagelump 0:b669c97da2c0 154 voltage = (int)(rand()%100);
kagelump 0:b669c97da2c0 155
kagelump 0:b669c97da2c0 156 //Changes values from 1 to 3 or 4 digits to test proper spacing and erasing with changing num of digits
kagelump 0:b669c97da2c0 157 if (testTimer.read_ms()%1000 == 0)
kagelump 0:b669c97da2c0 158 {
kagelump 0:b669c97da2c0 159 switch ((testTimer.read_ms()/1000)%4)
kagelump 0:b669c97da2c0 160 {
kagelump 0:b669c97da2c0 161 case 0: //1 digit
kagelump 0:b669c97da2c0 162 newSpeed = (int)(rand()%9)+1;
kagelump 0:b669c97da2c0 163 newAmperage = (int)(rand()%9)+1;
kagelump 0:b669c97da2c0 164 break;
kagelump 0:b669c97da2c0 165 case 3: //2 digits
kagelump 0:b669c97da2c0 166 newSpeed = (int)(rand()%90)+10;
kagelump 0:b669c97da2c0 167 newAmperage = (int)(rand()%90)+10;
kagelump 0:b669c97da2c0 168 break;
kagelump 0:b669c97da2c0 169 case 2: //3 digits
kagelump 0:b669c97da2c0 170 newSpeed = (int)(rand()%900)+100;
kagelump 0:b669c97da2c0 171 newAmperage = (int)(rand()%900)+100;
kagelump 0:b669c97da2c0 172 break;
kagelump 0:b669c97da2c0 173 case 1: //4 digits
kagelump 0:b669c97da2c0 174 newSpeed = (int)(rand()%90)+100;
kagelump 0:b669c97da2c0 175 newAmperage = (int)(rand()%900)+1000;
kagelump 0:b669c97da2c0 176 break;
kagelump 0:b669c97da2c0 177 default: break;
kagelump 0:b669c97da2c0 178 }
kagelump 0:b669c97da2c0 179 }*/
kagelump 0:b669c97da2c0 180
kagelump 0:b669c97da2c0 181 //Keeps screen from updating too much
kagelump 0:b669c97da2c0 182 if (testTimer.read_ms()%1000==0)
kagelump 0:b669c97da2c0 183 {
kagelump 0:b669c97da2c0 184 /* More printing testing
kagelump 0:b669c97da2c0 185
kagelump 0:b669c97da2c0 186 //Randomly increases or decreases values within a small range
kagelump 0:b669c97da2c0 187 newSpeed += (int)((rand()%5)-2);
kagelump 0:b669c97da2c0 188 newAmperage += (int)((rand()%21)-10);
kagelump 0:b669c97da2c0 189 newVoltage += (int)((rand()%3)-1);
kagelump 0:b669c97da2c0 190
kagelump 0:b669c97da2c0 191 //Keeps voltage between allowed values
kagelump 0:b669c97da2c0 192 if (newVoltage < MinVoltage || newVoltage > MaxVoltage)
kagelump 0:b669c97da2c0 193 newVoltage = MaxVoltage;
kagelump 0:b669c97da2c0 194 */
kagelump 0:b669c97da2c0 195
kagelump 0:b669c97da2c0 196 readFromCAN();
kagelump 0:b669c97da2c0 197
kagelump 0:b669c97da2c0 198 updateBattery(newVoltage);
kagelump 0:b669c97da2c0 199
kagelump 0:b669c97da2c0 200 //Updates speed
kagelump 0:b669c97da2c0 201 display.SelectFont(MyPhone_40, BLACK, ReadData);
kagelump 0:b669c97da2c0 202 printCenteredInt(newSpeed, speed, 12, 16, 15, 23, 1, 3);
kagelump 0:b669c97da2c0 203 speed = newSpeed;
kagelump 0:b669c97da2c0 204
kagelump 0:b669c97da2c0 205 //Updates amperage
kagelump 0:b669c97da2c0 206 display.SelectFont(MyPhone_20, BLACK, ReadData);
kagelump 0:b669c97da2c0 207 printCenteredInt(newAmperage, amperage, 76, 40+8, 8, 11, 1, 4);
kagelump 0:b669c97da2c0 208 amperage = newAmperage;
kagelump 0:b669c97da2c0 209 }
kagelump 0:b669c97da2c0 210 }
kagelump 0:b669c97da2c0 211 break;
kagelump 0:b669c97da2c0 212 default: break;
kagelump 0:b669c97da2c0 213 }
kagelump 0:b669c97da2c0 214 }
kagelump 0:b669c97da2c0 215 }
kagelump 0:b669c97da2c0 216
kagelump 0:b669c97da2c0 217 void setupScreen()
kagelump 0:b669c97da2c0 218 {
kagelump 0:b669c97da2c0 219 //Draws grid lines to separate the screen.
kagelump 0:b669c97da2c0 220 //Dimensions: (Speed) 70x64, (Battery) 57x42, (Amperage) 57x22
kagelump 0:b669c97da2c0 221 display.HLineShort(71, 42, 57, BLACK);
kagelump 0:b669c97da2c0 222 display.VLineShort(71, 0, 64, BLACK);
kagelump 0:b669c97da2c0 223
kagelump 0:b669c97da2c0 224 //This covers the stray black pixel...
kagelump 0:b669c97da2c0 225 display.SetPixel(71-5, 42, WHITE);
kagelump 0:b669c97da2c0 226
kagelump 0:b669c97da2c0 227 //Draws the outline for battery graphic. Dimensions: 40x24, 4x8
kagelump 0:b669c97da2c0 228 display.RoundRectangle(82, 4, 40, 24, 4, BLACK);
kagelump 0:b669c97da2c0 229 display.EmptyRectangle(78, 12, 78+4, 12+8, BLACK);
kagelump 0:b669c97da2c0 230
kagelump 0:b669c97da2c0 231 //Prints MPH
kagelump 0:b669c97da2c0 232 display.SelectFont(System5x7, BLACK, ReadData);
kagelump 0:b669c97da2c0 233 display.GotoXY(26, 48);
kagelump 0:b669c97da2c0 234 display.PrintString("MPH");
kagelump 0:b669c97da2c0 235
kagelump 0:b669c97da2c0 236 //Print mAmp
kagelump 0:b669c97da2c0 237 display.SelectFont(System5x7, BLACK, ReadData);
kagelump 0:b669c97da2c0 238 display.GotoXY(114, 24+24);
kagelump 0:b669c97da2c0 239 display.PrintString("mA");
kagelump 0:b669c97da2c0 240
kagelump 0:b669c97da2c0 241 //Prints the percent sign
kagelump 0:b669c97da2c0 242 display.GotoXY(108, 24+8);
kagelump 0:b669c97da2c0 243 display.PrintString("%");
kagelump 0:b669c97da2c0 244
kagelump 0:b669c97da2c0 245 //Draws initial shaded box for battery
kagelump 0:b669c97da2c0 246 display.FullRectangle(86+(int)(((double)(100-((newVoltage - MinVoltage)/(MaxVoltage - MinVoltage)*100))/100.0)*32), 4+4, 86+32, 4+24-4, BLACK);
kagelump 0:b669c97da2c0 247 }
kagelump 0:b669c97da2c0 248
kagelump 0:b669c97da2c0 249 //This exists to fix the problem with the percentage printing that was occuring for unknown reasons.
kagelump 0:b669c97da2c0 250 //Writes an int to the screen right-justified over another int.
kagelump 0:b669c97da2c0 251 void printRightJustifiedPercentage(int newInt, int oldInt, int startX, int startY, int charWidth, int charHeight, int charSpacing, int maxDigits)
kagelump 0:b669c97da2c0 252 {
kagelump 0:b669c97da2c0 253 for (int digits = maxDigits; digits > 0; digits--)
kagelump 0:b669c97da2c0 254 {
kagelump 0:b669c97da2c0 255 if (newInt >= (int)pow(10.0, digits-1))
kagelump 0:b669c97da2c0 256 {
kagelump 0:b669c97da2c0 257 //Instead of checking if digits need to be erased, all digits will be erased
kagelump 0:b669c97da2c0 258 //Erases digits that will not be covered by new text.
kagelump 0:b669c97da2c0 259 display.FullRectangle(startX, startY, startX + (charWidth+charSpacing) * (maxDigits - digits), startY + charHeight, WHITE);
kagelump 0:b669c97da2c0 260 display.GotoXY(startX + (charWidth+charSpacing) * (maxDigits - digits), startY);
kagelump 0:b669c97da2c0 261 //Break out of loop.
kagelump 0:b669c97da2c0 262 digits = -1;
kagelump 0:b669c97da2c0 263 }
kagelump 0:b669c97da2c0 264 }
kagelump 0:b669c97da2c0 265 display.PrintNumber(newInt);
kagelump 0:b669c97da2c0 266 }
kagelump 0:b669c97da2c0 267
kagelump 0:b669c97da2c0 268 //Writes an int to the screen center-justified over another int.
kagelump 0:b669c97da2c0 269 void printCenteredInt(int newInt, int oldInt, int startX, int startY, int charWidth, int charHeight, int charSpacing, int maxDigits)
kagelump 0:b669c97da2c0 270 {
kagelump 0:b669c97da2c0 271 for (int digits = maxDigits; digits > 0; digits--)
kagelump 0:b669c97da2c0 272 {
kagelump 0:b669c97da2c0 273 //If current value has "digits" number of digits
kagelump 0:b669c97da2c0 274 if (newInt >= (int)pow(10.0, digits-1))
kagelump 0:b669c97da2c0 275 {
kagelump 0:b669c97da2c0 276 //If previous value had more digits than current value
kagelump 0:b669c97da2c0 277 if (oldInt >= (int)pow(10.0, digits))
kagelump 0:b669c97da2c0 278 {
kagelump 0:b669c97da2c0 279 //Erases all possible digits that could be on screen.
kagelump 0:b669c97da2c0 280 //**First parameter should logically be startX, but it starts erasing 1 pixel too far to the right for some reason. ???
kagelump 0:b669c97da2c0 281 display.FullRectangle(startX-1, startY, startX + maxDigits*(charWidth+charSpacing), startY + charHeight, WHITE);
kagelump 0:b669c97da2c0 282 }
kagelump 0:b669c97da2c0 283 display.GotoXY(startX + 0.5*((maxDigits - digits)*charWidth + (maxDigits - digits - 1)*charSpacing), startY);
kagelump 0:b669c97da2c0 284 //Break out of loop.
kagelump 0:b669c97da2c0 285 digits = -1;
kagelump 0:b669c97da2c0 286 }
kagelump 0:b669c97da2c0 287 }
kagelump 0:b669c97da2c0 288 display.PrintNumber(newInt);
kagelump 0:b669c97da2c0 289 }
kagelump 0:b669c97da2c0 290
kagelump 0:b669c97da2c0 291 //Updates the battery graphic and prints corresponding percentage or voltage.
kagelump 0:b669c97da2c0 292 void updateBattery(int newVoltage)
kagelump 0:b669c97da2c0 293 {
kagelump 0:b669c97da2c0 294 //These are needed for the battery graphic, even if the values won't be displayed.
kagelump 0:b669c97da2c0 295 int percentage = (voltage - MinVoltage)/(MaxVoltage - MinVoltage)*100;
kagelump 0:b669c97da2c0 296 int newPercentage = (newVoltage - MinVoltage)/(MaxVoltage - MinVoltage)*100;
kagelump 0:b669c97da2c0 297
kagelump 0:b669c97da2c0 298 //This line fixes the printing problem with the percentage that was caused by drawing shaded rectangle. Might have something to do with the method WriteInstruction(int, int) used by both methods.
kagelump 0:b669c97da2c0 299 display.SetPixel(88, 32, WHITE);
kagelump 0:b669c97da2c0 300
kagelump 0:b669c97da2c0 301 // ** Problem: after a while of letting it run, sometimes a 0 will show up 1 pxl to the right of the hundreds place. But it gets erased when voltage drops enough
kagelump 0:b669c97da2c0 302 //Prints percentage
kagelump 0:b669c97da2c0 303 display.SelectFont(System5x7, BLACK, ReadData);
kagelump 0:b669c97da2c0 304 printRightJustifiedPercentage(newPercentage, percentage, 88, 32, 5, 7, 1, 3);
kagelump 0:b669c97da2c0 305
kagelump 0:b669c97da2c0 306 //Prints voltage -- currently NOT IN USE
kagelump 0:b669c97da2c0 307 /*display.SelectFont(System5x7, BLACK, ReadData);
kagelump 0:b669c97da2c0 308 display.GotoXY(108, 24+8);
kagelump 0:b669c97da2c0 309 display.PrintString("V");
kagelump 0:b669c97da2c0 310 display.SelectFont(System5x7, BLACK, ReadData);
kagelump 0:b669c97da2c0 311 printRightJustifiedInt(newVoltage, voltage, 88, 32, 5, 7, 1, 3);*/
kagelump 0:b669c97da2c0 312
kagelump 0:b669c97da2c0 313 updateBatteryGraphic(newPercentage, percentage);
kagelump 0:b669c97da2c0 314
kagelump 0:b669c97da2c0 315 percentage = newPercentage;
kagelump 0:b669c97da2c0 316 voltage = newVoltage;
kagelump 0:b669c97da2c0 317 }
kagelump 0:b669c97da2c0 318
kagelump 0:b669c97da2c0 319 //Changes shaded area of graphic to reflect current battery level. Dimensions 32x16
kagelump 0:b669c97da2c0 320 void updateBatteryGraphic(int newPercentage, int percentage)
kagelump 0:b669c97da2c0 321 {
kagelump 0:b669c97da2c0 322 //Stores x-coordinates of shaded part of battery.
kagelump 0:b669c97da2c0 323 int barX = 86+(int)(((double)(100-percentage)/100.0)*32);
kagelump 0:b669c97da2c0 324 int newBarX = 86+(int)(((double)(100-newPercentage)/100.0)*32);
kagelump 0:b669c97da2c0 325
kagelump 0:b669c97da2c0 326 //Draws increase in percentage.
kagelump 0:b669c97da2c0 327 if (newBarX < barX)
kagelump 0:b669c97da2c0 328 {
kagelump 0:b669c97da2c0 329 display.FullRectangle(newBarX, 4+4, barX, 4+24-4, BLACK);
kagelump 0:b669c97da2c0 330 }
kagelump 0:b669c97da2c0 331 //Draws decrease.
kagelump 0:b669c97da2c0 332 else
kagelump 0:b669c97da2c0 333 {
kagelump 0:b669c97da2c0 334 display.FullRectangle(86, 4+4, newBarX, 4+24-4, WHITE);
kagelump 0:b669c97da2c0 335 }
kagelump 0:b669c97da2c0 336 }
kagelump 0:b669c97da2c0 337
kagelump 0:b669c97da2c0 338 //***This method has NOT BEEN TESTED.
kagelump 0:b669c97da2c0 339
kagelump 0:b669c97da2c0 340 //Gets information about speed, voltage, current, and battery life through CAN.
kagelump 0:b669c97da2c0 341 void readFromCAN()
kagelump 0:b669c97da2c0 342 {
kagelump 0:b669c97da2c0 343 const int speedID = 0x403;
kagelump 0:b669c97da2c0 344 const int voltageAmperageID = 0x523;
kagelump 0:b669c97da2c0 345
kagelump 0:b669c97da2c0 346 //Gets message from CAN.
kagelump 0:b669c97da2c0 347 CANMessage msg;
kagelump 0:b669c97da2c0 348 can.read(msg);
kagelump 0:b669c97da2c0 349 int id = msg.id;
kagelump 0:b669c97da2c0 350
kagelump 0:b669c97da2c0 351 switch (id)
kagelump 0:b669c97da2c0 352 {
kagelump 0:b669c97da2c0 353 case voltageAmperageID: //contains four messages: voltage 1 and 2 in volts, current 1 and 2 in mA. each is an int (2 bytes)
kagelump 0:b669c97da2c0 354
kagelump 0:b669c97da2c0 355 //Read & store amperage value.
kagelump 0:b669c97da2c0 356 intchar amp_data;
kagelump 0:b669c97da2c0 357 for (int i = 0; i < 2; i++){
kagelump 0:b669c97da2c0 358 amp_data.s[i] = msg.data[i];
kagelump 0:b669c97da2c0 359 }
kagelump 0:b669c97da2c0 360 amperage = amp_data.i[0]; //***is this correct?
kagelump 0:b669c97da2c0 361
kagelump 0:b669c97da2c0 362 //Read & store voltage/battery life value.
kagelump 0:b669c97da2c0 363 floatchar volt_data;
kagelump 0:b669c97da2c0 364 for (int i = 0; i < 2; i++){
kagelump 0:b669c97da2c0 365 volt_data.s[i] = msg.data[i+4];
kagelump 0:b669c97da2c0 366 }
kagelump 0:b669c97da2c0 367 voltage = volt_data.f[0]; //***should this be reading voltage 1 or voltage 2? (volt1.f[0 or 1])
kagelump 0:b669c97da2c0 368 break;
kagelump 0:b669c97da2c0 369
kagelump 0:b669c97da2c0 370 case speedID:
kagelump 0:b669c97da2c0 371 //** This packet actually contains motor velocity in rpm as well as vehicle velociy in m/s. How do I get only the second part?
kagelump 0:b669c97da2c0 372 floatchar speed_data;
kagelump 0:b669c97da2c0 373 for (int i = 0; i < 4; i++) {
kagelump 0:b669c97da2c0 374 speed_data.s[i] = msg.data[i];
kagelump 0:b669c97da2c0 375 }
kagelump 0:b669c97da2c0 376 //Conversion factor: 1 m/s = 2.23693629 mph
kagelump 0:b669c97da2c0 377 speed = (speed_data.f[2])*2.23693629; //** should this be a combination of f[2] and f[3]?
kagelump 0:b669c97da2c0 378 break;
kagelump 0:b669c97da2c0 379 default: break;
kagelump 0:b669c97da2c0 380 }
kagelump 0:b669c97da2c0 381 }