diff --git a/comms.h b/comms.h new file mode 100644 index 00000000..62cd15f8 --- /dev/null +++ b/comms.h @@ -0,0 +1,8 @@ +#include +#include "globals.h" + +void command(); +void sendValues(); +void saveConfig(); +void sendPage(); +void testComm(); diff --git a/comms.ino b/comms.ino new file mode 100644 index 00000000..1a201a28 --- /dev/null +++ b/comms.ino @@ -0,0 +1,109 @@ +void command() +{ + switch (Serial.read()) + { + case 'A': // send 22 bytes of realtime values + sendValues(22); + break; + + case 'B': // store to eeprom + saveConfig(); + break; + + case 'C': // test communications + testComm(); + break; + + case 'P': // set the current page + digitalWrite(10, HIGH); + digitalWrite(9, LOW); + digitalWrite(13, LOW); + Serial.read(); //Not doing anything with this currently, but need to read the 2nd byte from the buffer + break; + + case 'R': // send 39 bytes of realtime values + sendValues(39); + break; + + case 'S': // send code version + Serial.write(ms_version); + break; + + case 'Q': // send code version + digitalWrite(9, LOW); + digitalWrite(10, LOW); + digitalWrite(13, LOW); + Serial.write(ms_version); + break; + + case 'V': // send VE table and constants + digitalWrite(9, LOW); + digitalWrite(10, LOW); + digitalWrite(13, HIGH); + sendPage(); + break; + + case 'W': // receive new VE or constant at 'W'++ + digitalWrite(9, HIGH); + digitalWrite(10, LOW); + digitalWrite(13, LOW); + Serial.read(); + Serial.read(); //Not doing anything with this currently, but need to read the next 2 bytes from the buffer + break; + + default: + break; + } +} + +void sendValues(int length) +{ + byte response[22]; + + response[0] = (uint8_t)1; //rtc.sec; + + boolean a = 0; //inj_port1.status; + boolean b = 0; //inj_port2.status; + response[1] = ((a & 0x01) << 0) | ((a & 0x02) << 1) | ((a & 0x04) << 1) | ((b & 0x01) << 1) | ((b & 0x02) << 3) | ((b & 0x04) << 3); //squirt + + response[2] = 0; // Engine Status + response[3] = 0x00; //baro + response[4] = 0x00; //map + response[5] = 0x00; //mat + response[6] = 0x00; //Coolant + response[7] = 0x00; //TPS + response[8] = 0x00; //battery voltage + response[9] = 0x00; //O2 + response[10] = 0x00; //Exhaust gas correction (%) + response[11] = 0x00; //Air Correction (%) + response[12] = 0x00; //Warmup enrichment (%) + response[13] = (rpm / 100); //rpm / 100 + response[14] = 0x00; //Pulsewidth 1 divided by 10 (in ms) + response[15] = 0x00; //acceleration enrichment (ms) + response[16] = 0x00; //Barometer correction (%) + response[17] = 0x00; //Total GammaE (%) + response[18] = 0x00; //Current VE 1 (%) + response[19] = 0x00; //Pulsewidth 2 divided by 10 (in ms) + response[20] = 0x00; //mCurrent VE 2 (%) + response[21] = 0x00; //Idle + + Serial.write(response, (size_t)22); + return; +} + +void saveConfig() +{ + return; +} + +void sendPage() +{ + Serial.write((uint8_t *)&fuelTable.values, sizeof(fuelTable.values)); + return; +} + +void testComm() +{ + Serial.write(1); + return; +} diff --git a/globals.h b/globals.h new file mode 100644 index 00000000..75a16647 --- /dev/null +++ b/globals.h @@ -0,0 +1,3 @@ +#include + +byte ms_version = 20; diff --git a/kartduino.ino b/kartduino.ino index 3971eebc..01bb5d87 100644 --- a/kartduino.ino +++ b/kartduino.ino @@ -30,6 +30,7 @@ Need to calculate the req_fuel figure here, preferably in pre-processor macro #include "table.h" #include "testing.h" #include "scheduler.h" +#include "comms.h" #include "fastAnalog.h" #include "digitalIOPerformance.h" @@ -116,6 +117,10 @@ void setup() { dummyIgnitionTable(&ignitionTable); initialiseScheduler(); counter = 0; + + //Setup some LEDs for testing + pinMode(10, OUTPUT); + pinMode(9, OUTPUT); } void loop() @@ -133,7 +138,7 @@ void loop() rpm = US_IN_MINUTE / revolutionTime; } //Serial.print("RPM: "); Serial.println(rpm); - rpm = 1000; + //rpm = 1000; //Get the current MAP value int MAP = 20; //Placeholder int TPS = 20; //Placeholder @@ -184,6 +189,12 @@ void loop() ); } + //Check for any requets from serial + if (Serial.available() > 0) + { + command(); + } + //Serial.println(VE); //Serial.print("VE: ");