Dashboard firmware for FBR2012
Dependencies: mbed TextLCD PinDetect
Revision 1:b3907b8d9f65, committed 2012-06-25
- Comitter:
- intrinseca
- Date:
- Mon Jun 25 21:01:02 2012 +0000
- Parent:
- 0:1f422ed56e0f
- Child:
- 2:825f572902c6
- Commit message:
- Prototype before adding full CAN comms
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/PinDetect.lib Mon Jun 25 21:01:02 2012 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/AjK/code/PinDetect/#cb3afc45028b
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/TextLCD.lib Mon Jun 25 21:01:02 2012 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/intrinseca/code/TextLCD/#006bcd28cc39
--- a/TextLCD/.lib Thu Nov 03 20:38:12 2011 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -http://mbed.org/users/dreschpe/libraries/TextLCD/ljfvru \ No newline at end of file
--- a/TextLCD/TextLCD.cpp Thu Nov 03 20:38:12 2011 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,226 +0,0 @@ -/* mbed TextLCD Library, for a 4-bit LCD based on HD44780 - * Copyright (c) 2007-2010, sford, http://mbed.org - * - * 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 "TextLCD.h" -#include "mbed.h" - -TextLCD::TextLCD(PinName rs, PinName rw, PinName e, PinName d0, PinName d1, - PinName d2, PinName d3, LCDType type) : _rs(rs), _rw(rw), - _e(e), _d(d0, d1, d2, d3), _type(type) { - _rs = 0; // command mode - _rw = 0; - _e = 0; - _d.output(); // data out - - wait(0.05); // Wait 50ms to ensure powered up - - // send "Display Settings" 3 times (Only top nibble of 0x30 as we've got 4-bit bus) - for (int i=0; i<3; i++) { - _e = 1; - __nop(); - _d = 0x3; - __nop(); - _e = 0; - wait(0.004f); // 4ms - } - _e = 1; - __nop(); - _d = 0x2; // 4 Bit mode - __nop(); - _e = 0; - - writeCommand(0x28); // Function set 4 Bit, 2Line, 5*7 - writeCommand(0x08); // Display off - writeCommand(0x01); // clear Display - writeCommand(0x04); // cursor right, Display is not shifted - writeCommand(0x0C); // Display on , Cursor off -} - -void TextLCD::character(int column, int row, int c) { - int a = address(column, row); - writeCommand(a); - writeData(c); -} - -void TextLCD::cls() { - writeCommand(0x01); // cls, and set cursor to 0 - locate(0, 0); -} - -void TextLCD::locate(int column, int row) { - _column = column; - _row = row; -} - - - -int TextLCD::_putc(int value) { - if (value == '\n') { - _column = 0; - _row++; - if (_row >= rows()) { - _row = 0; - } - } else { - character(_column, _row, value); - _column++; - if (_column >= columns()) { - _column = 0; - _row++; - if (_row >= rows()) { - _row = 0; - } - } - } - return value; -} - -int TextLCD::_getc() { - int a = address(_column, _row); - writeCommand(a); - return (readData()); -} - -void TextLCD::writeByte(int value) { - _e = 1; - __nop(); - _d = value >> 4; - __nop(); - _e = 0; - __nop(); - _e = 1; - __nop(); - _d = value >> 0; - __nop(); - _e = 0; -} - -void TextLCD::writeCommand(int command) { - waitBusy(); // check if display is ready - _rs = 0; - writeByte(command); -} - -int TextLCD::readData(){ - int input; - waitBusy(); - _rw = 1; - _rs = 1; - __nop(); - _d.input(); // switch Data port to input - _e = 1; - __nop(); - input = _d.read() << 4; // high nibble - _e = 0; - __nop(); - _e = 1; - __nop(); - input = input | _d.read(); // low nibble - _e = 0; - return (input); -} - - void TextLCD::waitBusy(){ - int input; - _rw = 1; - _rs = 0; - __nop(); - _d.input(); // switch Data port to input - do{ - _e = 1; - __nop(); - input = _d.read(); - _e = 0; - __nop(); - _e = 1; - __nop(); - _e = 0; - }while((0x8 & input) == 0x8); // wait until display is ready - _rw = 0; - _d.output(); // switch port back to output - } - -void TextLCD::writeData(int data) { - waitBusy(); - _rs = 1; - writeByte(data); -} - - -// set user defined char -void TextLCD::writeCGRAM(int address, int pattern[8]){ - int i; - address = address & 0x07; //max 8 char - for(i=0;i<8;i++){ - waitBusy(); // check if display is ready - _rs = 0; - writeByte(0x40 + address * 8 + i); - writeData(pattern[i]); - } -} - - -int TextLCD::address(int column, int row) { - switch (_type) { - case LCD20x4: - switch (row) { - case 0: - return 0x80 + column; - case 1: - return 0xc0 + column; - case 2: - return 0x94 + column; - case 3: - return 0xd4 + column; - } - case LCD16x2B: - return 0x80 + (row * 40) + column; - case LCD16x2: - case LCD20x2: - default: - return 0x80 + (row * 0x40) + column; - } -} - -int TextLCD::columns() { - switch (_type) { - case LCD20x4: - case LCD20x2: - return 20; - case LCD16x2: - case LCD16x2B: - default: - return 16; - } -} - -int TextLCD::rows() { - switch (_type) { - case LCD20x4: - return 4; - case LCD16x2: - case LCD16x2B: - case LCD20x2: - default: - return 2; - } -}
--- a/TextLCD/TextLCD.h Thu Nov 03 20:38:12 2011 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,153 +0,0 @@ -/* mbed TextLCD Library, for a 4-bit LCD based on HD44780 - * Copyright (c) 2007-2010, sford, http://mbed.org - * - * 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. - */ - -#ifndef MBED_TEXTLCD_H -#define MBED_TEXTLCD_H - -#include "mbed.h" - -/** A TextLCD interface for driving 4-bit HD44780-based LCDs - * - * Currently supports 16x2, 20x2 and 20x4 panels - * - * @code - * #include "mbed.h" - * #include "TextLCD.h" - * - * TextLCD lcd(p10, p12, ,p13, p15, p16, p29, p30); // rs, rw, e, d0-d3 - * - * int main() { - * lcd.printf("Hello mbed\n"); - * - * //define user chars - * int pattern[8]; - * int pattern1[8]; - * pattern[0] = 1; // * - * pattern[1] = 3; // ** - * pattern[2] = 5; // * * - * pattern[3] = 9; // * * - * pattern[4] = 0x11; // * * - * pattern[5] = 0x19; // ** * - * pattern[6] = 0x1d; // *** * - * pattern[7] = 0x1f; // ***** - * - * pattern1[0] = 0x10; // * - * pattern1[1] = 0x18; // ** - * pattern1[2] = 0x14; // * * - * pattern1[3] = 0x12; // * * - * pattern1[4] = 0x11; // * * - * pattern1[5] = 0x13; // * ** - * pattern1[6] = 0x17; // * *** - * pattern1[7] = 0x1f; // ***** - * - * lcd.writeCGRAM(0, pattern); - * lcd.writeCGRAM(1, pattern1); - * - * lcd.locate(15,0); - * lcd.putc(0); // user pattern 0 - * lcd.putc(1); // user pattern 1 - * } - * @endcode - */ -class TextLCD : public Stream { -public: - - /** LCD panel format */ - enum LCDType { - LCD16x2 /**< 16x2 LCD panel (default) */ - , LCD16x2B /**< 16x2 LCD panel alternate addressing */ - , LCD20x2 /**< 20x2 LCD panel */ - , LCD20x4 /**< 20x4 LCD panel */ - }; - - /** Create a TextLCD interface - * - * @param rs Instruction/data control line - * @param e Enable line (clock) - * @param rw read / write - * @param d0-d3 Data lines - * @param type Sets the panel size/addressing mode (default = LCD16x2) - */ - TextLCD(PinName rs, PinName rw, PinName e, PinName d0, PinName d1, PinName d2, PinName d3, LCDType type = LCD16x2); - -#if DOXYGEN_ONLY - /** Write a character to the LCD - * - * @param c The character to write to the display - */ - int putc(int c); - - /** Write a formated string to the LCD - * - * @param format A printf-style format string, followed by the - * variables to use in formating the string. - */ - int printf(const char* format, ...); -#endif - - /** Locate to a screen column and row - * - * @param column The horizontal position from the left, indexed from 0 - * @param row The vertical position from the top, indexed from 0 - */ - void locate(int column, int row); - - /** Clear the screen and locate to 0,0 */ - void cls(); - - int rows(); - int columns(); - - /** write a user defined char - * - * @param address The user defined char (0-7) - * @param pattern[8] bit pattern 5*8 of char - */ - void writeCGRAM(int address, int pattern[8]); - - /** Get the char at the current position - * - * int getc() - */ - -protected: - - // Stream implementation functions - virtual int _putc(int value); - virtual int _getc(); - - int address(int column, int row); - void character(int column, int row, int c); - void writeByte(int value); - void writeCommand(int command); - void writeData(int data); - int readData(); - void waitBusy(); - DigitalOut _rs, _rw, _e; - BusInOut _d; - LCDType _type; - - int _column; - int _row; -}; - -#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/inc/Comms.h Mon Jun 25 21:01:02 2012 +0000 @@ -0,0 +1,18 @@ +#ifndef FBRDASH_COMMS_H +#define FBRDASH_COMMS_H + +#include "State.h" + +class Comms +{ + public: + Comms(State* _values); + virtual void send(char message) {}; + + protected: + State* values; + + void process_packet(unsigned char id, int length, unsigned char data[]); +}; + +#endif \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/inc/Gears.h Mon Jun 25 21:01:02 2012 +0000 @@ -0,0 +1,27 @@ +#ifndef FBDDASH_GEARS_H +#define FBRDASH_GEARS_H + +#include "mbed.h" +#include "PinDetect.h" +#include "Comms.h" + +class Gears +{ + public: + Gears(PinName up, PinName neutral, PinName down, unsigned char* _currentGear, Comms* _remote); + + private: + PinDetect* btnUp; + PinDetect* btnNeutral; + PinDetect* btnDown; + + unsigned char* currentGear; + + Comms* remote; + + void up(); + void down(); + void neutral(); +}; + +#endif \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/inc/LEDS.h Mon Jun 25 21:01:02 2012 +0000 @@ -0,0 +1,22 @@ +#ifndef FBRDASH_LEDS_H +#define FBRDASH_LEDS_H + +#include "mbed.h" + +class LEDS +{ + public: + LEDS(PwmOut _pins[]); + void refresh(float rpm); + + static const int NUM_LEDS = 6; + + private: + PwmOut* pins; + + static const int LIMIT = 18000.0; + static const int RESOLUTION = LIMIT / NUM_LEDS; + +}; + +#endif \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/inc/Menu.h Mon Jun 25 21:01:02 2012 +0000 @@ -0,0 +1,124 @@ +#ifndef FBRDASH_MENU_H +#define FBRDASH_MENU_H + +#include "TextLCD.h" +#include "PinDetect.h" +#include <string> + +class MenuEntryBase +{ + public: + bool editable; + + string label; + string format; + + virtual void increment() = 0; + virtual void decrement() = 0; + virtual string getFormatted() = 0; +}; + +template <typename T> +class MenuEntry : public MenuEntryBase +{ + public: + T* value; + T step; + + virtual void increment() + { + *value += step; + } + + virtual void decrement() + { + *value -= step; + } + + virtual string getFormatted() + { + char buffer[16]; + sprintf(buffer, format.c_str(), *value); + return buffer; + } +}; + +class Menu +{ + public: + bool display; + bool edit; + + Menu(TextLCD* _screen, PinName ok, PinName left, PinName right); + + template <typename T> + void addItem(string label, string format, T* value); + + template <typename T> + void addEditableItem(string label, string format, T* value, T step); + + void refresh(); + + void enter(); + void done(); + void left(); + void right(); + + private: + MenuEntryBase* entries[20]; + int entryCount; + + PinDetect* btnOK; + PinDetect* btnLeft; + PinDetect* btnRight; + + TextLCD* screen; + + int position; + bool ignoreNextEnter; + + Timeout leftHeldTimeout; + Timeout rightHeldTimeout; + + void leftHeld(); + void cancelLeftHeld(); + void rightHeld(); + void cancelRightHeld(); + + static const float holdRepeatTime = 0.15; //should be multiple of screen refresh rate or wierdness will ensue + static const char leftArrow = 0x7F; + static const char rightArrow = 0x7E; +}; + +template <typename T> +void Menu::addItem(string label, string format, T* value) +{ + MenuEntry<T>* newEntry = new MenuEntry<T>(); + + newEntry->value = value; + newEntry->editable = false; + + newEntry->label = label; + newEntry->format = format; + + entries[entryCount] = newEntry; + entryCount++; +} + +template <typename T> +void Menu::addEditableItem(string label, string format, T* value, T step) +{ + MenuEntry<T> newEntry; + + newEntry.value = value; + newEntry.editable = true; + newEntry.step = step; + + newEntry.label = label; + newEntry.format = format; + + entries[entryCount] = newEntry; + entryCount++; +} + +#endif \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/inc/PCComms.h Mon Jun 25 21:01:02 2012 +0000 @@ -0,0 +1,21 @@ +#ifndef FBRDASH_PCCOMMS_H +#define FBRDASH_PCCOMMS_H + +#include "mbed.h" +#include "State.h" +#include "Comms.h" + +class PCComms : public Comms +{ + public: + PCComms(State* _values); + virtual void send(char message); + + private: + State* values; + Serial* pc; + + void receive(); +}; + +#endif \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/inc/State.h Mon Jun 25 21:01:02 2012 +0000 @@ -0,0 +1,21 @@ +#ifndef FBRDASH_STATE_H +#define FBRDASH_STATE_H + +struct State +{ + unsigned short int rpm; + unsigned char throttle_pos; + unsigned char manifold_pres; + unsigned char air_temp; + float coolant_temp; + unsigned char lambda; + unsigned char speed; + unsigned char accel_x; + unsigned char accel_y; + unsigned char gear; + unsigned char oil_temp; + unsigned char oil_pres; + unsigned char warnings; +}; + +#endif \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/inc/bigchar.h Mon Jun 25 21:01:02 2012 +0000 @@ -0,0 +1,21 @@ +#ifndef FBRDASH_BIGCHAR_H +#define FBRDASH_BIGCHAR_H + +#include "TextLCD.h" + +#define TOP_LEFT 0 +#define TOP_RIGHT 1 +#define BOTTOM_RIGHT 2 +#define BOTTOM_LEFT 3 +#define TWO_LINES 4 +#define BOTTOM 5 +#define TOP 6 +#define TWO_LINES_5 7 + +#define FULL 0xFF +#define BLANK 0xFE + +void setup_bigchar(TextLCD* lcd); +void write_bigchar(TextLCD* lcd, int position, int number); + +#endif \ No newline at end of file
--- a/main.cpp Thu Nov 03 20:38:12 2011 +0000 +++ b/main.cpp Mon Jun 25 21:01:02 2012 +0000 @@ -1,80 +1,130 @@ #include "mbed.h" #include "TextLCD.h" +#include "PinDetect.h" +#include "Menu.h" +#include "Comms.h" +#include "PCComms.h" +#include "Gears.h" +#include "LEDS.h" +#include "bigchar.h" -#define NUM_LEDS 6 -PwmOut leds[] = { (p26), (p25), (p24), (p23), (p22), (p21) }; +#define LCD_REFRESH_TIME 150000 +#define REV_REFRESH_TIME 50000 + +DigitalOut warn[] = { (p20), (p19), (p27), (p18) }; +DigitalOut debug[] = { (LED1), (LED2), (LED3) }; -TextLCD lcd(p10, p11, p12, p17, p18, p19, p20); +DigitalOut heartbeat(LED4); + +PwmOut leds[] = { (p24), (p25), (p26), (p23), (p22), (p21) }; + +TextLCD lcd(p5, p6, p7, p8, p9, p10, p11); -InterruptIn shiftDown(p5); -InterruptIn shiftUp(p6); -InterruptIn shiftNeutral(p7); +Ticker lcdRefreshTicker; +Ticker revRefreshTicker; +Ticker increment; + +State car; -volatile float increment = 0; +Menu dashMenu(&lcd, p13, p14, p15); //*LCD, OK, Left, Right +PCComms pc(&car); +Gears gearButtons(p17, p16, p12, &car.gear, &pc); //Up, Neutral, Down, *Current Gear +LEDS revs(leds); -void up() +void revRefresh() { - increment += 0.01; + revs.refresh(car.rpm); + + for(int i = 0; i < 4; i++) + { + warn[i] = car.warnings & (1 << i); + } } -void down() +void lcdRefresh() { - increment -= 0.01; -} - -void neutral() -{ - increment = 0; + if(dashMenu.display == false) + { + lcd.locate(0, 0); + lcd.printf("R:%-11.0d", car.rpm); + lcd.locate(0, 1); + lcd.printf("S:%-3d T:%-5.0f", car.speed, car.coolant_temp); + + write_bigchar(&lcd, 13, car.gear); + } + else + { + dashMenu.refresh(); + } + + heartbeat = !heartbeat; } -int main() { - float i; +/*void doIncrement() +{ + if(car.rpm < LIMIT && car.gear > 0) + car.rpm++; +}*/ - shiftUp.rise(&up); - shiftDown.rise(&down); - shiftNeutral.rise(&neutral); - - leds[0].period(0.0001); +void selfTest() +{ + lcd.printf(" FBR 2012"); - lcd.locate(0, 1); - lcd.printf("Neutral"); + for(int i = 0; i < LEDS::NUM_LEDS; i++) + { + leds[i] = true; + if(i < 4) + warn[i] = true; + wait(0.04); + } - while(1) { - if(increment >= 0) - i = 0; - else - i = 6.0; + for(int i = LEDS::NUM_LEDS - 1; i >= 0; i--) + { + leds[i] = false; + if(i < 4) + warn[i] = false; + wait(0.04); + } - for(; i <= 6 && i >= 0; i += increment) - { - for(int j = 0; j < NUM_LEDS; j++) - { - if(j < (int)i) - { - leds[j] = 1.0; - } - else if(j >= (int)i + 1) - { - leds[j] = 0; - } - else - { - leds[j] = i - ((int)i); - } - } - - lcd.locate(0, 0); - lcd.printf("%4.2f %4.2f", i, increment); - - lcd.locate(0, 1); - if(increment < 0) - lcd.printf("Down "); - else if(increment > 0) - lcd.printf("Up "); - else - lcd.printf("Neutral"); - - wait_ms(10); - } + lcd.cls(); +} + +int main() +{ + car.rpm = 5000; + car.gear = 0; + car.speed = 150; + car.coolant_temp = 21.5; + + car.throttle_pos = 1; + car.manifold_pres = 2; + car.air_temp = 3; + car.lambda = 5; + car.accel_x = 7; + car.accel_y = 8; + car.oil_temp = 10; + car.oil_pres = 11; + car.warnings = 12; + + dashMenu.addItem<float>("Coolant Temp ", "%12.1f\xDF\x43", &car.coolant_temp); // \xDF\x43 -> �C . Need code for C as otherwise it gets taken as hex digit. + dashMenu.addItem<unsigned char>("Air Temp ", "%12d\xDF\x43", &car.air_temp); + dashMenu.addItem<unsigned char>("Throttle Pos ", "%13d\xDF", &car.throttle_pos); + dashMenu.addItem<unsigned char>("Manifold Pres ", "%10d psi", &car.manifold_pres); + dashMenu.addItem<unsigned char>("Lambda ", "%14d", &car.lambda); + + dashMenu.addItem<unsigned char>("Oil Temp ", "%12d\xDF\x43", &car.oil_temp); + dashMenu.addItem<unsigned char>("Oil Pressure ", "%10d psi", &car.oil_pres); + + setup_bigchar(&lcd); + + selfTest(); + + lcdRefreshTicker.attach_us(&lcdRefresh, LCD_REFRESH_TIME); + revRefreshTicker.attach_us(&revRefresh, REV_REFRESH_TIME); + //increment.attach(&doIncrement, 0.0005); + + while(true) + { + __WFI(); } -} +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Mon Jun 25 21:01:02 2012 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/63bcd7ba4912 \ No newline at end of file
--- a/mbed.lib Thu Nov 03 20:38:12 2011 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -http://mbed.org/projects/libraries/svn/mbed/trunk@28 \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/Comms.cpp Mon Jun 25 21:01:02 2012 +0000 @@ -0,0 +1,32 @@ +#include "PCComms.h" +#include "mbed.h" +#include "State.h" + +Comms::Comms(State* _values) +{ + values = _values; +} + +void Comms::process_packet(unsigned char id, int length, unsigned char data[]) +{ + switch(id) + { + case 100: + values->rpm = data[0] + (data[1] << 8); + values->throttle_pos = data[2]; + values->manifold_pres = data[3]; + values->air_temp = data[4]; + values->coolant_temp = data[5]; + values->lambda = data[6]; + break; + case 200: + values->speed = data[0]; + values->accel_x = data[1]; + values->accel_y = data[2]; + values->gear = data[3]; + values->oil_temp = data[4]; + values->oil_pres = data[5]; + values->warnings = data[6]; + break; + } +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/Gears.cpp Mon Jun 25 21:01:02 2012 +0000 @@ -0,0 +1,51 @@ +#include "Gears.h" +#include "mbed.h" +#include "PinDetect.h" +#include "Comms.h" + +Gears::Gears(PinName up, PinName neutral, PinName down, unsigned char* _currentGear, Comms* _remote) +{ + currentGear = _currentGear; + remote = _remote; + + btnUp = new PinDetect(up, PullUp); + btnNeutral = new PinDetect(neutral, PullUp); + btnDown = new PinDetect(down, PullUp); + + btnUp->setAssertValue(0); + btnDown->setAssertValue(0); + btnNeutral->setAssertValue(0); + + btnUp->attach_asserted(this, &Gears::up); + btnDown->attach_asserted(this, &Gears::down); + btnNeutral->attach_asserted(this, &Gears::neutral); + + btnUp->setSampleFrequency(); + btnDown->setSampleFrequency(); + btnNeutral->setSampleFrequency(); +} + +void Gears::up() +{ + remote->send(2); + + if(*currentGear < 6) + (*currentGear)++; +} + +void Gears::neutral() +{ + remote->send(0); + + *currentGear = 0; +} + +void Gears::down() +{ + remote->send(1); + + if(*currentGear > 0) + { + (*currentGear)--; + } +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/LEDS.cpp Mon Jun 25 21:01:02 2012 +0000 @@ -0,0 +1,35 @@ +#include "mbed.h" +#include "LEDS.h" + +LEDS::LEDS(PwmOut _pins[]) +{ + pins = _pins; + + pins[0].period_us(100); +} + +void LEDS::refresh(float rpm) +{ + int value; + int remainder; + int i; + + value = rpm / RESOLUTION; + + for(i = 0; i < NUM_LEDS; i++) + { + if(i < value) + { + pins[i] = 1.0; + } + else if(i == value) + { + remainder = (int)rpm % RESOLUTION; + pins[i] = (float)remainder / (float)RESOLUTION; + } + else + { + pins[i] = 0.0; + } + } +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/Menu.cpp Mon Jun 25 21:01:02 2012 +0000 @@ -0,0 +1,131 @@ +#include "Menu.h" +#include "mbed.h" +#include <string> + +Menu::Menu(TextLCD* _screen, PinName ok, PinName left, PinName right) +{ + screen = _screen; + + entryCount = 0; + + position = 0; + + btnOK = new PinDetect(ok, PullUp); + btnLeft = new PinDetect(left, PullUp); + btnRight = new PinDetect(right, PullUp); + + btnOK->attach_deasserted(this, &Menu::enter); + btnOK->attach_asserted_held(this, &Menu::done); + + btnLeft->attach_asserted(this, &Menu::left); + btnLeft->attach_asserted_held(this, &Menu::leftHeld); + btnLeft->attach_deasserted(this, &Menu::cancelLeftHeld); + + btnRight->attach_asserted(this, &Menu::right); + btnRight->attach_asserted_held(this, &Menu::rightHeld); + btnRight->attach_deasserted(this, &Menu::cancelRightHeld); + + btnOK->setAssertValue(0); + btnLeft->setAssertValue(0); + btnRight->setAssertValue(0); + + btnOK->setSampleFrequency(); + btnLeft->setSampleFrequency(); + btnRight->setSampleFrequency(); + + ignoreNextEnter = false; +} + +void Menu::refresh() +{ + char labelLeft = (!edit & position > 0)?leftArrow:' '; + char labelRight = (!edit & position < entryCount)?rightArrow:' '; + char editIndic = (entries[position]->editable)?'*':' '; + + screen->locate(0, 0); + + if(position <= entryCount - 1) + { + screen->printf("%c%-14s%c", labelLeft, (entries[position])->label.c_str(), labelRight); + + char editLeft = (edit)?'-':editIndic; + char editRight = (edit)?'+':' '; + + screen->putc(editLeft); + screen->printf(entries[position]->getFormatted().c_str()); + screen->locate(15, 1); + screen->putc(editRight); + } + else + { + screen->printf("%cReturn ", leftArrow); + } +} + +void Menu::enter() +{ + if(!display && !ignoreNextEnter) + { + display = true; + edit = false; + } + else + { + if(position <= entryCount - 1) + { + if(entries[position]->editable) + edit = !edit; + } + else + { + position = 0; + done(); + } + } + + ignoreNextEnter = false; +} + +void Menu::done() +{ + display = false; + ignoreNextEnter = true; +} + +void Menu::left() +{ + if(!edit && display && position > 0) + position--; + else if(edit) + entries[position]->decrement(); +} + +void Menu::leftHeld() +{ + left(); + leftHeldTimeout.attach(this, &Menu::leftHeld, holdRepeatTime); +} + +void Menu::cancelLeftHeld() +{ + leftHeldTimeout.detach(); +} + +void Menu::right() +{ + if(!edit && display && position < entryCount) + position++; + else if(edit) + entries[position]->increment(); +} + +void Menu::rightHeld() +{ + right(); + rightHeldTimeout.attach(this, &Menu::rightHeld, holdRepeatTime); +} + +void Menu::cancelRightHeld() +{ + rightHeldTimeout.detach(); +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/PCComms.cpp Mon Jun 25 21:01:02 2012 +0000 @@ -0,0 +1,43 @@ +#include "PCComms.h" +#include "mbed.h" +#include "State.h" + +#define BUFFER_LENGTH 10 + +PCComms::PCComms(State* _values) : Comms(_values) +{ + pc = new Serial(USBTX, USBRX); + + pc->attach(this, &PCComms::receive, Serial::RxIrq); +} + +void PCComms::send(char message) +{ + pc->putc(message); +} + +void PCComms::receive() +{ + unsigned char packet_id; + unsigned char data_length; + + unsigned char buffer[BUFFER_LENGTH]; + + int i; + + if(pc->readable()) + { + if(pc->getc() == 0xFF && pc->getc() == 0xFF) //Handshake(ish) + { + packet_id = pc->getc(); + data_length = pc->getc(); + + for(i = 0; i < data_length && i < BUFFER_LENGTH; i++) + { + buffer[i] = pc->getc(); + } + + process_packet(packet_id, data_length, buffer); + } + } +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/bigchar.cpp Mon Jun 25 21:01:02 2012 +0000 @@ -0,0 +1,116 @@ +#include "TextLCD.h" +#include "bigchar.h" + +void setup_bigchar(TextLCD* lcd) +{ + int top_left[8] = {0x07, 0x0F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, }; + lcd->writeCGRAM(TOP_LEFT, top_left); + + int top_right[8] = {0x1C, 0x1E, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, }; + lcd->writeCGRAM(TOP_RIGHT, top_right); + + int bottom_right[8] = {0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1E, 0x1C, }; + lcd->writeCGRAM(BOTTOM_RIGHT, bottom_right); + + int bottom_left[8] = {0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x0F, 0x07, }; + lcd->writeCGRAM(BOTTOM_LEFT, bottom_left); + + int two_lines[8] = {0x1F, 0x1F, 0x1F, 0x00, 0x00, 0x00, 0x1F, 0x1F, }; + lcd->writeCGRAM(TWO_LINES, two_lines); + + int bottom[8] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x1F, 0x1F, 0x1F, }; + lcd->writeCGRAM(BOTTOM, bottom); + + int top[8] = {0x1F, 0x1F, 0x1F, 0x00, 0x00, 0x00, 0x00, 0x00, }; + lcd->writeCGRAM(TOP, top); + + int two_lines_5[8] = {0x1F, 0x1F, 0x1F, 0x00, 0x00, 0x00, 0x1E, 0x1F, }; + lcd->writeCGRAM(TWO_LINES_5, two_lines_5); +} + +void write_bigchar(TextLCD* lcd, int position, int number) +{ + switch(number) + { + case 0: + lcd->locate(position, 0); + lcd->putc(TOP_LEFT); + lcd->putc(TOP); + lcd->putc(TOP_RIGHT); + lcd->locate(position, 1); + lcd->putc(BOTTOM_LEFT); + lcd->putc(BOTTOM); + lcd->putc(BOTTOM_RIGHT); + break; + case 1: + lcd->locate(position, 0); + lcd->putc(TOP); + lcd->putc(TOP_RIGHT); + lcd->putc(BLANK); + lcd->locate(position, 1); + lcd->putc(BOTTOM); + lcd->putc(FULL); + lcd->putc(BOTTOM); + break; + case 2: + lcd->locate(position, 0); + lcd->putc(TWO_LINES); + lcd->putc(TWO_LINES); + lcd->putc(TOP_RIGHT); + lcd->locate(position, 1); + lcd->putc(BOTTOM_LEFT); + lcd->putc(BOTTOM); + lcd->putc(BOTTOM); + break; + case 3: + lcd->locate(position, 0); + lcd->putc(TOP); + lcd->putc(TWO_LINES); + lcd->putc(TOP_RIGHT); + lcd->locate(position, 1); + lcd->putc(BOTTOM); + lcd->putc(BOTTOM); + lcd->putc(BOTTOM_RIGHT); + break; + case 4: + lcd->locate(position, 0); + lcd->putc(BOTTOM_LEFT); + lcd->putc(BOTTOM); + lcd->putc(FULL); + lcd->locate(position, 1); + lcd->putc(BLANK); + lcd->putc(BLANK); + lcd->putc(FULL); + break; + case 5: + lcd->locate(position, 0); + lcd->putc(FULL); + lcd->putc(TWO_LINES); + lcd->putc(TWO_LINES_5); + lcd->locate(position, 1); + lcd->putc(BOTTOM); + lcd->putc(BOTTOM); + lcd->putc(BOTTOM_RIGHT); + break; + case 6: + lcd->locate(position, 0); + lcd->putc(TOP_LEFT); + lcd->putc(TWO_LINES); + lcd->putc(TWO_LINES_5); + lcd->locate(position, 1); + lcd->putc(BOTTOM_LEFT); + lcd->putc(BOTTOM); + lcd->putc(BOTTOM_RIGHT); + break; + default: + lcd->locate(position, 0); + lcd->putc(BLANK); + lcd->putc(BLANK); + lcd->putc(BLANK); + lcd->locate(position, 1); + lcd->putc(BLANK); + lcd->putc(BLANK); + lcd->putc(BLANK); + break; + } +} \ No newline at end of file