![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Gets messages form the pc and translates it to I2C and back.
Dependencies: DevInterfaces I2Cinterfaces MCP4725 mbed
Diff: main.cpp
- Revision:
- 0:b40341017545
- Child:
- 1:8ba039abd9b8
diff -r 000000000000 -r b40341017545 main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Wed May 18 11:22:41 2016 +0000 @@ -0,0 +1,104 @@ +#define MCP4728EXAMPLEVER "1.10 " + +#include <string> +#include "mbed.h" + +#if defined (TARGET_KL25Z) || defined (TARGET_KL46Z) + PinName const SDA = PTE25; + PinName const SCL = PTE24; +#elif defined (TARGET_KL05Z) + PinName const SDA = PTB4; + PinName const SCL = PTB3; +#elif defined (TARGET_K20D50M) + PinName const SDA = PTB1; + PinName const SCL = PTB0; +#else + #error TARGET NOT DEFINED +#endif + + +#include "I2C.h" +#include "I2CInterface.h" +#include "MBEDI2CInterface.h" +#include "DACInterface.h" +#include "dev_interface_def.h" +#include "mcp4725.h" +#include "Translator.h" +#include "Reader.h" +#include "Writer.h" + +// Initialize LEDs +PwmOut rled(LED1); +PwmOut gled(LED2); +PwmOut bled(LED3); + +Serial pc(USBTX, USBRX); + +int main(void) { + // <SDA en SCL zijn de pins hierboven gedefinieerd, wss hardware eigenschappen dat je mag negeren> + // Create MbedI2C interface + MBEDI2CInterface mbedi2c(SDA, SCL); + // <volgende lijn is voor indien de Mbed veranderd, enkel bovenstaande lijn geupdate moet worden> + // Create pointer to MBEDI2CInterface's parent + I2CInterface* i2cdev= &mbedi2c; + // Set maximum voltage of I2C board + float Vdd = 4.7; + // Create communication object using the MbedI2C interface + MCP4725 dac(i2cdev ,0, Vdd); + + // Create input buffer reader + Reader reader; + + // Start infinite loop + while (true) { + rled = 1.0f; + gled = 1.0f; + bled = 0.0f; + if (!reader.IsNewMessageReceived()) + continue; // Do nothing as long as no new complete message was received + rled = 1.0f; + gled = 0.0f; + bled = 1.0f; + + // Translate the raw message + Translator::MessageInfo info/* = reader.LastMessageInfo()*/; + if (!Translator::Translate(reader.GetLastMessage(), reader.GetLastMessageSize(), &info)) { + printf("I have failed you..."); + continue; + } + + int dacValue; + if (info.IsRead()) { // If read command + printf("Reading "); + // Read the current DAC value + if (dac.getDACvalue(dacValue) == 0) { + printf("succes!"); + // Succesful: send back the current DAC value + Writer::Send(info, dacValue); + } else { + printf("failed"); + // Failed: send error message + Writer::SendError(info); + } + } else if (info.IsWrite()) { // If write command + printf("Writing "); + // Set the given DAC value + if (dac.setDACvalue(info.DacValue) == 0) { + printf("succes!"); + // Succesful: Get the DAC value + dac.getDACvalue(dacValue); + // Send new value + Writer::Send(info, dacValue); + } else { + printf("failed"); + // Failed: send error message + Writer::SendError(info); + } + } else if (info.IsOption()) { + printf("Option failed"); + // Not implemented yet + Writer::SendError(info); + } + } +} +