![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Receiver code
Sensors.cpp@0:f8873e0badb2, 2016-02-18 (annotated)
- Committer:
- vinbel93
- Date:
- Thu Feb 18 13:22:31 2016 +0000
- Revision:
- 0:f8873e0badb2
Receiver
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
vinbel93 | 0:f8873e0badb2 | 1 | #include "mbed.h" |
vinbel93 | 0:f8873e0badb2 | 2 | #include "Sensors.h" |
vinbel93 | 0:f8873e0badb2 | 3 | |
vinbel93 | 0:f8873e0badb2 | 4 | // Capteurs |
vinbel93 | 0:f8873e0badb2 | 5 | DigitalIn button(p9); |
vinbel93 | 0:f8873e0badb2 | 6 | I2C accelero(p28, p27); |
vinbel93 | 0:f8873e0badb2 | 7 | |
vinbel93 | 0:f8873e0badb2 | 8 | // Fontion de lecture du bouton |
vinbel93 | 0:f8873e0badb2 | 9 | void readButton(char* buffer) |
vinbel93 | 0:f8873e0badb2 | 10 | { |
vinbel93 | 0:f8873e0badb2 | 11 | buffer[1] = button; |
vinbel93 | 0:f8873e0badb2 | 12 | } |
vinbel93 | 0:f8873e0badb2 | 13 | |
vinbel93 | 0:f8873e0badb2 | 14 | // Fonction de lecture de l'accéléromètre |
vinbel93 | 0:f8873e0badb2 | 15 | void readAccelero(char* buffer) |
vinbel93 | 0:f8873e0badb2 | 16 | { |
vinbel93 | 0:f8873e0badb2 | 17 | // Activation de l'accéléromètre |
vinbel93 | 0:f8873e0badb2 | 18 | char data[2] = {CTRL_REG, 0x01}; |
vinbel93 | 0:f8873e0badb2 | 19 | accelero.write(ACC_ADDRESS, (char*) data, 2); |
vinbel93 | 0:f8873e0badb2 | 20 | |
vinbel93 | 0:f8873e0badb2 | 21 | // Lecture du registre de l'accélération en X |
vinbel93 | 0:f8873e0badb2 | 22 | char result[2]; |
vinbel93 | 0:f8873e0badb2 | 23 | char reg[1] = {ACC_X_REG}; |
vinbel93 | 0:f8873e0badb2 | 24 | accelero.write(ACC_ADDRESS, (char*) reg, 1, true); |
vinbel93 | 0:f8873e0badb2 | 25 | accelero.read(ACC_ADDRESS, (char*) result, 2); |
vinbel93 | 0:f8873e0badb2 | 26 | short accX = convertToAcceleration(result[0], result[1]); |
vinbel93 | 0:f8873e0badb2 | 27 | |
vinbel93 | 0:f8873e0badb2 | 28 | // Conversion des données sur 16 bits |
vinbel93 | 0:f8873e0badb2 | 29 | buffer[1] = accX & 0x00FF; |
vinbel93 | 0:f8873e0badb2 | 30 | buffer[2] = (accX & 0xFF00) >> 8; |
vinbel93 | 0:f8873e0badb2 | 31 | } |
vinbel93 | 0:f8873e0badb2 | 32 | |
vinbel93 | 0:f8873e0badb2 | 33 | // Fonction qui convertit la donnée retournée par l'accéléromètre en une valeur 16 bits |
vinbel93 | 0:f8873e0badb2 | 34 | short convertToAcceleration(char hi, char lo) |
vinbel93 | 0:f8873e0badb2 | 35 | { |
vinbel93 | 0:f8873e0badb2 | 36 | short value = (lo >> 2) | (hi << 6); |
vinbel93 | 0:f8873e0badb2 | 37 | if (value > UINT14_MAX/2) |
vinbel93 | 0:f8873e0badb2 | 38 | { |
vinbel93 | 0:f8873e0badb2 | 39 | value -= UINT14_MAX; |
vinbel93 | 0:f8873e0badb2 | 40 | } |
vinbel93 | 0:f8873e0badb2 | 41 | |
vinbel93 | 0:f8873e0badb2 | 42 | return value; |
vinbel93 | 0:f8873e0badb2 | 43 | } |