
* Routeur
Diff: main.cpp
- Revision:
- 0:b399a75f4990
- Child:
- 1:2abb01319ca6
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Sat Feb 11 23:45:12 2017 +0000 @@ -0,0 +1,122 @@ +#include "ConfigFile.h" +#include "mbed.h" + +DigitalOut myled(LED1); +DigitalOut reset(p8); +Serial uart(p13, p14); // tx, rx +Serial pc(USBTX, USBRX); // tx, rx + +uint8_t calculateChecksum(uint8_t frameSize, uint8_t* buffer) { + uint32_t checkSum = 0; + uint8_t frameTypeIndex = 3; + + for (int i = frameTypeIndex; i < frameSize - 1; i++) { + checkSum += buffer[i]; + } + + return (uint8_t)(0xFF - (checkSum & 0xFF)); +} + +uint16_t getFrameSize(uint8_t value1, uint8_t value2) { + union { + uint16_t u16_value; + uint8_t u8_value[2]; + } length; + + length.u8_value[1] = value1; + length.u8_value[0] = value2; + + return length.u16_value; +} + +/*void readFrame(){ + uint8_t temp[2] = { 0 }; + uint8_t buffer[104] = { 0 }; + + if (uart.readable() && uart.getc() == 0x7E) { + + for (int i = 0; i < 2;) { + if(uart.readable()) { + temp[i] = uart.getc(); + i++; + } + } + uint16_t frameSize = getFrameSize(temp[0], temp[1]); + + for(int j = 0; j < frameSize + 1;) { + if (uart.readable()) { + buffer[j] = uart.getc(); + j++; + } + } + + pc.printf("Frame: %s\n", buffer); + } +}*/ + +void sendFrame(uint8_t bufferSize, uint8_t* buffer) { + if (uart.writeable()) { + uart.printf("%s", buffer); + } + /*for (int i = 0; i < bufferSize;) { + if (uart.writeable()) { + uart.putc(buffer[i]); + i++; + } + }*/ +} + +void setFrame() { + const uint8_t bufferSize = 22; + uint8_t buffer[bufferSize] = { 0 }; + + buffer[0] = 0x7E; // Start Delimiter + buffer[1] = 0x00; // Length + buffer[2] = 0x12; // Length + buffer[3] = 0x10; // Frame Type + buffer[4] = 0x01; // Frame ID + buffer[5] = 0x00; // 64 bit adress + buffer[6] = 0x00; // 64 bit adress + buffer[7] = 0x00; // 64 bit adress + buffer[8] = 0x00; // 64 bit adress + buffer[9] = 0x00; // 64 bit adress + buffer[10] = 0x00; // 64 bit adress + buffer[11] = 0x00; // 64 bit adress + buffer[12] = 0x00; // 64 bit adress + buffer[13] = 0xFF; // 16 bit adress + buffer[14] = 0xFE; // 16 bit adress + buffer[15] = 0x00; // Broadcast radius + buffer[16] = 0x00; // Options + buffer[17] = 0x02; // RF Data... + buffer[18] = 0x02; // RF Data... + buffer[19] = 0x02; // RF Data... + buffer[20] = 0x02; // RF Data... + buffer[21] = calculateChecksum(bufferSize, buffer); // Checksum + + pc.printf("Frame: %s\n", buffer); + sendFrame(bufferSize, buffer); +} + +/* void readConfigFile() { + LocalFileSystem local("local"); + ConfigFile cfg; + char *serverAddr = "serverAddr"; + char *tempPortNo = "portNb"; + + if (!cfg.read("/local/input.cfg")) { + error("Erreur dans la lecture du fichier\n"); + } else { + cfg.getValue(serverAddr, &serverAddrvalue[0], sizeof(serverAddrvalue)); + cfg.getValue(tempPortNo, &portNbValue[0], sizeof(portNbValue)); + portNo = (uint16_t)strtol(portNbValue, NULL, 10); + } +}*/ + +int main() { + reset = 0; + wait(0.4); + reset = 1; + wait_ms(1); + + setFrame(); +}