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@2:e2502545f2f0, 2015-04-22 (annotated)
- Committer:
- tyleralt
- Date:
- Wed Apr 22 00:28:29 2015 +0000
- Revision:
- 2:e2502545f2f0
- Parent:
- 1:533b878c22ab
- Child:
- 3:9e8eb12f831f
using interrupts;
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| tyleralt | 0:c8b0c4f50d18 | 1 | #include "mbed.h" |
| tyleralt | 0:c8b0c4f50d18 | 2 | #include <vector> |
| tyleralt | 0:c8b0c4f50d18 | 3 | #define BUFFER_SIZE 16 |
| tyleralt | 0:c8b0c4f50d18 | 4 | #define NUMBER_OF_SLICES 120 |
| tyleralt | 2:e2502545f2f0 | 5 | #include "MRF24J40.h" |
| tyleralt | 0:c8b0c4f50d18 | 6 | |
| tyleralt | 0:c8b0c4f50d18 | 7 | |
| tyleralt | 0:c8b0c4f50d18 | 8 | int offAngles [8] = {0, 180, 90, 270, 45, 225, 135, 315}; |
| tyleralt | 0:c8b0c4f50d18 | 9 | char slice_data [NUMBER_OF_SLICES][16]; //[slice][specific led distance] (0 is closest) & with approppriate bit for each arm |
| tyleralt | 0:c8b0c4f50d18 | 10 | |
| tyleralt | 2:e2502545f2f0 | 11 | //*********************point class************************// |
| tyleralt | 0:c8b0c4f50d18 | 12 | class Point { |
| tyleralt | 0:c8b0c4f50d18 | 13 | int positionRadian, positionHeight, positionDistance; |
| tyleralt | 0:c8b0c4f50d18 | 14 | public: |
| tyleralt | 0:c8b0c4f50d18 | 15 | Point (int , int , int ); |
| tyleralt | 0:c8b0c4f50d18 | 16 | int getArraySlice(void); |
| tyleralt | 0:c8b0c4f50d18 | 17 | char getIdentifyingChar(void); |
| tyleralt | 0:c8b0c4f50d18 | 18 | int getPositionDistance(void); |
| tyleralt | 0:c8b0c4f50d18 | 19 | void moveUp(); |
| tyleralt | 0:c8b0c4f50d18 | 20 | void moveDown(); |
| tyleralt | 0:c8b0c4f50d18 | 21 | void rotateRight(); |
| tyleralt | 0:c8b0c4f50d18 | 22 | void rotateLeft(); |
| tyleralt | 0:c8b0c4f50d18 | 23 | void moveIn(); |
| tyleralt | 0:c8b0c4f50d18 | 24 | void moveOut(); |
| tyleralt | 0:c8b0c4f50d18 | 25 | |
| tyleralt | 0:c8b0c4f50d18 | 26 | }; |
| tyleralt | 0:c8b0c4f50d18 | 27 | Point :: Point (int posRadian, int posHeight, int posDistance){ |
| tyleralt | 0:c8b0c4f50d18 | 28 | positionRadian = posRadian; |
| tyleralt | 0:c8b0c4f50d18 | 29 | positionHeight = posHeight; |
| tyleralt | 0:c8b0c4f50d18 | 30 | positionDistance = posDistance; |
| tyleralt | 0:c8b0c4f50d18 | 31 | } |
| tyleralt | 0:c8b0c4f50d18 | 32 | int Point :: getArraySlice (void){ |
| tyleralt | 1:533b878c22ab | 33 | return positionRadian + offAngles[positionHeight]; |
| tyleralt | 0:c8b0c4f50d18 | 34 | } |
| tyleralt | 0:c8b0c4f50d18 | 35 | char Point :: getIdentifyingChar(void){ |
| tyleralt | 1:533b878c22ab | 36 | return 0x01 << positionHeight; |
| tyleralt | 0:c8b0c4f50d18 | 37 | } |
| tyleralt | 0:c8b0c4f50d18 | 38 | int Point :: getPositionDistance(void){ |
| tyleralt | 0:c8b0c4f50d18 | 39 | return positionDistance; |
| tyleralt | 0:c8b0c4f50d18 | 40 | } |
| tyleralt | 1:533b878c22ab | 41 | void Point :: moveUp(){ |
| tyleralt | 0:c8b0c4f50d18 | 42 | if (positionHeight < 7){ |
| tyleralt | 0:c8b0c4f50d18 | 43 | positionHeight ++; |
| tyleralt | 0:c8b0c4f50d18 | 44 | } |
| tyleralt | 0:c8b0c4f50d18 | 45 | } |
| tyleralt | 1:533b878c22ab | 46 | void Point :: moveDown(){ |
| tyleralt | 1:533b878c22ab | 47 | if(positionHeight > 0){ |
| tyleralt | 0:c8b0c4f50d18 | 48 | positionHeight --; |
| tyleralt | 1:533b878c22ab | 49 | } |
| tyleralt | 1:533b878c22ab | 50 | } |
| tyleralt | 1:533b878c22ab | 51 | void Point :: rotateRight(){ |
| tyleralt | 1:533b878c22ab | 52 | positionRadian = (positionRadian + 1) % 120; |
| tyleralt | 1:533b878c22ab | 53 | } |
| tyleralt | 1:533b878c22ab | 54 | void Point :: rotateLeft(){ |
| tyleralt | 1:533b878c22ab | 55 | positionRadian = (positionRadian + 1) % 120; |
| tyleralt | 1:533b878c22ab | 56 | } |
| tyleralt | 1:533b878c22ab | 57 | void Point :: moveIn(){ |
| tyleralt | 1:533b878c22ab | 58 | if (positionDistance < 0){ |
| tyleralt | 1:533b878c22ab | 59 | positionDistance --; |
| tyleralt | 1:533b878c22ab | 60 | } |
| tyleralt | 1:533b878c22ab | 61 | } |
| tyleralt | 1:533b878c22ab | 62 | void Point :: moveOut(){ |
| tyleralt | 1:533b878c22ab | 63 | if (positionDistance > 15){ |
| tyleralt | 1:533b878c22ab | 64 | positionDistance ++; |
| tyleralt | 1:533b878c22ab | 65 | } |
| tyleralt | 1:533b878c22ab | 66 | } |
| tyleralt | 2:e2502545f2f0 | 67 | |
| tyleralt | 2:e2502545f2f0 | 68 | //******************end Point class*******************// |
| tyleralt | 0:c8b0c4f50d18 | 69 | |
| tyleralt | 1:533b878c22ab | 70 | enum DrawingMode { line, point, circle }; |
| tyleralt | 1:533b878c22ab | 71 | DrawingMode currentMode; |
| tyleralt | 1:533b878c22ab | 72 | |
| tyleralt | 1:533b878c22ab | 73 | Point pointer(60, 3, 8); |
| tyleralt | 0:c8b0c4f50d18 | 74 | |
| tyleralt | 0:c8b0c4f50d18 | 75 | InterruptIn buttonMode(p15); |
| tyleralt | 0:c8b0c4f50d18 | 76 | InterruptIn buttonUp(p16); |
| tyleralt | 0:c8b0c4f50d18 | 77 | InterruptIn buttonDown(p17); |
| tyleralt | 0:c8b0c4f50d18 | 78 | InterruptIn buttonRotateRight(p18); |
| tyleralt | 0:c8b0c4f50d18 | 79 | InterruptIn buttonRotateLeft(p19); |
| tyleralt | 0:c8b0c4f50d18 | 80 | InterruptIn buttonMoveOut(p20); |
| tyleralt | 0:c8b0c4f50d18 | 81 | InterruptIn buttonMoveIn(p21); |
| tyleralt | 1:533b878c22ab | 82 | InterruptIn buttonSelect(p22); |
| tyleralt | 2:e2502545f2f0 | 83 | DigitalIn digitalMode(p15); |
| tyleralt | 2:e2502545f2f0 | 84 | DigitalIn digitalUp(p16); |
| tyleralt | 2:e2502545f2f0 | 85 | DigitalIn digitalDown(p17); |
| tyleralt | 2:e2502545f2f0 | 86 | DigitalIn digitalRotateRight(p18); |
| tyleralt | 2:e2502545f2f0 | 87 | DigitalIn digitalRotateLeft(p19); |
| tyleralt | 2:e2502545f2f0 | 88 | DigitalIn digotalMoveOut(p20); |
| tyleralt | 2:e2502545f2f0 | 89 | DigitalIn digtialMoveIn(p21); |
| tyleralt | 2:e2502545f2f0 | 90 | DigitalIn digitalSelect(p22); |
| tyleralt | 0:c8b0c4f50d18 | 91 | |
| tyleralt | 1:533b878c22ab | 92 | Ticker pointerBlinkTicker; |
| tyleralt | 1:533b878c22ab | 93 | |
| tyleralt | 2:e2502545f2f0 | 94 | MRF24J40 mrf(p11, p12, p13, p14, p26); |
| tyleralt | 2:e2502545f2f0 | 95 | char txBuffer[128]; |
| tyleralt | 2:e2502545f2f0 | 96 | char rxBuffer[128]; |
| tyleralt | 2:e2502545f2f0 | 97 | int rxLen; |
| tyleralt | 2:e2502545f2f0 | 98 | |
| tyleralt | 2:e2502545f2f0 | 99 | void rf_send(char *data, uint8_t len) |
| tyleralt | 2:e2502545f2f0 | 100 | { |
| tyleralt | 2:e2502545f2f0 | 101 | //We need to prepend the message with a valid ZigBee header |
| tyleralt | 2:e2502545f2f0 | 102 | uint8_t header[8]= {1, 8, 0, 0xA1, 0xB2, 0xC3, 0xD4, 0x00}; |
| tyleralt | 2:e2502545f2f0 | 103 | uint8_t *send_buf = (uint8_t *) malloc( sizeof(uint8_t) * (len+8) ); |
| tyleralt | 2:e2502545f2f0 | 104 | |
| tyleralt | 2:e2502545f2f0 | 105 | for(uint8_t i = 0; i < len+8; i++) { |
| tyleralt | 2:e2502545f2f0 | 106 | //prepend the 8-byte header |
| tyleralt | 2:e2502545f2f0 | 107 | send_buf[i] = (i<8) ? header[i] : data[i-8]; |
| tyleralt | 2:e2502545f2f0 | 108 | } |
| tyleralt | 2:e2502545f2f0 | 109 | //pc.printf("Sent: %s\r\n", send_buf+8); |
| tyleralt | 2:e2502545f2f0 | 110 | |
| tyleralt | 2:e2502545f2f0 | 111 | mrf.Send(send_buf, len+8); |
| tyleralt | 2:e2502545f2f0 | 112 | free(send_buf); |
| tyleralt | 2:e2502545f2f0 | 113 | } |
| tyleralt | 2:e2502545f2f0 | 114 | |
| tyleralt | 1:533b878c22ab | 115 | void sendPointOver (int slice, int distance, char height){ |
| tyleralt | 1:533b878c22ab | 116 | ///TODO implement communications |
| tyleralt | 2:e2502545f2f0 | 117 | char sliceChar = (char) slice; |
| tyleralt | 2:e2502545f2f0 | 118 | char distanceChar = (char) distance; |
| tyleralt | 2:e2502545f2f0 | 119 | String data = sliceChar + distanceChar + height; |
| tyleralt | 2:e2502545f2f0 | 120 | |
| tyleralt | 2:e2502545f2f0 | 121 | strcpy(txBuffer, data); |
| tyleralt | 2:e2502545f2f0 | 122 | //Send the buffer |
| tyleralt | 2:e2502545f2f0 | 123 | rf_send(txBuffer, strlen(txBuffer) + 1); |
| tyleralt | 2:e2502545f2f0 | 124 | pc.printf("Sent: %s\r\n", txBuffer); |
| tyleralt | 1:533b878c22ab | 125 | } |
| tyleralt | 1:533b878c22ab | 126 | |
| tyleralt | 1:533b878c22ab | 127 | void drawPoint(){ |
| tyleralt | 1:533b878c22ab | 128 | int pointerSlice = pointer.getArraySlice(); |
| tyleralt | 1:533b878c22ab | 129 | int pointerDistance = pointer.getPositionDistance(); |
| tyleralt | 1:533b878c22ab | 130 | char height = pointer.getIdentifyingChar(); |
| tyleralt | 1:533b878c22ab | 131 | sendPointOver(pointerSlice, pointerDistance, height); |
| tyleralt | 1:533b878c22ab | 132 | } |
| tyleralt | 1:533b878c22ab | 133 | |
| tyleralt | 0:c8b0c4f50d18 | 134 | |
| tyleralt | 2:e2502545f2f0 | 135 | |
| tyleralt | 2:e2502545f2f0 | 136 | //***************** all of the button reactions *********// |
| tyleralt | 0:c8b0c4f50d18 | 137 | void changeMode(){ |
| tyleralt | 1:533b878c22ab | 138 | if (currentMode == point){ |
| tyleralt | 1:533b878c22ab | 139 | currentMode = line; |
| tyleralt | 1:533b878c22ab | 140 | } |
| tyleralt | 1:533b878c22ab | 141 | if (currentMode == line){ |
| tyleralt | 1:533b878c22ab | 142 | currentMode = circle; |
| tyleralt | 1:533b878c22ab | 143 | } |
| tyleralt | 1:533b878c22ab | 144 | if (currentMode == circle){ |
| tyleralt | 1:533b878c22ab | 145 | currentMode = point; |
| tyleralt | 1:533b878c22ab | 146 | } |
| tyleralt | 0:c8b0c4f50d18 | 147 | } |
| tyleralt | 0:c8b0c4f50d18 | 148 | void moveUp(){ |
| tyleralt | 1:533b878c22ab | 149 | if (currentMode == line){ |
| tyleralt | 1:533b878c22ab | 150 | drawPoint(); |
| tyleralt | 1:533b878c22ab | 151 | } |
| tyleralt | 1:533b878c22ab | 152 | pointer.moveUp(); |
| tyleralt | 0:c8b0c4f50d18 | 153 | } |
| tyleralt | 0:c8b0c4f50d18 | 154 | void moveDown(){ |
| tyleralt | 1:533b878c22ab | 155 | if (currentMode == line){ |
| tyleralt | 1:533b878c22ab | 156 | drawPoint(); |
| tyleralt | 1:533b878c22ab | 157 | } |
| tyleralt | 1:533b878c22ab | 158 | pointer.moveDown(); |
| tyleralt | 0:c8b0c4f50d18 | 159 | } |
| tyleralt | 0:c8b0c4f50d18 | 160 | void rotateRight(){ |
| tyleralt | 1:533b878c22ab | 161 | if (currentMode == line){ |
| tyleralt | 1:533b878c22ab | 162 | pointer.rotateRight(); |
| tyleralt | 1:533b878c22ab | 163 | } |
| tyleralt | 1:533b878c22ab | 164 | pointer.rotateRight(); |
| tyleralt | 0:c8b0c4f50d18 | 165 | } |
| tyleralt | 0:c8b0c4f50d18 | 166 | void rotateLeft(){ |
| tyleralt | 1:533b878c22ab | 167 | if (currentMode == line){ |
| tyleralt | 1:533b878c22ab | 168 | drawPoint(); |
| tyleralt | 1:533b878c22ab | 169 | } |
| tyleralt | 1:533b878c22ab | 170 | pointer.rotateLeft(); |
| tyleralt | 2:e2502545f2f0 | 171 | wait(.3); |
| tyleralt | 2:e2502545f2f0 | 172 | while(buttonRotateLeft. |
| tyleralt | 0:c8b0c4f50d18 | 173 | } |
| tyleralt | 0:c8b0c4f50d18 | 174 | void moveOut(){ |
| tyleralt | 1:533b878c22ab | 175 | if (currentMode == line){ |
| tyleralt | 1:533b878c22ab | 176 | drawPoint(); |
| tyleralt | 1:533b878c22ab | 177 | } |
| tyleralt | 1:533b878c22ab | 178 | pointer.moveOut(); |
| tyleralt | 0:c8b0c4f50d18 | 179 | } |
| tyleralt | 0:c8b0c4f50d18 | 180 | void moveIn(){ |
| tyleralt | 1:533b878c22ab | 181 | if (currentMode == line){ |
| tyleralt | 1:533b878c22ab | 182 | drawPoint(); |
| tyleralt | 1:533b878c22ab | 183 | } |
| tyleralt | 1:533b878c22ab | 184 | pointer.moveIn(); |
| tyleralt | 0:c8b0c4f50d18 | 185 | } |
| tyleralt | 0:c8b0c4f50d18 | 186 | void pointerBlink(){ |
| tyleralt | 1:533b878c22ab | 187 | Point currentBlink = pointer; |
| tyleralt | 1:533b878c22ab | 188 | int slice = currentBlink.getArraySlice(); |
| tyleralt | 1:533b878c22ab | 189 | int distance = currentBlink.getPositionDistance(); |
| tyleralt | 1:533b878c22ab | 190 | char height = pointer.getIdentifyingChar(); |
| tyleralt | 1:533b878c22ab | 191 | sendPointOver(slice, distance, height); |
| tyleralt | 1:533b878c22ab | 192 | wait(.3); |
| tyleralt | 1:533b878c22ab | 193 | sendPointOver(slice, distance, height); |
| tyleralt | 0:c8b0c4f50d18 | 194 | } |
| tyleralt | 1:533b878c22ab | 195 | void drawWholeCircle(){ |
| tyleralt | 1:533b878c22ab | 196 | int distance = pointer.getPositionDistance(); |
| tyleralt | 1:533b878c22ab | 197 | char height = pointer.getIdentifyingChar(); |
| tyleralt | 1:533b878c22ab | 198 | for (int i = 0; i < 120; i ++){ |
| tyleralt | 1:533b878c22ab | 199 | sendPointOver(i, distance, height); |
| tyleralt | 1:533b878c22ab | 200 | } |
| tyleralt | 1:533b878c22ab | 201 | |
| tyleralt | 1:533b878c22ab | 202 | } |
| tyleralt | 1:533b878c22ab | 203 | void select(){ |
| tyleralt | 1:533b878c22ab | 204 | if (currentMode == point){ |
| tyleralt | 1:533b878c22ab | 205 | drawPoint(); |
| tyleralt | 1:533b878c22ab | 206 | } |
| tyleralt | 1:533b878c22ab | 207 | if (currentMode == circle){ |
| tyleralt | 1:533b878c22ab | 208 | drawWholeCircle(); |
| tyleralt | 1:533b878c22ab | 209 | } |
| tyleralt | 1:533b878c22ab | 210 | } |
| tyleralt | 2:e2502545f2f0 | 211 | //***************** all of the button reactions finish*********// |
| tyleralt | 0:c8b0c4f50d18 | 212 | |
| tyleralt | 0:c8b0c4f50d18 | 213 | |
| tyleralt | 0:c8b0c4f50d18 | 214 | int main() { |
| tyleralt | 0:c8b0c4f50d18 | 215 | currentMode = point; |
| tyleralt | 0:c8b0c4f50d18 | 216 | buttonMode.rise(&changeMode); |
| tyleralt | 0:c8b0c4f50d18 | 217 | buttonUp.rise(&moveUp); |
| tyleralt | 0:c8b0c4f50d18 | 218 | buttonDown.rise(&moveDown); |
| tyleralt | 0:c8b0c4f50d18 | 219 | buttonRotateRight.rise(&rotateRight); |
| tyleralt | 0:c8b0c4f50d18 | 220 | buttonRotateLeft.rise(&rotateLeft); |
| tyleralt | 0:c8b0c4f50d18 | 221 | buttonMoveOut.rise(&moveOut); |
| tyleralt | 0:c8b0c4f50d18 | 222 | buttonMoveIn.rise(&moveIn); |
| tyleralt | 1:533b878c22ab | 223 | buttonSelect.rise(&select); |
| tyleralt | 1:533b878c22ab | 224 | pointerBlinkTicker.attach(&pointerBlink, 1); |
| tyleralt | 1:533b878c22ab | 225 | |
| tyleralt | 0:c8b0c4f50d18 | 226 | |
| tyleralt | 0:c8b0c4f50d18 | 227 | for (int i = 0; i < NUMBER_OF_SLICES; i ++){ |
| tyleralt | 0:c8b0c4f50d18 | 228 | for (int j = 0; j < 16; j ++){ |
| tyleralt | 1:533b878c22ab | 229 | slice_data[i][j] = 0x00; |
| tyleralt | 0:c8b0c4f50d18 | 230 | } |
| tyleralt | 0:c8b0c4f50d18 | 231 | } |
| tyleralt | 0:c8b0c4f50d18 | 232 | |
| tyleralt | 0:c8b0c4f50d18 | 233 | |
| tyleralt | 0:c8b0c4f50d18 | 234 | } |