Tympan / Tympan_Library

Arduino/Teensy Library for Tympan Open Source Hearing Aid
MIT License
122 stars 32 forks source link

Merge Feature_Add_Callback... to Main #65

Closed eyuan-creare closed 2 years ago

eyuan-creare commented 2 years ago

Added callback for multibyte serial messages. Tested using Realterm to send:

See attached script that modified an SD Card Writer example to register a callback function DataStream_example.zip

Here is background info copied from SerialManagerBase.h

DataStreams: Besides the single-character and four-character modes, there are also
             data streaming modes to support specialized communication.  These special modes
             are not inteded to be invoked by a user's GUI, so they can be ignored.  To avoid
             inadvertently invoking these modes, never send characters such as 0x02, 0x03, 0x04.
             In fact, you should generally avoid any non-printable character or you risk seeing
             unexpected behavior.

             Datastreams expect the following message protocol.  Note that the message length and payload are sent as little endian (LSB, MSB):
                1.  DATASTREAM_START_CHAR   (0x02)
                2.  Message Length (int32): number of bytes including parts-4 thru part-6
                3.  DATASTREAM_SEPARATOR    (0x03)
                4.  Message Type (char): Avoid using the special characters 0x03 or 0x04.  (if set to ‘test’, it will print out the payload as an int, then a float)
                5.  DATASTREAM_SEPARATOR    (0x03)
                6.  Payload
                7.  DATASTREAM_END_CHAR     (0x04)

            Use RealTerm to send a 'test' message: 
            0x02 0x0D 0x00 0x00 0x00 0x03 0x74 0x65 0x73 0x74 0x03 0xD2 0x02 0x96 0x49 0xD2 0x02 0x96 0x49 0x04
                1. DATASTREAM_START_CHAR    (0x02)
                2.  Message Length (int32): (0x000D) = 13 
                3.  DATASTREAM_SEPARATOR    (0x03)
                4.  Message Type (char):    (0x74657374) = 'test'
                5.  DATASTREAM_SEPARATOR    (0x03)
                6.  Payload                 (0x499602D2, 0x499602D2) = [1234567890, 1234567890]
                7.  DATASTREAM_END_CHAR     (0x04)

            - Expected output
                -  SerialManagerBase: RespondToByte: Start data stream.
                - SerialManagerBase: RespondToByte: Stream length = 13
                - SerialManagerBase: RespondToByte: Time to process stream!
                - Stream is of type 'test'.
                - int is 1234567890
                - float is 1228890.25

            To register a callback when a datastream message is received, use setDataStreamCallback() and set a unique message type (i.e. not 'gha', 'dsl', afc', or 'test'):
            - `serialManager.setDataStreamCallback(&dataStreamCallback);`
            - `void dataStreamCallback(char* payload_p, String *msgType_p, int numBytes)`
eyuan-creare commented 2 years ago

Here is a good article on misalignment and type punning

eyuan-creare commented 2 years ago

Sorry for all the shenanigans! I accidentally pushed to main instead. I'll reopen this pull request when everything is straightened out..

eyuan-creare commented 2 years ago

Added checks to ensure buffer is not overrun and that the length is sent as 4-bytes. The latter was tested with a message that erroneously has a 3-byte length: 0x02 0x08 0x00 0x00 0x03 0x00 0x00 0x04

eyuan-creare commented 2 years ago

@kxm-creare What other applications use this data stream? Should I test a hearing aid script to ensure I did not bugger up 4-character messages?

chipaudette commented 2 years ago

@kxm-creare, Hmm...I don't remember if we disabled the predefined pages or not. I'm pretty sure that we didn't expose them for any of our ASA participants. Yes, maybe the reason is because we disabled them!

I agree that I don't think that Haley tested any of that functionality. So, whether or not they are disabled, they are not tested.