Éric Bisson
/
Code_APP3_R
app3
Fork of Code_APP1 by
main.cpp@5:1a60144f7163, 2017-01-16 (annotated)
- Committer:
- ericbisson
- Date:
- Mon Jan 16 20:52:12 2017 +0000
- Revision:
- 5:1a60144f7163
- Parent:
- 4:ffd7cef72270
- Child:
- 6:2ec2a74c807f
reset cursor + moving average + separate SPI and UART code
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
LouBe4 | 0:52fba6f1554b | 1 | #include "mbed.h" |
ericbisson | 5:1a60144f7163 | 2 | #include "header.h" |
LouBe4 | 0:52fba6f1554b | 3 | |
LouBe4 | 3:5dcf9b78f3ad | 4 | #define PI 3.1416 |
LouBe4 | 3:5dcf9b78f3ad | 5 | |
LouBe4 | 0:52fba6f1554b | 6 | Serial pc(USBTX, USBRX); |
LouBe4 | 0:52fba6f1554b | 7 | SPI spi(p11, p12, p13); |
LouBe4 | 0:52fba6f1554b | 8 | DigitalOut cs(p14); |
LouBe4 | 0:52fba6f1554b | 9 | I2C i2c(p28, p27); |
LouBe4 | 0:52fba6f1554b | 10 | PwmOut led1(LED1); |
LouBe4 | 1:3430643e8ed4 | 11 | |
LouBe4 | 3:5dcf9b78f3ad | 12 | void calculer_angle(char bufferAngle[], int accZ) |
LouBe4 | 1:3430643e8ed4 | 13 | { |
ericbisson | 5:1a60144f7163 | 14 | float angle = abs(cos(static_cast<float>(moyenne_mobile(accZ)*90/64)*PI/180)*90); |
LouBe4 | 3:5dcf9b78f3ad | 15 | int angleInt = (int)(angle*100); |
LouBe4 | 4:ffd7cef72270 | 16 | snprintf(bufferAngle, 5, "%d", angleInt); |
LouBe4 | 3:5dcf9b78f3ad | 17 | |
LouBe4 | 3:5dcf9b78f3ad | 18 | if(angle < 10) |
LouBe4 | 3:5dcf9b78f3ad | 19 | { |
LouBe4 | 3:5dcf9b78f3ad | 20 | bufferAngle[3] = bufferAngle[2]; |
LouBe4 | 3:5dcf9b78f3ad | 21 | bufferAngle[2] = bufferAngle[1]; |
LouBe4 | 3:5dcf9b78f3ad | 22 | bufferAngle[1] = bufferAngle[0]; |
LouBe4 | 3:5dcf9b78f3ad | 23 | bufferAngle[0] = '0'; |
ericbisson | 5:1a60144f7163 | 24 | } |
LouBe4 | 1:3430643e8ed4 | 25 | } |
LouBe4 | 0:52fba6f1554b | 26 | |
LouBe4 | 0:52fba6f1554b | 27 | int main() { |
ericbisson | 5:1a60144f7163 | 28 | int addrChip = 0x3A; |
ericbisson | 5:1a60144f7163 | 29 | char buffer[3] = {0,0,0}; |
ericbisson | 5:1a60144f7163 | 30 | |
ericbisson | 5:1a60144f7163 | 31 | SetClockAndMode(250000, 0); |
ericbisson | 5:1a60144f7163 | 32 | change_dots(0x02); |
ericbisson | 5:1a60144f7163 | 33 | |
ericbisson | 5:1a60144f7163 | 34 | //Activer l'accéléromètre pour lecture 8 bits |
ericbisson | 5:1a60144f7163 | 35 | char activation[2] = {0x2A, 0x03}; |
ericbisson | 5:1a60144f7163 | 36 | char fullScale[2] = {0x0E, 0x00}; |
ericbisson | 5:1a60144f7163 | 37 | char resultat[5] = {0}; |
ericbisson | 5:1a60144f7163 | 38 | i2c.write(addrChip, activation, 2, true); |
ericbisson | 5:1a60144f7163 | 39 | i2c.write(addrChip, fullScale, 2, true); |
ericbisson | 5:1a60144f7163 | 40 | |
LouBe4 | 3:5dcf9b78f3ad | 41 | while(1) |
ericbisson | 5:1a60144f7163 | 42 | { |
LouBe4 | 3:5dcf9b78f3ad | 43 | //Aller lire les valeurs d'accélération |
LouBe4 | 3:5dcf9b78f3ad | 44 | buffer[0] = 0x01; |
LouBe4 | 3:5dcf9b78f3ad | 45 | i2c.write(addrChip, buffer, 1, true); |
LouBe4 | 3:5dcf9b78f3ad | 46 | i2c.read(addrChip, buffer, 3); |
LouBe4 | 3:5dcf9b78f3ad | 47 | calculer_angle(resultat, buffer[2]); |
LouBe4 | 3:5dcf9b78f3ad | 48 | |
ericbisson | 5:1a60144f7163 | 49 | chip_select(false); |
ericbisson | 5:1a60144f7163 | 50 | write_to_7segment(resultat[0],resultat[1],resultat[2],resultat[3]); |
ericbisson | 5:1a60144f7163 | 51 | chip_select(true); |
ericbisson | 5:1a60144f7163 | 52 | wait(0.1); |
ericbisson | 5:1a60144f7163 | 53 | } |
ericbisson | 5:1a60144f7163 | 54 | } |
ericbisson | 5:1a60144f7163 | 55 | |
ericbisson | 5:1a60144f7163 | 56 | void SetClockAndMode(int Speed, char Mode) |
ericbisson | 5:1a60144f7163 | 57 | { |
ericbisson | 5:1a60144f7163 | 58 | if (IS_USING_SPI) |
ericbisson | 5:1a60144f7163 | 59 | { |
ericbisson | 5:1a60144f7163 | 60 | spi.format(8,Mode); |
ericbisson | 5:1a60144f7163 | 61 | spi.frequency(Speed); |
ericbisson | 5:1a60144f7163 | 62 | } |
ericbisson | 5:1a60144f7163 | 63 | else |
ericbisson | 5:1a60144f7163 | 64 | { |
ericbisson | 5:1a60144f7163 | 65 | // TODO |
ericbisson | 5:1a60144f7163 | 66 | } |
ericbisson | 5:1a60144f7163 | 67 | } |
ericbisson | 5:1a60144f7163 | 68 | |
ericbisson | 5:1a60144f7163 | 69 | // function to change displayed dots |
ericbisson | 5:1a60144f7163 | 70 | void change_dots(char dot) |
ericbisson | 5:1a60144f7163 | 71 | { |
ericbisson | 5:1a60144f7163 | 72 | if (IS_USING_SPI) |
ericbisson | 5:1a60144f7163 | 73 | { |
LouBe4 | 3:5dcf9b78f3ad | 74 | spi.write(0x77); |
ericbisson | 5:1a60144f7163 | 75 | spi.write(dot); |
ericbisson | 5:1a60144f7163 | 76 | } |
ericbisson | 5:1a60144f7163 | 77 | else |
ericbisson | 5:1a60144f7163 | 78 | { |
ericbisson | 5:1a60144f7163 | 79 | // TODO |
ericbisson | 5:1a60144f7163 | 80 | } |
ericbisson | 5:1a60144f7163 | 81 | } |
ericbisson | 5:1a60144f7163 | 82 | |
ericbisson | 5:1a60144f7163 | 83 | void ResetCursor() |
ericbisson | 5:1a60144f7163 | 84 | { |
ericbisson | 5:1a60144f7163 | 85 | if (IS_USING_SPI) |
ericbisson | 5:1a60144f7163 | 86 | { |
ericbisson | 5:1a60144f7163 | 87 | spi.write(0x79); |
ericbisson | 5:1a60144f7163 | 88 | spi.write(0); |
ericbisson | 5:1a60144f7163 | 89 | } |
ericbisson | 5:1a60144f7163 | 90 | else |
ericbisson | 5:1a60144f7163 | 91 | { |
ericbisson | 5:1a60144f7163 | 92 | // TODO |
LouBe4 | 3:5dcf9b78f3ad | 93 | } |
LouBe4 | 0:52fba6f1554b | 94 | } |
ericbisson | 5:1a60144f7163 | 95 | |
ericbisson | 5:1a60144f7163 | 96 | // function used to write numbers to all four digits |
ericbisson | 5:1a60144f7163 | 97 | void write_to_7segment(char d1, char d2, char d3, char d4) |
ericbisson | 5:1a60144f7163 | 98 | { |
ericbisson | 5:1a60144f7163 | 99 | ResetCursor(); |
ericbisson | 5:1a60144f7163 | 100 | if (IS_USING_SPI) |
ericbisson | 5:1a60144f7163 | 101 | { |
ericbisson | 5:1a60144f7163 | 102 | spi.write(d1); |
ericbisson | 5:1a60144f7163 | 103 | spi.write(d2); |
ericbisson | 5:1a60144f7163 | 104 | spi.write(d3); |
ericbisson | 5:1a60144f7163 | 105 | spi.write(d4); |
ericbisson | 5:1a60144f7163 | 106 | } |
ericbisson | 5:1a60144f7163 | 107 | else |
ericbisson | 5:1a60144f7163 | 108 | { |
ericbisson | 5:1a60144f7163 | 109 | // TODO |
ericbisson | 5:1a60144f7163 | 110 | } |
ericbisson | 5:1a60144f7163 | 111 | } |
ericbisson | 5:1a60144f7163 | 112 | |
ericbisson | 5:1a60144f7163 | 113 | void chip_select(bool bSet) |
ericbisson | 5:1a60144f7163 | 114 | { |
ericbisson | 5:1a60144f7163 | 115 | if (IS_USING_SPI) |
ericbisson | 5:1a60144f7163 | 116 | { |
ericbisson | 5:1a60144f7163 | 117 | if (!bSet) |
ericbisson | 5:1a60144f7163 | 118 | { |
ericbisson | 5:1a60144f7163 | 119 | wait_us(25); |
ericbisson | 5:1a60144f7163 | 120 | } |
ericbisson | 5:1a60144f7163 | 121 | cs = bSet; |
ericbisson | 5:1a60144f7163 | 122 | if (bSet) |
ericbisson | 5:1a60144f7163 | 123 | { |
ericbisson | 5:1a60144f7163 | 124 | wait_us(25); |
ericbisson | 5:1a60144f7163 | 125 | } |
ericbisson | 5:1a60144f7163 | 126 | } |
ericbisson | 5:1a60144f7163 | 127 | } |
ericbisson | 5:1a60144f7163 | 128 | |
ericbisson | 5:1a60144f7163 | 129 | // function used to calculate and return the new value of a moving average |
ericbisson | 5:1a60144f7163 | 130 | int moyenne_mobile(int newData) |
ericbisson | 5:1a60144f7163 | 131 | { |
ericbisson | 5:1a60144f7163 | 132 | int sum = 0; |
ericbisson | 5:1a60144f7163 | 133 | MovingAverage.buffer[MovingAverage.cursor] = newData; |
ericbisson | 5:1a60144f7163 | 134 | MovingAverage.cursor++; |
ericbisson | 5:1a60144f7163 | 135 | if (MovingAverage.cursor >= MOVING_AVG_SIZE) |
ericbisson | 5:1a60144f7163 | 136 | { |
ericbisson | 5:1a60144f7163 | 137 | MovingAverage.cursor = 0; |
ericbisson | 5:1a60144f7163 | 138 | MovingAverage.bFilled = true; |
ericbisson | 5:1a60144f7163 | 139 | } |
ericbisson | 5:1a60144f7163 | 140 | |
ericbisson | 5:1a60144f7163 | 141 | if (MovingAverage.bFilled) |
ericbisson | 5:1a60144f7163 | 142 | { |
ericbisson | 5:1a60144f7163 | 143 | for (int i = 0; i < MOVING_AVG_SIZE; i++) |
ericbisson | 5:1a60144f7163 | 144 | { |
ericbisson | 5:1a60144f7163 | 145 | sum += MovingAverage.buffer[i]; |
ericbisson | 5:1a60144f7163 | 146 | } |
ericbisson | 5:1a60144f7163 | 147 | sum = sum / MOVING_AVG_SIZE; |
ericbisson | 5:1a60144f7163 | 148 | } |
ericbisson | 5:1a60144f7163 | 149 | else |
ericbisson | 5:1a60144f7163 | 150 | { |
ericbisson | 5:1a60144f7163 | 151 | for (int i = 0; i < MovingAverage.cursor; i++) |
ericbisson | 5:1a60144f7163 | 152 | { |
ericbisson | 5:1a60144f7163 | 153 | sum += MovingAverage.buffer[i]; |
ericbisson | 5:1a60144f7163 | 154 | } |
ericbisson | 5:1a60144f7163 | 155 | sum = sum / MovingAverage.cursor; |
ericbisson | 5:1a60144f7163 | 156 | } |
ericbisson | 5:1a60144f7163 | 157 | |
ericbisson | 5:1a60144f7163 | 158 | return sum; |
ericbisson | 5:1a60144f7163 | 159 | } |