![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Test code for interfacing to megasquirt ECU.
main.cpp@0:cf99a7b873b3, 2015-02-14 (annotated)
- Committer:
- jont
- Date:
- Sat Feb 14 08:50:32 2015 +0000
- Revision:
- 0:cf99a7b873b3
- Child:
- 2:57a6c415e495
First version checkpoint
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jont | 0:cf99a7b873b3 | 1 | #include "mbed.h" |
jont | 0:cf99a7b873b3 | 2 | #include "jtlcd.h" |
jont | 0:cf99a7b873b3 | 3 | #include "Counter.h" |
jont | 0:cf99a7b873b3 | 4 | |
jont | 0:cf99a7b873b3 | 5 | |
jont | 0:cf99a7b873b3 | 6 | float coolant; |
jont | 0:cf99a7b873b3 | 7 | int int_coolant = 0; |
jont | 0:cf99a7b873b3 | 8 | void callback_reed(void); |
jont | 0:cf99a7b873b3 | 9 | void siphon_data(); |
jont | 0:cf99a7b873b3 | 10 | |
jont | 0:cf99a7b873b3 | 11 | DigitalOut myled(LED1); |
jont | 0:cf99a7b873b3 | 12 | DigitalOut myled2(LED2); |
jont | 0:cf99a7b873b3 | 13 | DigitalOut myled3(LED3); |
jont | 0:cf99a7b873b3 | 14 | DigitalOut myled4(LED4); |
jont | 0:cf99a7b873b3 | 15 | |
jont | 0:cf99a7b873b3 | 16 | |
jont | 0:cf99a7b873b3 | 17 | DigitalIn reed(p9); //would default to in but we wanna play with its mode |
jont | 0:cf99a7b873b3 | 18 | Counter counter_reed(p9, callback_reed ); //setup the counter to use pin 9 |
jont | 0:cf99a7b873b3 | 19 | |
jont | 0:cf99a7b873b3 | 20 | void sioRXHandler(void); |
jont | 0:cf99a7b873b3 | 21 | #define combuffsize 256 |
jont | 0:cf99a7b873b3 | 22 | char comBuff[combuffsize]; /* space for commands yeh its small make bigger if you need to */ |
jont | 0:cf99a7b873b3 | 23 | int comBuffIndex; /* buffer index */ |
jont | 0:cf99a7b873b3 | 24 | |
jont | 0:cf99a7b873b3 | 25 | char lineBuffer[32]; //for saving data for the lcd display |
jont | 0:cf99a7b873b3 | 26 | |
jont | 0:cf99a7b873b3 | 27 | Serial pc(USBTX, USBRX); // tx, rx |
jont | 0:cf99a7b873b3 | 28 | |
jont | 0:cf99a7b873b3 | 29 | /*=====================================================================================*/ |
jont | 0:cf99a7b873b3 | 30 | // |
jont | 0:cf99a7b873b3 | 31 | // callback funtions mostly for the counters |
jont | 0:cf99a7b873b3 | 32 | // |
jont | 0:cf99a7b873b3 | 33 | /*=====================================================================================*/ |
jont | 0:cf99a7b873b3 | 34 | /*=====================================================================================*/ |
jont | 0:cf99a7b873b3 | 35 | // callback for the reed switch input |
jont | 0:cf99a7b873b3 | 36 | // puts logentry into the ringbuffer |
jont | 0:cf99a7b873b3 | 37 | /*=====================================================================================*/ |
jont | 0:cf99a7b873b3 | 38 | void callback_reed() |
jont | 0:cf99a7b873b3 | 39 | { |
jont | 0:cf99a7b873b3 | 40 | time_t seconds = time(NULL); |
jont | 0:cf99a7b873b3 | 41 | //LPC_RTC->GPREG3 = counter_reed.read(); |
jont | 0:cf99a7b873b3 | 42 | __disable_irq(); // Disable Interrupts |
jont | 0:cf99a7b873b3 | 43 | comBuffIndex = 0; |
jont | 0:cf99a7b873b3 | 44 | __enable_irq(); |
jont | 0:cf99a7b873b3 | 45 | pc.putc('A'); |
jont | 0:cf99a7b873b3 | 46 | |
jont | 0:cf99a7b873b3 | 47 | } |
jont | 0:cf99a7b873b3 | 48 | |
jont | 0:cf99a7b873b3 | 49 | void serialInit(void) |
jont | 0:cf99a7b873b3 | 50 | { |
jont | 0:cf99a7b873b3 | 51 | pc.attach(&sioRXHandler); |
jont | 0:cf99a7b873b3 | 52 | } |
jont | 0:cf99a7b873b3 | 53 | |
jont | 0:cf99a7b873b3 | 54 | |
jont | 0:cf99a7b873b3 | 55 | /*======================================================================*/ |
jont | 0:cf99a7b873b3 | 56 | /* sioRXHandler */ |
jont | 0:cf99a7b873b3 | 57 | /*======================================================================*/ |
jont | 0:cf99a7b873b3 | 58 | /* |
jont | 0:cf99a7b873b3 | 59 | This is inefficient and long handed but makes explaining it to someone |
jont | 0:cf99a7b873b3 | 60 | easier. |
jont | 0:cf99a7b873b3 | 61 | |
jont | 0:cf99a7b873b3 | 62 | Which is the point, to be understandable at this stage :-) |
jont | 0:cf99a7b873b3 | 63 | |
jont | 0:cf99a7b873b3 | 64 | |
jont | 0:cf99a7b873b3 | 65 | If more data received than buffer length we throw it away! |
jont | 0:cf99a7b873b3 | 66 | */ |
jont | 0:cf99a7b873b3 | 67 | void sioRXHandler(void) |
jont | 0:cf99a7b873b3 | 68 | { |
jont | 0:cf99a7b873b3 | 69 | char theChar = 0x00; |
jont | 0:cf99a7b873b3 | 70 | myled2 = 1; |
jont | 0:cf99a7b873b3 | 71 | myled3 = 1; |
jont | 0:cf99a7b873b3 | 72 | while (pc.readable()){ |
jont | 0:cf99a7b873b3 | 73 | theChar = pc.getc(); |
jont | 0:cf99a7b873b3 | 74 | pc.putc(theChar); //echo it back |
jont | 0:cf99a7b873b3 | 75 | if (comBuffIndex >= combuffsize - 1) |
jont | 0:cf99a7b873b3 | 76 | { |
jont | 0:cf99a7b873b3 | 77 | comBuffIndex = combuffsize - 1; |
jont | 0:cf99a7b873b3 | 78 | } |
jont | 0:cf99a7b873b3 | 79 | comBuff[comBuffIndex] = theChar; |
jont | 0:cf99a7b873b3 | 80 | comBuffIndex++; |
jont | 0:cf99a7b873b3 | 81 | } |
jont | 0:cf99a7b873b3 | 82 | myled2 = 0; |
jont | 0:cf99a7b873b3 | 83 | } |
jont | 0:cf99a7b873b3 | 84 | |
jont | 0:cf99a7b873b3 | 85 | /*======================================================================*/ |
jont | 0:cf99a7b873b3 | 86 | /* Main */ |
jont | 0:cf99a7b873b3 | 87 | /*======================================================================*/ |
jont | 0:cf99a7b873b3 | 88 | int main() { |
jont | 0:cf99a7b873b3 | 89 | setbuf(stdout, NULL); |
jont | 0:cf99a7b873b3 | 90 | // no buffering for this filehandle, this stop the weird |
jont | 0:cf99a7b873b3 | 91 | //behaviour with things not being dfiplaye untill a character was received. |
jont | 0:cf99a7b873b3 | 92 | serialInit(); |
jont | 0:cf99a7b873b3 | 93 | reed.mode(PullUp); |
jont | 0:cf99a7b873b3 | 94 | |
jont | 0:cf99a7b873b3 | 95 | LcdInit(); |
jont | 0:cf99a7b873b3 | 96 | LcdHomeTop(); |
jont | 0:cf99a7b873b3 | 97 | LcdWriteTextLine("GM0HYY"); |
jont | 0:cf99a7b873b3 | 98 | LcdHomeBottom(); |
jont | 0:cf99a7b873b3 | 99 | LcdWriteTextLine("Hello World"); |
jont | 0:cf99a7b873b3 | 100 | while(1) { |
jont | 0:cf99a7b873b3 | 101 | siphon_data(); |
jont | 0:cf99a7b873b3 | 102 | myled = 1; |
jont | 0:cf99a7b873b3 | 103 | wait(0.2); |
jont | 0:cf99a7b873b3 | 104 | myled = 0; |
jont | 0:cf99a7b873b3 | 105 | wait(0.2); |
jont | 0:cf99a7b873b3 | 106 | LcdHomeBottom(); |
jont | 0:cf99a7b873b3 | 107 | sprintf(lineBuffer,"%.3f %04X %3d", coolant, int_coolant, counter_reed.read()); |
jont | 0:cf99a7b873b3 | 108 | LcdWriteTextLine(lineBuffer); |
jont | 0:cf99a7b873b3 | 109 | } |
jont | 0:cf99a7b873b3 | 110 | } |
jont | 0:cf99a7b873b3 | 111 | |
jont | 0:cf99a7b873b3 | 112 | void siphon_data(){ |
jont | 0:cf99a7b873b3 | 113 | __disable_irq(); // Disable Interrupts |
jont | 0:cf99a7b873b3 | 114 | |
jont | 0:cf99a7b873b3 | 115 | int_coolant = 0; |
jont | 0:cf99a7b873b3 | 116 | if(comBuffIndex > 24){ |
jont | 0:cf99a7b873b3 | 117 | int_coolant = comBuff[23]; |
jont | 0:cf99a7b873b3 | 118 | int_coolant <<= 8; |
jont | 0:cf99a7b873b3 | 119 | int_coolant |= comBuff[22]; |
jont | 0:cf99a7b873b3 | 120 | coolant = (int_coolant-320) * 0.05555; |
jont | 0:cf99a7b873b3 | 121 | } |
jont | 0:cf99a7b873b3 | 122 | __enable_irq(); |
jont | 0:cf99a7b873b3 | 123 | |
jont | 0:cf99a7b873b3 | 124 | } |