Small project to display some OBD values from the Toyota GT86/ Subaru BRZ/ Scion FRS on an OLED display.
Dependencies: Adafruit_GFX MODSERIAL mbed-rtos mbed
Revision 1:ca506b88b1d6, committed 2014-04-22
- Comitter:
- chrta
- Date:
- Tue Apr 22 15:32:49 2014 +0000
- Parent:
- 0:6b1f6139fb25
- Child:
- 2:d3d61d9d323e
- Commit message:
- Add minimum serial port interface.
Changed in this revision
IsoTpHandler.h | Show annotated file Show diff for this revision Revisions of this file |
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/IsoTpHandler.h Tue Apr 22 14:51:04 2014 +0000 +++ b/IsoTpHandler.h Tue Apr 22 15:32:49 2014 +0000 @@ -9,12 +9,37 @@ class IsoTpHandler { private: + + /** + * Represents a state. + */ class State { public: + + /** + * Empty destructor. + */ virtual ~State() {} + + /** + * Processes the received can message. + * + * \param[in] message This can message it processed. + * \param context This state machine (context) is used. + */ virtual void processInput(const CANMessage* message, IsoTpHandler* context) const = 0; + + /** + * This method is called when this state is entered. + * \param context This state machine (context) is used. + */ virtual void onEnter(IsoTpHandler* context) const = 0; + + /** + * This method is called when leaving this state. + * \param context This state machine (context) is used. + */ virtual void onLeave(IsoTpHandler* context) const = 0; }; @@ -44,25 +69,71 @@ virtual void onLeave(IsoTpHandler* context) const; }; public: + + /** + * Constructor + * + * \param[in] canInterface The can interface the packets should be sent to. It must not be NULL. + */ IsoTpHandler(CAN* canInterface); - + + /** + * Processes the given can message. + * + * This is the main method. It must be called for every received IsoTp can packet. + * It updates the internal state and sends the can response. + * + * \param[in] message The received can message. It must not be NULL. + */ void processCanMessage(const CANMessage* message); + /** + * This method is called when a complete Iso Tp message was received. + * + * Currently the packet is only printed out. + * Later a user callbeck must be executed from here. + * + * \param[in] data The content of the Iso Tp message. + * \param[in] length The amount of bytes in data. + */ void handle_decoded_packet(const uint8_t* data, uint16_t length); /** * + * \param[in] messageSize Total bytes included in the consequtive transfer. * \param[in] data Always 6 bytes. */ void init_consequtive_reading(uint16_t messageSize, const uint8_t* data); + + /** + * Returns the next expected index value of the can packet. + * + * \return The next expected index value. + */ uint8_t getExpectedIndex() const; + + /** + * Increments the expected index. + * + * This method ensures an automatic wrap around from 15 to 0. + */ void incrementExpectedIndex(); + /** + * Appends the given data to the internal consequtive transfer buffer. + * * \retval \c True if the state should be switched. * \retval \c False if the state not change. */ bool appendReceivedData(const uint8_t* data, uint8_t length); + /** + * Modifies the internal state. + * + * Should not be used externally. + * + * \param[in] The new state. + */ void setState(const State* state); static const IdleState idleState; @@ -71,15 +142,25 @@ static bool isValidIsoTpPacket(const CANMessage* message); private: + + /** The current state. */ const State* m_state; + /** The used can interface. */ CAN* m_canInterface; + /** This buffer must be able to store the maximum message size. */ uint8_t m_messageBuffer[256]; + + /** The expected size of the current message. */ uint16_t m_expectedMessageSize; + + /** The current size of the message that is currently received. */ uint16_t m_currentMessageSize; + + /** Stores the index that is expected in the next can packet. */ uint8_t m_expectedIndex; }; -#endif //ISO_TP_HANDLER \ No newline at end of file +#endif //ISO_TP_HANDLER
--- a/main.cpp Tue Apr 22 14:51:04 2014 +0000 +++ b/main.cpp Tue Apr 22 15:32:49 2014 +0000 @@ -1,7 +1,8 @@ #include "mbed.h" #include "rtos.h" #include "IsoTpHandler.h" - + +Serial pc(USBTX, USBRX); // tx, rx DigitalOut led1(LED1); DigitalOut led2(LED2); CAN can2(p30, p29); @@ -42,8 +43,45 @@ error("Putting can message into mailbox failed with code %d!", error); } } + +void serial_int_handler() { + if (!pc.readable()) { + return; + } + uint8_t character = pc.getc(); + printf("Received '%c'\n", character); + + char can_msg[8] = {0}; + can_msg[0] = 0x02; + can_msg[1] = 0x01; + char pid = 0; + switch (character) + { + case '1': + pid = 0x0C; //engine rpm + break; + case '2': + pid = 0x11; //throttle + break; + case '3': //oil 1 + can_msg[1] = 0x21; //endian + pid = 1; + break; + case '4': //oil 2 + can_msg[1] = 1; //endian + pid = 0x21; + break; + default: + pid = 0x05; //engine coolant temp + } + can_msg[2] = pid; + + printf("Can write %d\n", can2.write(CANMessage(0x7DF, can_msg, 8))); + } int main() { + pc.baud(115200); + pc.attach(serial_int_handler); can2.frequency(500000); can2.attach(can_rx_int_handler); Thread thread(led2_thread);