![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Test code for interfacing to megasquirt ECU.
Diff: main.cpp
- Revision:
- 0:cf99a7b873b3
- Child:
- 2:57a6c415e495
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Sat Feb 14 08:50:32 2015 +0000 @@ -0,0 +1,124 @@ +#include "mbed.h" +#include "jtlcd.h" +#include "Counter.h" + + +float coolant; +int int_coolant = 0; +void callback_reed(void); +void siphon_data(); + +DigitalOut myled(LED1); +DigitalOut myled2(LED2); +DigitalOut myled3(LED3); +DigitalOut myled4(LED4); + + +DigitalIn reed(p9); //would default to in but we wanna play with its mode +Counter counter_reed(p9, callback_reed ); //setup the counter to use pin 9 + +void sioRXHandler(void); +#define combuffsize 256 +char comBuff[combuffsize]; /* space for commands yeh its small make bigger if you need to */ +int comBuffIndex; /* buffer index */ + +char lineBuffer[32]; //for saving data for the lcd display + +Serial pc(USBTX, USBRX); // tx, rx + +/*=====================================================================================*/ +// +// callback funtions mostly for the counters +// +/*=====================================================================================*/ +/*=====================================================================================*/ +// callback for the reed switch input +// puts logentry into the ringbuffer +/*=====================================================================================*/ +void callback_reed() +{ + time_t seconds = time(NULL); + //LPC_RTC->GPREG3 = counter_reed.read(); + __disable_irq(); // Disable Interrupts + comBuffIndex = 0; + __enable_irq(); + pc.putc('A'); + +} + +void serialInit(void) +{ + pc.attach(&sioRXHandler); +} + + +/*======================================================================*/ +/* sioRXHandler */ +/*======================================================================*/ +/* +This is inefficient and long handed but makes explaining it to someone +easier. + +Which is the point, to be understandable at this stage :-) + + +If more data received than buffer length we throw it away! +*/ +void sioRXHandler(void) +{ +char theChar = 0x00; +myled2 = 1; +myled3 = 1; + while (pc.readable()){ + theChar = pc.getc(); + pc.putc(theChar); //echo it back + if (comBuffIndex >= combuffsize - 1) + { + comBuffIndex = combuffsize - 1; + } + comBuff[comBuffIndex] = theChar; + comBuffIndex++; + } + myled2 = 0; +} + +/*======================================================================*/ +/* Main */ +/*======================================================================*/ +int main() { + setbuf(stdout, NULL); + // no buffering for this filehandle, this stop the weird + //behaviour with things not being dfiplaye untill a character was received. + serialInit(); + reed.mode(PullUp); + + LcdInit(); + LcdHomeTop(); + LcdWriteTextLine("GM0HYY"); + LcdHomeBottom(); + LcdWriteTextLine("Hello World"); + while(1) { + siphon_data(); + myled = 1; + wait(0.2); + myled = 0; + wait(0.2); + LcdHomeBottom(); + sprintf(lineBuffer,"%.3f %04X %3d", coolant, int_coolant, counter_reed.read()); + LcdWriteTextLine(lineBuffer); + } +} + +void siphon_data(){ +__disable_irq(); // Disable Interrupts + +int_coolant = 0; + if(comBuffIndex > 24){ + int_coolant = comBuff[23]; + int_coolant <<= 8; + int_coolant |= comBuff[22]; + coolant = (int_coolant-320) * 0.05555; + } +__enable_irq(); + +} \ No newline at end of file