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@7:7a73065d2d92, 2015-04-22 (annotated)
- Committer:
- tyleralt
- Date:
- Wed Apr 22 06:36:01 2015 +0000
- Revision:
- 7:7a73065d2d92
- Parent:
- 6:1d7d769b398d
- Child:
- 8:3fc2849c098d
mixed up pin fixed
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 | 6:1d7d769b398d | 33 | int firstTime; |
| tyleralt | 0:d9730cf42ecc | 34 | |
| tyleralt | 6:1d7d769b398d | 35 | //serial port for pc and bluetooth |
| tyleralt | 0:d9730cf42ecc | 36 | Serial pc(USBTX, USBRX); // tx, rx |
| tyleralt | 3:1e9b4a4bf177 | 37 | Serial bt(p9, p10);// tx, rx |
| tyleralt | 0:d9730cf42ecc | 38 | |
| tyleralt | 6:1d7d769b398d | 39 | //define variables for ziggbee |
| tyleralt | 6:1d7d769b398d | 40 | MRF24J40 mrf(p11, p12, p13, p14, p26); |
| tyleralt | 6:1d7d769b398d | 41 | char txBuffer[128]; |
| tyleralt | 6:1d7d769b398d | 42 | char rxBuffer[128]; |
| tyleralt | 6:1d7d769b398d | 43 | int rxLen; |
| tyleralt | 6:1d7d769b398d | 44 | |
| tyleralt | 6:1d7d769b398d | 45 | |
| tyleralt | 6:1d7d769b398d | 46 | int rf_receive(char *data, uint8_t maxLength) |
| tyleralt | 6:1d7d769b398d | 47 | { |
| tyleralt | 6:1d7d769b398d | 48 | uint8_t len = mrf.Receive((uint8_t *)data, maxLength); |
| tyleralt | 6:1d7d769b398d | 49 | uint8_t header[8]= {1, 8, 0, 0xA1, 0xB2, 0xC3, 0xD4, 0x00}; |
| tyleralt | 6:1d7d769b398d | 50 | |
| tyleralt | 6:1d7d769b398d | 51 | if(len > 10) { |
| tyleralt | 6:1d7d769b398d | 52 | //Remove the header and footer of the message |
| tyleralt | 6:1d7d769b398d | 53 | for(uint8_t i = 0; i < len-2; i++) { |
| tyleralt | 6:1d7d769b398d | 54 | if(i<8) { |
| tyleralt | 6:1d7d769b398d | 55 | //Make sure our header is valid first |
| tyleralt | 6:1d7d769b398d | 56 | if(data[i] != header[i]) |
| tyleralt | 6:1d7d769b398d | 57 | return 0; |
| tyleralt | 6:1d7d769b398d | 58 | } else { |
| tyleralt | 6:1d7d769b398d | 59 | data[i-8] = data[i]; |
| tyleralt | 6:1d7d769b398d | 60 | } |
| tyleralt | 6:1d7d769b398d | 61 | } |
| tyleralt | 6:1d7d769b398d | 62 | |
| tyleralt | 6:1d7d769b398d | 63 | //pc.printf("Received: %s length:%d\r\n", data, ((int)len)-10); |
| tyleralt | 6:1d7d769b398d | 64 | } |
| tyleralt | 6:1d7d769b398d | 65 | return ((int)len)-10; |
| tyleralt | 6:1d7d769b398d | 66 | } |
| tyleralt | 6:1d7d769b398d | 67 | void recievePoint (int slice, int distance, char addChar){ |
| tyleralt | 6:1d7d769b398d | 68 | //TODO make this edit the other thing |
| tyleralt | 6:1d7d769b398d | 69 | } |
| tyleralt | 0:d9730cf42ecc | 70 | |
| tyleralt | 4:f81b40e04a58 | 71 | void pushData (char bits [16]){ |
| tyleralt | 6:1d7d769b398d | 72 | bt.printf("pushing data \r\n"); |
| tyleralt | 4:f81b40e04a58 | 73 | for (int i = 8; i < 16; i ++){ |
| tyleralt | 4:f81b40e04a58 | 74 | dataArmOne = bits [i] & 0x01; |
| tyleralt | 4:f81b40e04a58 | 75 | dataArmTwo = bits [i] & 0x02; |
| tyleralt | 4:f81b40e04a58 | 76 | dataArmThree = bits [i]& 0x04; |
| tyleralt | 4:f81b40e04a58 | 77 | dataArmFour = bits [i]& 0x08; |
| tyleralt | 4:f81b40e04a58 | 78 | dataArmFive = bits [i] & 0x10; |
| tyleralt | 4:f81b40e04a58 | 79 | dataArmSix = bits [i] & 0x20; |
| tyleralt | 4:f81b40e04a58 | 80 | dataArmSeven = bits [i] & 0x40; |
| tyleralt | 4:f81b40e04a58 | 81 | dataArmEight = bits [i] & 0x80; |
| tyleralt | 1:ea12b9153c51 | 82 | |
| tyleralt | 1:ea12b9153c51 | 83 | pushBit = 1; |
| tyleralt | 1:ea12b9153c51 | 84 | pushBit = 0; |
| tyleralt | 1:ea12b9153c51 | 85 | } |
| tyleralt | 4:f81b40e04a58 | 86 | for (int i = 7; i >= 0; i --){ |
| tyleralt | 4:f81b40e04a58 | 87 | dataArmOne = bits [i] & 0x01; |
| tyleralt | 4:f81b40e04a58 | 88 | dataArmTwo = bits [i] & 0x02; |
| tyleralt | 4:f81b40e04a58 | 89 | dataArmThree = bits [i]& 0x04; |
| tyleralt | 4:f81b40e04a58 | 90 | dataArmFour = bits [i]& 0x08; |
| tyleralt | 4:f81b40e04a58 | 91 | dataArmFive = bits [i] & 0x10; |
| tyleralt | 4:f81b40e04a58 | 92 | dataArmSix = bits [i] & 0x20; |
| tyleralt | 4:f81b40e04a58 | 93 | dataArmSeven = bits [i] & 0x40; |
| tyleralt | 4:f81b40e04a58 | 94 | dataArmEight = bits [i] & 0x80; |
| tyleralt | 4:f81b40e04a58 | 95 | |
| tyleralt | 4:f81b40e04a58 | 96 | pushBit = 1; |
| tyleralt | 4:f81b40e04a58 | 97 | pushBit = 0; |
| tyleralt | 4:f81b40e04a58 | 98 | } |
| tyleralt | 4:f81b40e04a58 | 99 | |
| tyleralt | 4:f81b40e04a58 | 100 | pushRegister = 1; |
| tyleralt | 4:f81b40e04a58 | 101 | pushRegister = 0; |
| tyleralt | 1:ea12b9153c51 | 102 | } |
| tyleralt | 1:ea12b9153c51 | 103 | |
| tyleralt | 1:ea12b9153c51 | 104 | |
| tyleralt | 1:ea12b9153c51 | 105 | //periodic display interrupt |
| tyleralt | 1:ea12b9153c51 | 106 | void nextLedPush(){ |
| tyleralt | 1:ea12b9153c51 | 107 | if (current_slice < NUMBER_OF_SLICES){ |
| tyleralt | 4:f81b40e04a58 | 108 | pushData(slice_data[current_slice]); |
| tyleralt | 1:ea12b9153c51 | 109 | current_slice ++; |
| tyleralt | 2:554edc4b0cf2 | 110 | } else { |
| tyleralt | 2:554edc4b0cf2 | 111 | updateLeds.detach(); |
| tyleralt | 1:ea12b9153c51 | 112 | } |
| tyleralt | 1:ea12b9153c51 | 113 | } |
| tyleralt | 1:ea12b9153c51 | 114 | |
| tyleralt | 0:d9730cf42ecc | 115 | //Hall sensor interupt |
| tyleralt | 0:d9730cf42ecc | 116 | void rotate_sense(){ |
| tyleralt | 6:1d7d769b398d | 117 | bt.printf("interupt"); |
| tyleralt | 0:d9730cf42ecc | 118 | if (firstTime){ |
| tyleralt | 0:d9730cf42ecc | 119 | rotationTime.reset(); |
| tyleralt | 0:d9730cf42ecc | 120 | rotationTime.start(); |
| tyleralt | 6:1d7d769b398d | 121 | firstTime = false; |
| tyleralt | 3:1e9b4a4bf177 | 122 | pc.printf("first time"); |
| tyleralt | 2:554edc4b0cf2 | 123 | return; |
| tyleralt | 4:f81b40e04a58 | 124 | } else if(current_slice < NUMBER_OF_SLICES / 4){ |
| tyleralt | 2:554edc4b0cf2 | 125 | return; |
| tyleralt | 2:554edc4b0cf2 | 126 | } |
| tyleralt | 6:1d7d769b398d | 127 | bt.printf("seconod time \n"); |
| tyleralt | 0:d9730cf42ecc | 128 | rotate_time = rotationTime.read_us(); |
| tyleralt | 0:d9730cf42ecc | 129 | rotationTime.reset(); |
| tyleralt | 0:d9730cf42ecc | 130 | rotationTime.start(); |
| tyleralt | 0:d9730cf42ecc | 131 | |
| tyleralt | 2:554edc4b0cf2 | 132 | slice_time = (double) rotate_time/NUMBER_OF_SLICES; |
| tyleralt | 0:d9730cf42ecc | 133 | current_slice = 0; |
| tyleralt | 2:554edc4b0cf2 | 134 | updateLeds.attach_us(&nextLedPush, slice_time); |
| tyleralt | 0:d9730cf42ecc | 135 | } |
| tyleralt | 0:d9730cf42ecc | 136 | |
| tyleralt | 6:1d7d769b398d | 137 | void didRecievePoint(){ |
| tyleralt | 6:1d7d769b398d | 138 | } |
| tyleralt | 6:1d7d769b398d | 139 | |
| tyleralt | 0:d9730cf42ecc | 140 | |
| tyleralt | 0:d9730cf42ecc | 141 | int main() { |
| tyleralt | 6:1d7d769b398d | 142 | bt.printf("started"); |
| tyleralt | 6:1d7d769b398d | 143 | firstTime = true; |
| tyleralt | 6:1d7d769b398d | 144 | current_slice = 0; |
| tyleralt | 6:1d7d769b398d | 145 | |
| tyleralt | 6:1d7d769b398d | 146 | uint8_t channel = 6; |
| tyleralt | 6:1d7d769b398d | 147 | //mrf.SetChannel(channel); |
| tyleralt | 0:d9730cf42ecc | 148 | |
| tyleralt | 3:1e9b4a4bf177 | 149 | hallSensor.fall(&rotate_sense); |
| tyleralt | 6:1d7d769b398d | 150 | |
| tyleralt | 6:1d7d769b398d | 151 | for (int i = 0; i < 60; i ++){ |
| tyleralt | 4:f81b40e04a58 | 152 | slice_data [i][0] = 0x01; // bit 0 is high on arm 0 |
| tyleralt | 5:6d8f9165021e | 153 | slice_data [i][1] = 0x01; |
| tyleralt | 5:6d8f9165021e | 154 | slice_data [i][2] = 0x01; |
| tyleralt | 5:6d8f9165021e | 155 | slice_data [i][3] = 0x01; |
| tyleralt | 5:6d8f9165021e | 156 | slice_data [i][4] = 0x01; |
| tyleralt | 5:6d8f9165021e | 157 | slice_data [i][5] = 0x01; |
| tyleralt | 5:6d8f9165021e | 158 | slice_data [i][6] = 0x01; |
| tyleralt | 5:6d8f9165021e | 159 | slice_data [i][7] = 0x01; |
| tyleralt | 5:6d8f9165021e | 160 | slice_data [i][8] = 0x01; |
| tyleralt | 5:6d8f9165021e | 161 | slice_data [i][9] = 0x01; |
| tyleralt | 5:6d8f9165021e | 162 | slice_data [i][10] = 0x01; |
| tyleralt | 5:6d8f9165021e | 163 | slice_data [i][11] = 0x01; |
| tyleralt | 5:6d8f9165021e | 164 | slice_data [i][12] = 0x01; |
| tyleralt | 5:6d8f9165021e | 165 | slice_data [i][13] = 0x01; |
| tyleralt | 5:6d8f9165021e | 166 | slice_data [i][14] = 0x01; |
| tyleralt | 5:6d8f9165021e | 167 | slice_data [i][15] = 0x01; |
| tyleralt | 0:d9730cf42ecc | 168 | } |
| tyleralt | 6:1d7d769b398d | 169 | for (int i = 60; i < 120; i++){ |
| tyleralt | 4:f81b40e04a58 | 170 | slice_data [i][0] = 0x00; // bit 0 is high on arm 0 |
| tyleralt | 5:6d8f9165021e | 171 | slice_data [i][1] = 0x01; |
| tyleralt | 4:f81b40e04a58 | 172 | slice_data [i][2] = 0x00; |
| tyleralt | 5:6d8f9165021e | 173 | slice_data [i][3] = 0x01; |
| tyleralt | 4:f81b40e04a58 | 174 | slice_data [i][4] = 0x00; |
| tyleralt | 5:6d8f9165021e | 175 | slice_data [i][5] = 0x01; |
| tyleralt | 4:f81b40e04a58 | 176 | slice_data [i][6] = 0x00; |
| tyleralt | 5:6d8f9165021e | 177 | slice_data [i][7] = 0x01; |
| tyleralt | 5:6d8f9165021e | 178 | slice_data [i][8] = 0x00; |
| tyleralt | 5:6d8f9165021e | 179 | slice_data [i][9] = 0x01; |
| tyleralt | 4:f81b40e04a58 | 180 | slice_data [i][10] = 0x00; |
| tyleralt | 5:6d8f9165021e | 181 | slice_data [i][11] = 0x01; |
| tyleralt | 4:f81b40e04a58 | 182 | slice_data [i][12] = 0x00; |
| tyleralt | 5:6d8f9165021e | 183 | slice_data [i][13] = 0x01; |
| tyleralt | 4:f81b40e04a58 | 184 | slice_data [i][14] = 0x00; |
| tyleralt | 5:6d8f9165021e | 185 | slice_data [i][15] = 0x01; |
| tyleralt | 0:d9730cf42ecc | 186 | } |
| tyleralt | 6:1d7d769b398d | 187 | |
| tyleralt | 6:1d7d769b398d | 188 | pushData(slice_data[80]); |
| tyleralt | 6:1d7d769b398d | 189 | while(true) { |
| tyleralt | 6:1d7d769b398d | 190 | //Try to receive some data |
| tyleralt | 6:1d7d769b398d | 191 | |
| tyleralt | 6:1d7d769b398d | 192 | //rxLen = rf_receive(rxBuffer, 24); |
| tyleralt | 6:1d7d769b398d | 193 | //if(rxLen > 24) { |
| tyleralt | 6:1d7d769b398d | 194 | |
| tyleralt | 6:1d7d769b398d | 195 | //} |
| tyleralt | 0:d9730cf42ecc | 196 | } |
| tyleralt | 0:d9730cf42ecc | 197 | } |