Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp@9:bfb877e5fecf, 2015-04-24 (annotated)
- Committer:
- tyleralt
- Date:
- Fri Apr 24 17:37:01 2015 +0000
- Revision:
- 9:bfb877e5fecf
- Parent:
- 8:3fc2849c098d
board driver code;
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| tyleralt | 0:d9730cf42ecc | 1 | #include "mbed.h" |
| tyleralt | 1:ea12b9153c51 | 2 | #include <vector> |
| tyleralt | 0:d9730cf42ecc | 3 | #define BUFFER_SIZE 16 |
| tyleralt | 6:1d7d769b398d | 4 | #define NUMBER_OF_SLICES 120 |
| tyleralt | 6:1d7d769b398d | 5 | #include "MRF24J40.h" |
| tyleralt | 0:d9730cf42ecc | 6 | |
| tyleralt | 6:1d7d769b398d | 7 | //push data pins |
| tyleralt | 7:7a73065d2d92 | 8 | DigitalOut pushRegister(p24); |
| tyleralt | 7:7a73065d2d92 | 9 | DigitalOut pushBit(p23); |
| tyleralt | 0:d9730cf42ecc | 10 | |
| tyleralt | 6:1d7d769b398d | 11 | //write to arm pins |
| tyleralt | 0:d9730cf42ecc | 12 | DigitalOut dataArmOne(p15); |
| tyleralt | 0:d9730cf42ecc | 13 | DigitalOut dataArmTwo(p16); |
| tyleralt | 0:d9730cf42ecc | 14 | DigitalOut dataArmThree(p17); |
| tyleralt | 0:d9730cf42ecc | 15 | DigitalOut dataArmFour(p18); |
| tyleralt | 0:d9730cf42ecc | 16 | DigitalOut dataArmFive(p19); |
| tyleralt | 0:d9730cf42ecc | 17 | DigitalOut dataArmSix(p20); |
| tyleralt | 0:d9730cf42ecc | 18 | DigitalOut dataArmSeven(p21); |
| tyleralt | 0:d9730cf42ecc | 19 | DigitalOut dataArmEight(p22); |
| tyleralt | 0:d9730cf42ecc | 20 | |
| tyleralt | 6:1d7d769b398d | 21 | //hass sensor interupt |
| tyleralt | 0:d9730cf42ecc | 22 | InterruptIn hallSensor(p25); |
| tyleralt | 0:d9730cf42ecc | 23 | |
| tyleralt | 1:ea12b9153c51 | 24 | //Set Up Timer and ticker |
| tyleralt | 0:d9730cf42ecc | 25 | Timer rotationTime; |
| tyleralt | 1:ea12b9153c51 | 26 | Ticker updateLeds; |
| tyleralt | 0:d9730cf42ecc | 27 | |
| tyleralt | 0:d9730cf42ecc | 28 | //Declare global vars |
| tyleralt | 2:554edc4b0cf2 | 29 | double slice_time; |
| tyleralt | 6:1d7d769b398d | 30 | double rotate_time; |
| tyleralt | 0:d9730cf42ecc | 31 | int current_slice; |
| tyleralt | 4:f81b40e04a58 | 32 | char slice_data [NUMBER_OF_SLICES][16]; //[slice][specific led distance] (0 is closest) & with approppriate bit for each arm |
| tyleralt | 8:3fc2849c098d | 33 | bool firstTime; |
| tyleralt | 8:3fc2849c098d | 34 | |
| tyleralt | 0:d9730cf42ecc | 35 | |
| tyleralt | 6:1d7d769b398d | 36 | //serial port for pc and bluetooth |
| tyleralt | 0:d9730cf42ecc | 37 | Serial pc(USBTX, USBRX); // tx, rx |
| tyleralt | 3:1e9b4a4bf177 | 38 | Serial bt(p9, p10);// tx, rx |
| tyleralt | 0:d9730cf42ecc | 39 | |
| tyleralt | 6:1d7d769b398d | 40 | //define variables for ziggbee |
| tyleralt | 6:1d7d769b398d | 41 | MRF24J40 mrf(p11, p12, p13, p14, p26); |
| tyleralt | 8:3fc2849c098d | 42 | char rxBuffer[128]; |
| tyleralt | 6:1d7d769b398d | 43 | char txBuffer[128]; |
| tyleralt | 6:1d7d769b398d | 44 | int rxLen; |
| tyleralt | 8:3fc2849c098d | 45 | char buffer [4]; |
| tyleralt | 6:1d7d769b398d | 46 | |
| tyleralt | 6:1d7d769b398d | 47 | |
| tyleralt | 6:1d7d769b398d | 48 | int rf_receive(char *data, uint8_t maxLength) |
| tyleralt | 6:1d7d769b398d | 49 | { |
| tyleralt | 6:1d7d769b398d | 50 | uint8_t len = mrf.Receive((uint8_t *)data, maxLength); |
| tyleralt | 6:1d7d769b398d | 51 | uint8_t header[8]= {1, 8, 0, 0xA1, 0xB2, 0xC3, 0xD4, 0x00}; |
| tyleralt | 8:3fc2849c098d | 52 | |
| tyleralt | 6:1d7d769b398d | 53 | if(len > 10) { |
| tyleralt | 6:1d7d769b398d | 54 | //Remove the header and footer of the message |
| tyleralt | 6:1d7d769b398d | 55 | for(uint8_t i = 0; i < len-2; i++) { |
| tyleralt | 6:1d7d769b398d | 56 | if(i<8) { |
| tyleralt | 6:1d7d769b398d | 57 | //Make sure our header is valid first |
| tyleralt | 6:1d7d769b398d | 58 | if(data[i] != header[i]) |
| tyleralt | 6:1d7d769b398d | 59 | return 0; |
| tyleralt | 6:1d7d769b398d | 60 | } else { |
| tyleralt | 6:1d7d769b398d | 61 | data[i-8] = data[i]; |
| tyleralt | 6:1d7d769b398d | 62 | } |
| tyleralt | 6:1d7d769b398d | 63 | } |
| tyleralt | 8:3fc2849c098d | 64 | |
| tyleralt | 6:1d7d769b398d | 65 | //pc.printf("Received: %s length:%d\r\n", data, ((int)len)-10); |
| tyleralt | 6:1d7d769b398d | 66 | } |
| tyleralt | 6:1d7d769b398d | 67 | return ((int)len)-10; |
| tyleralt | 6:1d7d769b398d | 68 | } |
| tyleralt | 8:3fc2849c098d | 69 | void recievePoint (int slice, int distance, char addChar, char operation){ |
| tyleralt | 8:3fc2849c098d | 70 | //pc.printf("recieved %i %i %X %c \r\n" , slice, distance, addChar, operation); |
| tyleralt | 8:3fc2849c098d | 71 | if (operation == 'x'){ |
| tyleralt | 8:3fc2849c098d | 72 | //pc.printf("odl slice data with x %X is \r\n", slice_data[slice][distance]); |
| tyleralt | 8:3fc2849c098d | 73 | slice_data [slice] [distance] ^= addChar; |
| tyleralt | 8:3fc2849c098d | 74 | //pc.printf("new slice data with X %X is \r\n", slice_data[slice][distance]); |
| tyleralt | 8:3fc2849c098d | 75 | return; |
| tyleralt | 8:3fc2849c098d | 76 | } |
| tyleralt | 8:3fc2849c098d | 77 | if (operation == 'o'){ |
| tyleralt | 8:3fc2849c098d | 78 | // pc.printf("odl slice data with o %X is \r\n", slice_data[slice][distance]); |
| tyleralt | 8:3fc2849c098d | 79 | slice_data [slice] [distance] |= addChar; |
| tyleralt | 8:3fc2849c098d | 80 | //pc.printf("new slice data with 0 %X is \r\n", slice_data[slice][distance]); |
| tyleralt | 8:3fc2849c098d | 81 | return; |
| tyleralt | 8:3fc2849c098d | 82 | } |
| tyleralt | 8:3fc2849c098d | 83 | if (operation == 'a'){ |
| tyleralt | 8:3fc2849c098d | 84 | slice_data [slice] [distance] = slice_data[slice][distance] & addChar; |
| tyleralt | 8:3fc2849c098d | 85 | return; |
| tyleralt | 8:3fc2849c098d | 86 | } |
| tyleralt | 6:1d7d769b398d | 87 | } |
| tyleralt | 0:d9730cf42ecc | 88 | |
| tyleralt | 4:f81b40e04a58 | 89 | void pushData (char bits [16]){ |
| tyleralt | 8:3fc2849c098d | 90 | |
| tyleralt | 8:3fc2849c098d | 91 | // pc.printf("pushing data \r\n"); |
| tyleralt | 4:f81b40e04a58 | 92 | for (int i = 8; i < 16; i ++){ |
| tyleralt | 4:f81b40e04a58 | 93 | dataArmOne = bits [i] & 0x01; |
| tyleralt | 4:f81b40e04a58 | 94 | dataArmTwo = bits [i] & 0x02; |
| tyleralt | 4:f81b40e04a58 | 95 | dataArmThree = bits [i]& 0x04; |
| tyleralt | 4:f81b40e04a58 | 96 | dataArmFour = bits [i]& 0x08; |
| tyleralt | 4:f81b40e04a58 | 97 | dataArmFive = bits [i] & 0x10; |
| tyleralt | 4:f81b40e04a58 | 98 | dataArmSix = bits [i] & 0x20; |
| tyleralt | 4:f81b40e04a58 | 99 | dataArmSeven = bits [i] & 0x40; |
| tyleralt | 4:f81b40e04a58 | 100 | dataArmEight = bits [i] & 0x80; |
| tyleralt | 1:ea12b9153c51 | 101 | |
| tyleralt | 1:ea12b9153c51 | 102 | pushBit = 1; |
| tyleralt | 1:ea12b9153c51 | 103 | pushBit = 0; |
| tyleralt | 1:ea12b9153c51 | 104 | } |
| tyleralt | 4:f81b40e04a58 | 105 | for (int i = 7; i >= 0; i --){ |
| tyleralt | 4:f81b40e04a58 | 106 | dataArmOne = bits [i] & 0x01; |
| tyleralt | 4:f81b40e04a58 | 107 | dataArmTwo = bits [i] & 0x02; |
| tyleralt | 4:f81b40e04a58 | 108 | dataArmThree = bits [i]& 0x04; |
| tyleralt | 4:f81b40e04a58 | 109 | dataArmFour = bits [i]& 0x08; |
| tyleralt | 4:f81b40e04a58 | 110 | dataArmFive = bits [i] & 0x10; |
| tyleralt | 4:f81b40e04a58 | 111 | dataArmSix = bits [i] & 0x20; |
| tyleralt | 4:f81b40e04a58 | 112 | dataArmSeven = bits [i] & 0x40; |
| tyleralt | 4:f81b40e04a58 | 113 | dataArmEight = bits [i] & 0x80; |
| tyleralt | 4:f81b40e04a58 | 114 | |
| tyleralt | 4:f81b40e04a58 | 115 | pushBit = 1; |
| tyleralt | 4:f81b40e04a58 | 116 | pushBit = 0; |
| tyleralt | 4:f81b40e04a58 | 117 | } |
| tyleralt | 4:f81b40e04a58 | 118 | |
| tyleralt | 4:f81b40e04a58 | 119 | pushRegister = 1; |
| tyleralt | 4:f81b40e04a58 | 120 | pushRegister = 0; |
| tyleralt | 1:ea12b9153c51 | 121 | } |
| tyleralt | 1:ea12b9153c51 | 122 | |
| tyleralt | 1:ea12b9153c51 | 123 | |
| tyleralt | 1:ea12b9153c51 | 124 | //periodic display interrupt |
| tyleralt | 1:ea12b9153c51 | 125 | void nextLedPush(){ |
| tyleralt | 8:3fc2849c098d | 126 | // pc.printf("next led push"); |
| tyleralt | 1:ea12b9153c51 | 127 | if (current_slice < NUMBER_OF_SLICES){ |
| tyleralt | 4:f81b40e04a58 | 128 | pushData(slice_data[current_slice]); |
| tyleralt | 1:ea12b9153c51 | 129 | current_slice ++; |
| tyleralt | 2:554edc4b0cf2 | 130 | } else { |
| tyleralt | 2:554edc4b0cf2 | 131 | updateLeds.detach(); |
| tyleralt | 1:ea12b9153c51 | 132 | } |
| tyleralt | 1:ea12b9153c51 | 133 | } |
| tyleralt | 1:ea12b9153c51 | 134 | |
| tyleralt | 0:d9730cf42ecc | 135 | //Hall sensor interupt |
| tyleralt | 0:d9730cf42ecc | 136 | void rotate_sense(){ |
| tyleralt | 8:3fc2849c098d | 137 | // pc.printf("interupt"); |
| tyleralt | 0:d9730cf42ecc | 138 | if (firstTime){ |
| tyleralt | 0:d9730cf42ecc | 139 | rotationTime.reset(); |
| tyleralt | 0:d9730cf42ecc | 140 | rotationTime.start(); |
| tyleralt | 6:1d7d769b398d | 141 | firstTime = false; |
| tyleralt | 8:3fc2849c098d | 142 | // pc.printf(" first time"); |
| tyleralt | 2:554edc4b0cf2 | 143 | return; |
| tyleralt | 8:3fc2849c098d | 144 | } |
| tyleralt | 8:3fc2849c098d | 145 | if(current_slice < NUMBER_OF_SLICES / 4){ |
| tyleralt | 2:554edc4b0cf2 | 146 | return; |
| tyleralt | 8:3fc2849c098d | 147 | } |
| tyleralt | 8:3fc2849c098d | 148 | // pc.printf(" seconod time \n"); |
| tyleralt | 0:d9730cf42ecc | 149 | rotate_time = rotationTime.read_us(); |
| tyleralt | 0:d9730cf42ecc | 150 | rotationTime.reset(); |
| tyleralt | 0:d9730cf42ecc | 151 | rotationTime.start(); |
| tyleralt | 0:d9730cf42ecc | 152 | |
| tyleralt | 2:554edc4b0cf2 | 153 | slice_time = (double) rotate_time/NUMBER_OF_SLICES; |
| tyleralt | 0:d9730cf42ecc | 154 | current_slice = 0; |
| tyleralt | 2:554edc4b0cf2 | 155 | updateLeds.attach_us(&nextLedPush, slice_time); |
| tyleralt | 0:d9730cf42ecc | 156 | } |
| tyleralt | 0:d9730cf42ecc | 157 | |
| tyleralt | 6:1d7d769b398d | 158 | void didRecievePoint(){ |
| tyleralt | 6:1d7d769b398d | 159 | } |
| tyleralt | 6:1d7d769b398d | 160 | |
| tyleralt | 0:d9730cf42ecc | 161 | |
| tyleralt | 0:d9730cf42ecc | 162 | int main() { |
| tyleralt | 8:3fc2849c098d | 163 | pc.printf("started"); |
| tyleralt | 6:1d7d769b398d | 164 | firstTime = true; |
| tyleralt | 8:3fc2849c098d | 165 | current_slice = 100; |
| tyleralt | 6:1d7d769b398d | 166 | |
| tyleralt | 8:3fc2849c098d | 167 | uint8_t channel = 2; |
| tyleralt | 8:3fc2849c098d | 168 | mrf.SetChannel(channel); |
| tyleralt | 0:d9730cf42ecc | 169 | |
| tyleralt | 3:1e9b4a4bf177 | 170 | hallSensor.fall(&rotate_sense); |
| tyleralt | 6:1d7d769b398d | 171 | |
| tyleralt | 8:3fc2849c098d | 172 | /* for (int i = 0; i < 60; i ++){ |
| tyleralt | 8:3fc2849c098d | 173 | slice_data [i][0] = 0x11; // bit 0 is high on arm 0 |
| tyleralt | 8:3fc2849c098d | 174 | slice_data [i][1] = 0x11; |
| tyleralt | 8:3fc2849c098d | 175 | slice_data [i][2] = 0x11; |
| tyleralt | 8:3fc2849c098d | 176 | slice_data [i][3] = 0x11; |
| tyleralt | 8:3fc2849c098d | 177 | slice_data [i][4] = 0x11; |
| tyleralt | 8:3fc2849c098d | 178 | slice_data [i][5] = 0x11; |
| tyleralt | 8:3fc2849c098d | 179 | slice_data [i][6] = 0x11; |
| tyleralt | 8:3fc2849c098d | 180 | slice_data [i][7] = 0x11; |
| tyleralt | 8:3fc2849c098d | 181 | slice_data [i][8] = 0x11; |
| tyleralt | 8:3fc2849c098d | 182 | slice_data [i][9] = 0x11; |
| tyleralt | 8:3fc2849c098d | 183 | slice_data [i][10] = 0x11; |
| tyleralt | 8:3fc2849c098d | 184 | slice_data [i][11] = 0x11; |
| tyleralt | 8:3fc2849c098d | 185 | slice_data [i][12] = 0x11; |
| tyleralt | 8:3fc2849c098d | 186 | slice_data [i][13] = 0x11; |
| tyleralt | 8:3fc2849c098d | 187 | slice_data [i][14] = 0x11; |
| tyleralt | 8:3fc2849c098d | 188 | slice_data [i][15] = 0x11; |
| tyleralt | 8:3fc2849c098d | 189 | }*/ |
| tyleralt | 8:3fc2849c098d | 190 | /* |
| tyleralt | 6:1d7d769b398d | 191 | for (int i = 0; i < 60; i ++){ |
| tyleralt | 8:3fc2849c098d | 192 | slice_data [i][0] = 0x00; // bit 0 is high on arm 0 |
| tyleralt | 8:3fc2849c098d | 193 | slice_data [i][1] = 0x00; |
| tyleralt | 8:3fc2849c098d | 194 | slice_data [i][2] = 0x00; |
| tyleralt | 8:3fc2849c098d | 195 | slice_data [i][3] = 0x00; |
| tyleralt | 8:3fc2849c098d | 196 | slice_data [i][4] = 0x00; |
| tyleralt | 8:3fc2849c098d | 197 | slice_data [i][5] = 0x00; |
| tyleralt | 8:3fc2849c098d | 198 | slice_data [i][6] = 0x08; |
| tyleralt | 8:3fc2849c098d | 199 | slice_data [i][7] = 0x00; |
| tyleralt | 8:3fc2849c098d | 200 | slice_data [i][8] = 0x00; |
| tyleralt | 8:3fc2849c098d | 201 | slice_data [i][9] = 0x00; |
| tyleralt | 8:3fc2849c098d | 202 | slice_data [i][10] = 0x00; |
| tyleralt | 8:3fc2849c098d | 203 | slice_data [i][11] = 0x00; |
| tyleralt | 8:3fc2849c098d | 204 | slice_data [i][12] = 0x00; |
| tyleralt | 8:3fc2849c098d | 205 | slice_data [i][13] = 0x00; |
| tyleralt | 8:3fc2849c098d | 206 | slice_data [i][14] = 0x00; |
| tyleralt | 8:3fc2849c098d | 207 | slice_data [i][15] = 0x00; |
| tyleralt | 0:d9730cf42ecc | 208 | } |
| tyleralt | 8:3fc2849c098d | 209 | |
| tyleralt | 6:1d7d769b398d | 210 | for (int i = 60; i < 120; i++){ |
| tyleralt | 4:f81b40e04a58 | 211 | slice_data [i][0] = 0x00; // bit 0 is high on arm 0 |
| tyleralt | 8:3fc2849c098d | 212 | slice_data [i][1] = 0x00; |
| tyleralt | 4:f81b40e04a58 | 213 | slice_data [i][2] = 0x00; |
| tyleralt | 8:3fc2849c098d | 214 | slice_data [i][3] = 0x00; |
| tyleralt | 8:3fc2849c098d | 215 | slice_data [i][4] = 0x08; |
| tyleralt | 8:3fc2849c098d | 216 | slice_data [i][5] = 0x00; |
| tyleralt | 4:f81b40e04a58 | 217 | slice_data [i][6] = 0x00; |
| tyleralt | 8:3fc2849c098d | 218 | slice_data [i][7] = 0x00; |
| tyleralt | 5:6d8f9165021e | 219 | slice_data [i][8] = 0x00; |
| tyleralt | 8:3fc2849c098d | 220 | slice_data [i][9] = 0x00; |
| tyleralt | 4:f81b40e04a58 | 221 | slice_data [i][10] = 0x00; |
| tyleralt | 8:3fc2849c098d | 222 | slice_data [i][11] = 0x00; |
| tyleralt | 4:f81b40e04a58 | 223 | slice_data [i][12] = 0x00; |
| tyleralt | 8:3fc2849c098d | 224 | slice_data [i][13] = 0x00; |
| tyleralt | 4:f81b40e04a58 | 225 | slice_data [i][14] = 0x00; |
| tyleralt | 8:3fc2849c098d | 226 | slice_data [i][15] = 0x00; |
| tyleralt | 0:d9730cf42ecc | 227 | } |
| tyleralt | 8:3fc2849c098d | 228 | */ |
| tyleralt | 8:3fc2849c098d | 229 | slice_data [60][15] = 0xFF; |
| tyleralt | 8:3fc2849c098d | 230 | for (int i = 0; i < 120; i ++){ |
| tyleralt | 8:3fc2849c098d | 231 | for (int j = 0; j < 16; j++){ |
| tyleralt | 8:3fc2849c098d | 232 | slice_data [i][j] = 0x00; |
| tyleralt | 9:bfb877e5fecf | 233 | // if (j == 14){ |
| tyleralt | 9:bfb877e5fecf | 234 | // slice_data [i][j] = 0x01; |
| tyleralt | 9:bfb877e5fecf | 235 | // } |
| tyleralt | 9:bfb877e5fecf | 236 | |
| tyleralt | 8:3fc2849c098d | 237 | } |
| tyleralt | 8:3fc2849c098d | 238 | } |
| tyleralt | 8:3fc2849c098d | 239 | slice_data[60][14] = 0xFF; |
| tyleralt | 6:1d7d769b398d | 240 | |
| tyleralt | 6:1d7d769b398d | 241 | while(true) { |
| tyleralt | 8:3fc2849c098d | 242 | |
| tyleralt | 6:1d7d769b398d | 243 | //Try to receive some data |
| tyleralt | 8:3fc2849c098d | 244 | rxLen = rf_receive(rxBuffer, 128); |
| tyleralt | 8:3fc2849c098d | 245 | if(rxLen > 0) { |
| tyleralt | 8:3fc2849c098d | 246 | int slice = (int) rxBuffer[0]; |
| tyleralt | 8:3fc2849c098d | 247 | int distance = (int) rxBuffer[1]; |
| tyleralt | 8:3fc2849c098d | 248 | char addChar = (char) rxBuffer[2]; |
| tyleralt | 8:3fc2849c098d | 249 | char operation = (char) rxBuffer[3]; |
| tyleralt | 6:1d7d769b398d | 250 | |
| tyleralt | 8:3fc2849c098d | 251 | //pc.printf("we got his from the buffer slice %i distance %i char %X operation %c \r\n", slice, distance, addChar, operation); |
| tyleralt | 8:3fc2849c098d | 252 | recievePoint(slice, distance, addChar, operation); |
| tyleralt | 8:3fc2849c098d | 253 | //TODO this means we got a |
| tyleralt | 8:3fc2849c098d | 254 | //char |
| tyleralt | 8:3fc2849c098d | 255 | } |
| tyleralt | 0:d9730cf42ecc | 256 | } |
| tyleralt | 0:d9730cf42ecc | 257 | } |