Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed TextLCD PinDetect
Revision 2:825f572902c6, committed 2012-06-25
- Comitter:
- intrinseca
- Date:
- Mon Jun 25 21:20:22 2012 +0000
- Parent:
- 1:b3907b8d9f65
- Child:
- 3:cb334e1d31c6
- Commit message:
- Add comments
Changed in this revision
--- a/inc/LEDS.h Mon Jun 25 21:01:02 2012 +0000
+++ b/inc/LEDS.h Mon Jun 25 21:20:22 2012 +0000
@@ -14,7 +14,10 @@
private:
PwmOut* pins;
+ //revs represented by last red LED
static const int LIMIT = 18000.0;
+
+ //number of revs per LED
static const int RESOLUTION = LIMIT / NUM_LEDS;
};
--- a/main.cpp Mon Jun 25 21:01:02 2012 +0000
+++ b/main.cpp Mon Jun 25 21:20:22 2012 +0000
@@ -8,29 +8,44 @@
#include "LEDS.h"
#include "bigchar.h"
+//Refresh interval for LCD
#define LCD_REFRESH_TIME 150000
+
+//Refresh interval for rev LEDs
#define REV_REFRESH_TIME 50000
+//Warning lights
DigitalOut warn[] = { (p20), (p19), (p27), (p18) };
+
+//mBED LEDs
DigitalOut debug[] = { (LED1), (LED2), (LED3) };
+//Heartbeat LED
DigitalOut heartbeat(LED4);
+//Rev LEDS
PwmOut leds[] = { (p24), (p25), (p26), (p23), (p22), (p21) };
+//LCD
TextLCD lcd(p5, p6, p7, p8, p9, p10, p11);
+//Tickers for refreshing
Ticker lcdRefreshTicker;
Ticker revRefreshTicker;
+
+//Used to animate LEDs for testing
Ticker increment;
+//Main car state structure
State car;
+//Classes for various parts of the firmware
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);
+//Refresh the rev LEDs and warning LEDs
void revRefresh()
{
revs.refresh(car.rpm);
@@ -41,8 +56,10 @@
}
}
+//Refresh the LCD
void lcdRefresh()
{
+ //If menu off screen, display HUD
if(dashMenu.display == false)
{
lcd.locate(0, 0);
@@ -52,25 +69,31 @@
write_bigchar(&lcd, 13, car.gear);
}
+ //Otherwise show menu
else
{
dashMenu.refresh();
}
+ //Blink heartbeat
heartbeat = !heartbeat;
}
+
+//Used to animate LEDs for testing
/*void doIncrement()
{
if(car.rpm < LIMIT && car.gear > 0)
car.rpm++;
}*/
+//Startup animation
void selfTest()
{
lcd.printf(" FBR 2012");
- for(int i = 0; i < LEDS::NUM_LEDS; i++)
+ //Light up LEDs
+ for(int i = 0; i < LEDS::NUM_LEDS; i++)
{
leds[i] = true;
if(i < 4)
@@ -78,6 +101,7 @@
wait(0.04);
}
+ //Turn off LEDs
for(int i = LEDS::NUM_LEDS - 1; i >= 0; i--)
{
leds[i] = false;
@@ -90,12 +114,12 @@
}
int main()
-{
+{
+ //Initialise state
car.rpm = 5000;
car.gear = 0;
car.speed = 150;
- car.coolant_temp = 21.5;
-
+ car.coolant_temp = 21.5;
car.throttle_pos = 1;
car.manifold_pres = 2;
car.air_temp = 3;
@@ -106,23 +130,27 @@
car.oil_pres = 11;
car.warnings = 12;
+ //Set up menu
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>("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);
+ //Set up characters on LCS
setup_bigchar(&lcd);
-
+
+ //Do bootup animation
selfTest();
+ //Start refresh tickers
lcdRefreshTicker.attach_us(&lcdRefresh, LCD_REFRESH_TIME);
revRefreshTicker.attach_us(&revRefresh, REV_REFRESH_TIME);
//increment.attach(&doIncrement, 0.0005);
+ //Infinite loop - program is interrupt driven
while(true)
{
__WFI();
--- a/src/Comms.cpp Mon Jun 25 21:01:02 2012 +0000
+++ b/src/Comms.cpp Mon Jun 25 21:20:22 2012 +0000
@@ -2,11 +2,15 @@
#include "mbed.h"
#include "State.h"
+//Process incoming data from Comms interfaces
+
+//Initialise members
Comms::Comms(State* _values)
{
values = _values;
}
+//Process an incoming data packet
void Comms::process_packet(unsigned char id, int length, unsigned char data[])
{
switch(id)
--- a/src/Gears.cpp Mon Jun 25 21:01:02 2012 +0000
+++ b/src/Gears.cpp Mon Jun 25 21:20:22 2012 +0000
@@ -3,6 +3,9 @@
#include "PinDetect.h"
#include "Comms.h"
+//Detect and process presses of the Gear buttons
+
+//Initialise members and set up button handlers
Gears::Gears(PinName up, PinName neutral, PinName down, unsigned char* _currentGear, Comms* _remote)
{
currentGear = _currentGear;
@@ -12,14 +15,17 @@
btnNeutral = new PinDetect(neutral, PullUp);
btnDown = new PinDetect(down, PullUp);
+ //Set buttons as pull down to assert
btnUp->setAssertValue(0);
btnDown->setAssertValue(0);
btnNeutral->setAssertValue(0);
+ //Attach handlers
btnUp->attach_asserted(this, &Gears::up);
btnDown->attach_asserted(this, &Gears::down);
btnNeutral->attach_asserted(this, &Gears::neutral);
+ //Start checking buttons
btnUp->setSampleFrequency();
btnDown->setSampleFrequency();
btnNeutral->setSampleFrequency();
@@ -27,23 +33,29 @@
void Gears::up()
{
+ //Send change up command
remote->send(2);
+ //Update state
if(*currentGear < 6)
(*currentGear)++;
}
void Gears::neutral()
{
+ //Sent neutral command
remote->send(0);
+ //Update state
*currentGear = 0;
}
void Gears::down()
{
+ //Send change down command
remote->send(1);
+ //Update state
if(*currentGear > 0)
{
(*currentGear)--;
--- a/src/LEDS.cpp Mon Jun 25 21:01:02 2012 +0000
+++ b/src/LEDS.cpp Mon Jun 25 21:20:22 2012 +0000
@@ -1,6 +1,9 @@
#include "mbed.h"
#include "LEDS.h"
+//Drive the rev LEDs
+
+//Initialise PWM pins
LEDS::LEDS(PwmOut _pins[])
{
pins = _pins;
@@ -8,27 +11,32 @@
pins[0].period_us(100);
}
+//Calculate new PWM values
void LEDS::refresh(float rpm)
{
int value;
int remainder;
int i;
+ //Number of fully-lit LEDs
value = rpm / RESOLUTION;
for(i = 0; i < NUM_LEDS; i++)
{
if(i < value)
- {
+ {
+ //First LEDs on
pins[i] = 1.0;
}
else if(i == value)
{
+ //Last LED partially lit - calculate intensity and set.
remainder = (int)rpm % RESOLUTION;
pins[i] = (float)remainder / (float)RESOLUTION;
}
else
{
+ //All others off
pins[i] = 0.0;
}
}
--- a/src/Menu.cpp Mon Jun 25 21:01:02 2012 +0000
+++ b/src/Menu.cpp Mon Jun 25 21:20:22 2012 +0000
@@ -2,6 +2,9 @@
#include "mbed.h"
#include <string>
+//Drive the menu system
+
+//Initialise and attach button handlers
Menu::Menu(TextLCD* _screen, PinName ok, PinName left, PinName right)
{
screen = _screen;
@@ -38,12 +41,14 @@
void Menu::refresh()
{
+ //Create chars (if needed) for left and right arrows and editable indicator
char labelLeft = (!edit & position > 0)?leftArrow:' ';
char labelRight = (!edit & position < entryCount)?rightArrow:' ';
char editIndic = (entries[position]->editable)?'*':' ';
screen->locate(0, 0);
+ //If position is on an entry, display it
if(position <= entryCount - 1)
{
screen->printf("%c%-14s%c", labelLeft, (entries[position])->label.c_str(), labelRight);
@@ -56,28 +61,36 @@
screen->locate(15, 1);
screen->putc(editRight);
}
+ //Otherwise show the return command
else
- {
+ {
screen->printf("%cReturn ", leftArrow);
}
}
+//Handler for enter button
void Menu::enter()
{
+ //Menu not on screen, display it
if(!display && !ignoreNextEnter)
{
display = true;
edit = false;
}
+ //Menu on screen
else
{
+ //On an entry
if(position <= entryCount - 1)
{
+ //If its editable, toggle edit status
if(entries[position]->editable)
edit = !edit;
}
+ //On return
else
{
+ //Return, resetting position first
position = 0;
done();
}
@@ -86,31 +99,40 @@
ignoreNextEnter = false;
}
+//Handler for enter held, clear the menu retaining position
void Menu::done()
{
+ //Hide the menu
display = false;
+ //TODO: Can't remember why I needed this
ignoreNextEnter = true;
}
+//Handler for left button
void Menu::left()
{
+ //If not editing, scroll
if(!edit && display && position > 0)
position--;
+ //If editing, edit
else if(edit)
entries[position]->decrement();
}
+//Handler for left button held, start scrolling through menu.
void Menu::leftHeld()
{
left();
leftHeldTimeout.attach(this, &Menu::leftHeld, holdRepeatTime);
}
+//Handler for left button release, stop scrolling
void Menu::cancelLeftHeld()
{
leftHeldTimeout.detach();
}
+//Same as left
void Menu::right()
{
if(!edit && display && position < entryCount)
@@ -119,12 +141,14 @@
entries[position]->increment();
}
+//Same as left
void Menu::rightHeld()
{
right();
rightHeldTimeout.attach(this, &Menu::rightHeld, holdRepeatTime);
}
+//Same as left
void Menu::cancelRightHeld()
{
rightHeldTimeout.detach();
--- a/src/bigchar.cpp Mon Jun 25 21:01:02 2012 +0000
+++ b/src/bigchar.cpp Mon Jun 25 21:20:22 2012 +0000
@@ -1,6 +1,7 @@
#include "TextLCD.h"
#include "bigchar.h"
+//Set up character memory on LCD for drawing two line numbers
void setup_bigchar(TextLCD* lcd)
{
int top_left[8] = {0x07, 0x0F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, };
@@ -28,6 +29,7 @@
lcd->writeCGRAM(TWO_LINES_5, two_lines_5);
}
+//Draw two-line numbers
void write_bigchar(TextLCD* lcd, int position, int number)
{
switch(number)
