code d'envoie de trame
Dependencies: mbed ConfigFile
Fork of app4-router by
Revision 4:21b8959e8b71, committed 2014-02-24
- Comitter:
- passelin
- Date:
- Mon Feb 24 18:27:45 2014 +0000
- Parent:
- 3:ef0e12857e30
- Child:
- 5:b4b4a64ef0d6
- Commit message:
- config file fonctionnel
Changed in this revision
ConfigFile.lib | 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 |
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ConfigFile.lib Mon Feb 24 18:27:45 2014 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/shintamainjp/code/ConfigFile/#f6ceafabe9f8
--- a/main.cpp Mon Feb 24 15:44:51 2014 +0000 +++ b/main.cpp Mon Feb 24 18:27:45 2014 +0000 @@ -3,8 +3,6 @@ #include <string> #include "ConfigFile.h" -//#define HIGH_RES 10 // Uncomment to operate the accelerometer in HIGH RESOLUTION(12 bits) otherwise, it would operates in LOW RESOLUTION(8 bits) - #define DELAY 0.25 #define MMA8452_WRITE_ADDRESS 0x3A @@ -19,20 +17,51 @@ #define CTRL_REG1 0x2A #define CTRL_REG2 0x2B -#define ACCEL 0x01 -#define PUSHBOUTON 0x02 +#define ACCEL_X 0x01 +#define ACCEL_Y 0x02 +#define ACCEL_Z 0x03 +#define PUSHBOUTON 0x04 DigitalOut myled(LED1); -DigitalOut myled2(LED2); +DigitalIn pb1(p5); DigitalOut reset(p8); Serial pc(USBTX, USBRX); Serial xbee(p13, p14); I2C i2c(p9, p10); // I2C port (sda, scl) -//LocalFileSystem local("local"); +LocalFileSystem local("local"); ConfigFile cfg; string AccelData; +int period_ms; + +void configInit() +{ + char *key1 = "PANID"; + char *key2 = "FREQ"; + char value[BUFSIZ]; + + // Read a configuration file from a mbed. + if (!cfg.read("/local/initconf.cfg")) + { + error("Failure to read a configuration file.\n"); + } + + /* + * Get a configuration value. + */ + if (cfg.getValue(key1, &value[0], sizeof(value))) + { + printf("'%s'='%s'\n", key1, value); + } + + if (cfg.getValue(key2, &value[0], sizeof(value))) + { + std::stringstream freq_s(value); + freq_s >> period_ms; + printf("'%s'='%d'\n", key2, period_ms); + } +} void xbee_init() { @@ -85,43 +114,35 @@ unsigned short getAccelValue(char MSB_addr, char LSB_addr) { - unsigned short retValue; - unsigned char msb; - - msb = i2c_read_reg(MSB_addr); // Read MSB data from MSB register - -#ifdef HIGH_RES // Use LSB only in HIGH RESOLUTION mode. - unsigned char lsb; - lsb = i2c_read_reg(LSB_addr); // Read LSB data from MSB register - retValue = (msb << 4); // Shift the data by 4 to the left in order to rebuild the 12 bits data ( MSB MSB MSB MSB MSB MSB MSB MSB 0 0 0 0 ) - retValue = ((lsb >> 4) | retValue); // Shift the data by 4 to the right in order to keep only the 4 first bits and rebuild the 12 bits data ( MSB MSB MSB MSB MSB MSB MSB MSB LSB LSB LSB LSB) - retValue = retValue*36000/4096; - -#endif -#ifndef HIGH_RES // In LOW RESOLUTION mode, we use only the MSB - retValue = msb; - retValue = retValue*36000/256; -#endif - - if(retValue > 19000) // value cannot be between 90deg and 270deg. - { - retValue = 36000 - retValue; - } - else if(retValue > 9000) - { - retValue = 9000 - (retValue - 9000); - } - - // Invert the angle value to use the honrizontal axis as reference instead of z axis - retValue = 9000 - retValue; - - return retValue; + return i2c_read_reg(MSB_addr); // Read MSB data from MSB register } -void xbee_transmit(char type, string data) +void xbee_transmit(char type, int data) { + std::stringstream out; + + string type_s; + switch(type) + { + case ACCEL_X: type_s = "<AX"; + break; + + case ACCEL_Y: type_s = "<AY"; + break; + + case ACCEL_Z: type_s = "<AZ"; + break; + + case PUSHBOUTON: type_s = "<PB"; + break; + } + + out << data; + string data_s = out.str(); + data_s = type_s + data_s + ">"; + char checkSum = 0; - char size = data.length() + 0x0E; //0x0E + data type + nombre de data = 0x0F + nombre de data + char size = data_s.length() + 0x0E; //0x0E + data type + nombre de data = 0x0F + nombre de data xbee.putc(0x7E); // start delimeter xbee.putc(0x00); // length xbee.putc(size); // length @@ -142,52 +163,49 @@ xbee.putc(0x00); // option checkSum = checkSum + 0x10 + 0x01 + 0x00 + 0x13+ 0xA2 + 0x00 + 0x40 + 0x8B +0x41 + 0x98 + 0x00 + 0x00 + 0x00 + 0x00; //data type - for (int i=0;i<data.length();i++) //data + for (int i=0;i<data_s.length();i++) //data { - xbee.putc(data[i]); - checkSum += data[i]; + xbee.putc(data_s[i]); + checkSum += data_s[i]; } checkSum = 0xff - checkSum; - pc.printf(" check sum = %x" , checkSum); xbee.putc(checkSum); // checksum } int main() { - unsigned short z; + unsigned short x, y, z; + + configInit(); xbee_init(); initAccel(); - myled = 1; - myled2 = 1; + myled = 0; wait(4); while(1) { - // Get z angle from the accelerometer + wait_ms(period_ms); + myled = !myled; + + // Get accelerometer values + x= getAccelValue(OUT_X_MSB, OUT_X_LSB); + y= getAccelValue(OUT_Y_MSB, OUT_Y_LSB); z= getAccelValue(OUT_Z_MSB, OUT_Z_LSB); - std::stringstream out; - out << z; - AccelData = out.str(); + + //Transmit ACCEL xyz to coordonitator + xbee_transmit(ACCEL_X, x); + xbee_transmit(ACCEL_Y, y); + xbee_transmit(ACCEL_Z, z); + + //Transmit PUSHBUTTON to coordonitator + xbee_transmit(PUSHBOUTON, pb1.read()); - //Transmit to coordonitator - xbee_transmit(ACCEL, AccelData); - - + - /* if(xbee.readable()) - { - pc.putc(xbee.getc()); - myled2 = ! myled2; - } - if(pc.readable()) - { - xbee.putc(pc.getc()); - myled = ! myled; - }*/ } }