Library to control the mp3-tf-16p
Revision 2:4697650da797, committed 2022-01-25
- Comitter:
- JeroenAero
- Date:
- Tue Jan 25 19:26:21 2022 +0000
- Parent:
- 1:c76171b5e9df
- Commit message:
- Version1;
Changed in this revision
diff -r c76171b5e9df -r 4697650da797 LAS_TB.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/LAS_TB.cpp Tue Jan 25 19:26:21 2022 +0000 @@ -0,0 +1,70 @@ +#include "mbed.h" +#include "LAS_TB.h" + +LAS_TB::LAS_TB(PinName PinTX, PinName PinRX) : SerialPort(PinTX,PinRX) +{ + //constructor. + SerialPort.baud(115200); + SerialPort.attach(callback(this,&LAS_TB::Rx_interrupt), Serial::RxIrq); + + //Dit verzenden we naar de LAS-TB. Bevat het order get measured.... + Stack[0] = 0x55; //Sync byte. + Stack[1] = 0x08; //Order (Get measerued values from L-LAS-RAM.) + Stack[2] = 0x00; //ARG LO. + Stack[3] = 0x00; //ARG HI. + Stack[4] = 0x00; //LEN LO. + Stack[5] = 0x00; //LEN HI. + Stack[6] = 0xAA; //CRC8 HEAD. + Stack[7] = 0x76; //CRC8 DATA. +} + +union Converter +{ + int32_t um; // occupies 4 bytes + uint8_t byteArray[4]; // occupies 4 bytes +}; + +//Vragen om een meting. +void LAS_TB::MeasurementRequest() +{ + SendStack(); +} + +//Wordt uitgevoerd bij ontvangen bericht. +void LAS_TB::Rx_interrupt() +{ + while (SerialPort.readable()) + { + for (int i = 0; i < 71; i++) + { + ReceivedStack[i] = ReceivedStack[i+1]; + } + ReceivedStack[71] = SerialPort.getc(); + + //Hoe weten we dat het bericht goed is? + if (ReceivedStack[0] == 0x55 && ReceivedStack[1] == 0x08 && ReceivedStack[4] == 64) + { + int32_t temp = ReceivedStack[16]; + + umvalue = temp << 24; + temp = ReceivedStack[17]; + umvalue += temp << 16; + temp = ReceivedStack[18]; + umvalue += temp << 8; + temp = ReceivedStack[19]; + umvalue += temp; + } + } +} + +//Het sturen van data. +//Sends the stack of data to the sensor module. +void LAS_TB::SendStack() +{ + + for (int i = 0; i < 10; i++) + { + SerialPort.putc(Stack[i]); + } + +} \ No newline at end of file
diff -r c76171b5e9df -r 4697650da797 LAS_TB.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/LAS_TB.h Tue Jan 25 19:26:21 2022 +0000 @@ -0,0 +1,27 @@ +#ifndef MBED_LAS_TB_H +#define MBED_LAS_TB_H + +#include "mbed.h" + +class LAS_TB { + + public: + + LAS_TB(PinName PinTX, PinName PinRX); + void MeasurementRequest(); + int32_t umvalue; + + private: + + Serial SerialPort; + uint8_t ReceivedStack[72]; + uint8_t Stack[8]; + + union Converter; + + + void SendStack(); + void Rx_interrupt(); +}; + +#endif \ No newline at end of file
diff -r c76171b5e9df -r 4697650da797 MP3_TF_16P.cpp --- a/MP3_TF_16P.cpp Fri Mar 12 12:35:52 2021 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,119 +0,0 @@ -#include "mbed.h" -#include "MP3_TF_16P.h" - -MP3_TF_16P::MP3_TF_16P(PinName PinTX, PinName PinRX) : SerialPort(PinTX,PinRX) -{ - //constructor. - SerialPort.baud(9600); - SerialPort.attach(callback(this,&MP3_TF_16P::Rx_interrupt), Serial::RxIrq); - - Stack[0] = 0x7E; //Start byte (always 7E) - Stack[1] = 0xFF; //Version (always FF) - Stack[2] = 0x06; //Length (always 6) - Stack[3] = 0x00; //Command - Stack[4] = 0x00; //Feedback on/off - Stack[5] = 0x00; //Parameter high byte - Stack[6] = 0x00; //Parameter low byte - Stack[7] = 0x00; //Checksum high byte - Stack[8] = 0x00; //Checksum low byte - Stack[9] = 0xEF; //End byte (always EF) -} - -void MP3_TF_16P::SetVolume(uint16_t Value) -{ - Stack[3] = 0x06; //Put commando in stack. - Stack[4] = 0x00; //No feedback required. - PutUint16InArray(Value,5,6); //Put value in stack. - SendStack(); -} - -void MP3_TF_16P::PlayTrackNumber(uint16_t Number) -{ - Stack[3] = 0x03; //Put commando in stack. - Stack[4] = 0x00; //No feedback required. - PutUint16InArray(Number,5,6); //Put value in stack. - SendStack(); -} - -void MP3_TF_16P::PausePlay() -{ - Stack[3] = 0x0E; //Put commando in stack. - Stack[4] = 0x00; //No feedback required. - SendStack(); -} - -void MP3_TF_16P::RepeatPlay(uint16_t Number) -{ - Stack[3] = 0x08; //Put commando in stack. - Stack[4] = 0x00; //No feedback required. - PutUint16InArray(Number,5,6); //Put value in stack. - SendStack(); -} - -//Sends the stack of data to the MP3-TF-16P module. -void MP3_TF_16P::SendStack() -{ - CalculateCheckSum(); //First calculate the checksum. - - for (int i = 0; i < 10; i++) - { - SerialPort.putc(Stack[i]); - } -} - -void MP3_TF_16P::CalculateCheckSum() -{ - uint16_t sum = 0; - - //Calculate checksum over bytes 1 to 6. - for (int i = 1; i < 7; i++) - { - sum += Stack[i]; - } - - //Invert checksum. - sum = -sum; - - //Put result in stack. - PutUint16InArray(sum,7,8); -} - -void MP3_TF_16P::PutUint16InArray(uint16_t value, uint8_t PlaceHighByte, uint8_t PlaceLowByte) -{ - //Put 16 bits usigned integer in stack at the requested places. - Stack[PlaceHighByte] = (uint8_t)(value>>8); //Shift bits to the right. - Stack[PlaceLowByte] = (uint8_t)(value); -} - -void MP3_TF_16P::Rx_interrupt() -{ - while (SerialPort.readable()) - { - for (int i = 0; i < 9; i++) - { - ReceivedStack[i] = ReceivedStack[i+1]; - } - ReceivedStack[9] = SerialPort.getc(); - - if (ReceivedStack[0] == 0x7E && ReceivedStack[1] == 0xFF && ReceivedStack[2] == 0x06 && ReceivedStack[9] == 0xEF) - { - if (ReceivedStack[3] == 0x48) - { - numberoffiles = ReceivedStack[6]; - } - if (ReceivedStack[3] == 0x40) - { - numberoffiles = 0; - } - } - } -} - -uint16_t MP3_TF_16P::GetNumberOfFiles() -{ - Stack[3] = 0x48; //Put commando in stack. - Stack[4] = 0x00; //No feedback required. - SendStack(); - wait(0.2); - return numberoffiles; -} \ No newline at end of file
diff -r c76171b5e9df -r 4697650da797 MP3_TF_16P.h --- a/MP3_TF_16P.h Fri Mar 12 12:35:52 2021 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,40 +0,0 @@ -#ifndef MBED_MP3_TF_16P_H -#define MBED_MP3_TF_16P_H - -#include "mbed.h" - -class MP3_TF_16P { - - public: - - MP3_TF_16P(PinName PinTX, PinName PinRX); - - void PlayTrackNumber(uint16_t Number); - void SetVolume(uint16_t Value); - void PausePlay(); - void RepeatPlay(uint16_t Number); - uint16_t GetNumberOfFiles(); - - - - uint8_t ReceivedStack[10]; - - private: - - uint16_t numberoffiles; - Serial SerialPort; - - uint8_t Stack[10]; - - void SendStack(); - void CalculateCheckSum(); - void PutUint16InArray(uint16_t number, uint8_t PlaceHighByte, uint8_t PlaceLowByte); - - - void Rx_interrupt(); - - - -}; - -#endif \ No newline at end of file