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:891b3618be4f, 2015-05-01 (annotated)
- Committer:
- tyleralt
- Date:
- Fri May 01 10:12:55 2015 +0000
- Revision:
- 2:891b3618be4f
- Parent:
- 1:bb1507f0bb64
- Child:
- 5:d7d16cb9c974
working yo
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tyleralt | 0:ded79d89abdf | 1 | #include "mbed.h" |
tyleralt | 0:ded79d89abdf | 2 | #include <vector> |
tyleralt | 0:ded79d89abdf | 3 | #define BUFFER_SIZE 16 |
tyleralt | 0:ded79d89abdf | 4 | #define NUMBER_OF_SLICES 360 |
tyleralt | 0:ded79d89abdf | 5 | #include "Point.h" |
tyleralt | 0:ded79d89abdf | 6 | #include "View.h" |
tyleralt | 2:891b3618be4f | 7 | #include "Block.h" |
tyleralt | 0:ded79d89abdf | 8 | |
tyleralt | 0:ded79d89abdf | 9 | |
tyleralt | 0:ded79d89abdf | 10 | //hass sensor interupt |
tyleralt | 0:ded79d89abdf | 11 | InterruptIn hallSensor(p25); |
tyleralt | 0:ded79d89abdf | 12 | |
tyleralt | 0:ded79d89abdf | 13 | //Set Up Timer and ticker |
tyleralt | 0:ded79d89abdf | 14 | Timer rotationTime; |
tyleralt | 0:ded79d89abdf | 15 | Ticker updateLeds; |
tyleralt | 0:ded79d89abdf | 16 | |
tyleralt | 0:ded79d89abdf | 17 | //Declare global vars |
tyleralt | 0:ded79d89abdf | 18 | double slice_time; |
tyleralt | 0:ded79d89abdf | 19 | double rotate_time; |
tyleralt | 0:ded79d89abdf | 20 | int current_slice; |
tyleralt | 0:ded79d89abdf | 21 | bool firstTime; |
tyleralt | 0:ded79d89abdf | 22 | |
tyleralt | 0:ded79d89abdf | 23 | enum DisplayMode {lineDraw, map, eucDraw}; |
tyleralt | 0:ded79d89abdf | 24 | |
tyleralt | 0:ded79d89abdf | 25 | DisplayMode mode; |
tyleralt | 0:ded79d89abdf | 26 | |
tyleralt | 0:ded79d89abdf | 27 | Point pointer; |
tyleralt | 0:ded79d89abdf | 28 | EuclidPoint eucPointer; |
tyleralt | 0:ded79d89abdf | 29 | //the View |
tyleralt | 0:ded79d89abdf | 30 | View display; |
tyleralt | 0:ded79d89abdf | 31 | |
tyleralt | 2:891b3618be4f | 32 | Block blocks [3]; |
tyleralt | 0:ded79d89abdf | 33 | |
tyleralt | 0:ded79d89abdf | 34 | |
tyleralt | 0:ded79d89abdf | 35 | |
tyleralt | 0:ded79d89abdf | 36 | //serial port for pc and bluetooth |
tyleralt | 0:ded79d89abdf | 37 | Serial pc(USBTX, USBRX); // tx, rx |
tyleralt | 0:ded79d89abdf | 38 | Serial bt(p9, p10);// tx, rx |
tyleralt | 0:ded79d89abdf | 39 | |
tyleralt | 2:891b3618be4f | 40 | void updateMap(){ |
tyleralt | 2:891b3618be4f | 41 | display.resetDisplay(); |
tyleralt | 2:891b3618be4f | 42 | display.addBlock(blocks[0]); |
tyleralt | 2:891b3618be4f | 43 | display.addBlock(blocks[1]); |
tyleralt | 2:891b3618be4f | 44 | display.addBlock(blocks[2]); |
tyleralt | 2:891b3618be4f | 45 | } |
tyleralt | 2:891b3618be4f | 46 | |
tyleralt | 0:ded79d89abdf | 47 | void rotate_sense(){ |
tyleralt | 0:ded79d89abdf | 48 | if (firstTime){ |
tyleralt | 0:ded79d89abdf | 49 | rotationTime.reset(); |
tyleralt | 0:ded79d89abdf | 50 | rotationTime.start(); |
tyleralt | 0:ded79d89abdf | 51 | firstTime = false; |
tyleralt | 0:ded79d89abdf | 52 | display.current_slice = 180; |
tyleralt | 0:ded79d89abdf | 53 | return; |
tyleralt | 0:ded79d89abdf | 54 | } |
tyleralt | 0:ded79d89abdf | 55 | if(display.current_slice < NUMBER_OF_SLICES / 4){ |
tyleralt | 0:ded79d89abdf | 56 | return; |
tyleralt | 0:ded79d89abdf | 57 | } |
tyleralt | 0:ded79d89abdf | 58 | rotate_time = rotationTime.read_us(); |
tyleralt | 0:ded79d89abdf | 59 | rotationTime.reset(); |
tyleralt | 0:ded79d89abdf | 60 | rotationTime.start(); |
tyleralt | 0:ded79d89abdf | 61 | slice_time = (double) rotate_time/NUMBER_OF_SLICES; |
tyleralt | 0:ded79d89abdf | 62 | display.resetCount(); |
tyleralt | 0:ded79d89abdf | 63 | updateLeds.attach_us(&display, &View::nextLedPush, slice_time); |
tyleralt | 0:ded79d89abdf | 64 | } |
tyleralt | 0:ded79d89abdf | 65 | void moveUp(); |
tyleralt | 0:ded79d89abdf | 66 | void moveDown(); |
tyleralt | 0:ded79d89abdf | 67 | void rotateRight(); |
tyleralt | 0:ded79d89abdf | 68 | void rotateLeft(); |
tyleralt | 0:ded79d89abdf | 69 | void moveIn(); |
tyleralt | 0:ded79d89abdf | 70 | void moveOut(); |
tyleralt | 0:ded79d89abdf | 71 | |
tyleralt | 0:ded79d89abdf | 72 | void parseLineMode(char c){ |
tyleralt | 0:ded79d89abdf | 73 | switch(c){ |
tyleralt | 0:ded79d89abdf | 74 | case 'w': |
tyleralt | 0:ded79d89abdf | 75 | pointer.moveOut(); |
tyleralt | 0:ded79d89abdf | 76 | break; |
tyleralt | 0:ded79d89abdf | 77 | case 's': |
tyleralt | 0:ded79d89abdf | 78 | pointer.moveIn(); |
tyleralt | 0:ded79d89abdf | 79 | break; |
tyleralt | 0:ded79d89abdf | 80 | case 'a': |
tyleralt | 0:ded79d89abdf | 81 | pointer.rotateRight(); |
tyleralt | 0:ded79d89abdf | 82 | break; |
tyleralt | 0:ded79d89abdf | 83 | case 'd': |
tyleralt | 0:ded79d89abdf | 84 | pointer.rotateLeft(); |
tyleralt | 0:ded79d89abdf | 85 | break; |
tyleralt | 0:ded79d89abdf | 86 | case 'p': |
tyleralt | 0:ded79d89abdf | 87 | pointer.moveUp(); |
tyleralt | 0:ded79d89abdf | 88 | break; |
tyleralt | 0:ded79d89abdf | 89 | case 'l': |
tyleralt | 0:ded79d89abdf | 90 | pointer.moveDown(); |
tyleralt | 0:ded79d89abdf | 91 | break; |
tyleralt | 0:ded79d89abdf | 92 | default: return; |
tyleralt | 0:ded79d89abdf | 93 | } |
tyleralt | 0:ded79d89abdf | 94 | display.addPoint(pointer); |
tyleralt | 0:ded79d89abdf | 95 | } |
tyleralt | 0:ded79d89abdf | 96 | |
tyleralt | 0:ded79d89abdf | 97 | void parseMapMode(char c){ |
tyleralt | 2:891b3618be4f | 98 | switch(c){ |
tyleralt | 2:891b3618be4f | 99 | case 'w': |
tyleralt | 2:891b3618be4f | 100 | for (int i = 0; i < 3; i ++){ |
tyleralt | 2:891b3618be4f | 101 | blocks[i].moveOut(); |
tyleralt | 2:891b3618be4f | 102 | } |
tyleralt | 2:891b3618be4f | 103 | updateMap(); |
tyleralt | 2:891b3618be4f | 104 | break; |
tyleralt | 2:891b3618be4f | 105 | case 's': |
tyleralt | 2:891b3618be4f | 106 | for (int i = 0; i < 3; i ++){ |
tyleralt | 2:891b3618be4f | 107 | blocks[i].moveIn(); |
tyleralt | 2:891b3618be4f | 108 | } |
tyleralt | 2:891b3618be4f | 109 | updateMap(); |
tyleralt | 2:891b3618be4f | 110 | break; |
tyleralt | 2:891b3618be4f | 111 | case 'a': |
tyleralt | 2:891b3618be4f | 112 | for (int i = 0; i < 3; i ++){ |
tyleralt | 2:891b3618be4f | 113 | blocks[i].moveLeft(); |
tyleralt | 2:891b3618be4f | 114 | } |
tyleralt | 2:891b3618be4f | 115 | updateMap(); |
tyleralt | 2:891b3618be4f | 116 | break; |
tyleralt | 2:891b3618be4f | 117 | case 'd': |
tyleralt | 2:891b3618be4f | 118 | for (int i = 0; i < 3; i ++){ |
tyleralt | 2:891b3618be4f | 119 | blocks[i].moveRight(); |
tyleralt | 2:891b3618be4f | 120 | } |
tyleralt | 2:891b3618be4f | 121 | updateMap(); |
tyleralt | 2:891b3618be4f | 122 | break; |
tyleralt | 2:891b3618be4f | 123 | case 'p': |
tyleralt | 2:891b3618be4f | 124 | return; |
tyleralt | 2:891b3618be4f | 125 | case 'l': |
tyleralt | 2:891b3618be4f | 126 | return; |
tyleralt | 2:891b3618be4f | 127 | default: return; |
tyleralt | 2:891b3618be4f | 128 | } |
tyleralt | 0:ded79d89abdf | 129 | } |
tyleralt | 0:ded79d89abdf | 130 | void parseEucMode(char c){ |
tyleralt | 0:ded79d89abdf | 131 | switch(c){ |
tyleralt | 0:ded79d89abdf | 132 | case 'w': |
tyleralt | 0:ded79d89abdf | 133 | eucPointer.moveAway(); |
tyleralt | 0:ded79d89abdf | 134 | break; |
tyleralt | 0:ded79d89abdf | 135 | case 's': |
tyleralt | 0:ded79d89abdf | 136 | eucPointer.moveTowards(); |
tyleralt | 0:ded79d89abdf | 137 | break; |
tyleralt | 0:ded79d89abdf | 138 | case 'a': |
tyleralt | 0:ded79d89abdf | 139 | eucPointer.moveLeft(); |
tyleralt | 0:ded79d89abdf | 140 | break; |
tyleralt | 0:ded79d89abdf | 141 | case 'd': |
tyleralt | 0:ded79d89abdf | 142 | eucPointer.moveRight(); |
tyleralt | 0:ded79d89abdf | 143 | break; |
tyleralt | 0:ded79d89abdf | 144 | case 'p': |
tyleralt | 0:ded79d89abdf | 145 | eucPointer.moveUp(); |
tyleralt | 0:ded79d89abdf | 146 | break; |
tyleralt | 0:ded79d89abdf | 147 | case 'l': |
tyleralt | 0:ded79d89abdf | 148 | eucPointer.moveDown(); |
tyleralt | 0:ded79d89abdf | 149 | break; |
tyleralt | 0:ded79d89abdf | 150 | default: return; |
tyleralt | 0:ded79d89abdf | 151 | } |
tyleralt | 1:bb1507f0bb64 | 152 | Point startPoint = eucPointer.getPoint(); |
tyleralt | 0:ded79d89abdf | 153 | Point endPoint = eucPointer.getEndPoint(); |
tyleralt | 2:891b3618be4f | 154 | //bt.printf("\r\n"); |
tyleralt | 2:891b3618be4f | 155 | //bt.printf("points%i , %i , %X \r\n", eucPointer.x, eucPointer.y,eucPointer.z); |
tyleralt | 2:891b3618be4f | 156 | //bt.printf("drawingEucPointer Starting %i , %i , %X \r\n", startPoint.getArraySlice(), startPoint.getPositionDistance(),startPoint.getIdentifyingChar()); |
tyleralt | 1:bb1507f0bb64 | 157 | //bt.printf("drawingEucPointer Ending %i , %i , %X \r\n", endPoint.getArraySlice(), endPoint.getPositionDistance(),endPoint.getIdentifyingChar()); |
tyleralt | 0:ded79d89abdf | 158 | display.addEucPoint(eucPointer); |
tyleralt | 0:ded79d89abdf | 159 | } |
tyleralt | 0:ded79d89abdf | 160 | |
tyleralt | 0:ded79d89abdf | 161 | void parseBt(char c){ |
tyleralt | 0:ded79d89abdf | 162 | if (c == 'm'){ |
tyleralt | 1:bb1507f0bb64 | 163 | display.resetDisplay(); |
tyleralt | 1:bb1507f0bb64 | 164 | switch (mode){ |
tyleralt | 0:ded79d89abdf | 165 | case(lineDraw):{ |
tyleralt | 0:ded79d89abdf | 166 | mode = eucDraw; |
tyleralt | 0:ded79d89abdf | 167 | bt.printf("we are drawing Euclidian"); |
tyleralt | 0:ded79d89abdf | 168 | break; |
tyleralt | 0:ded79d89abdf | 169 | } |
tyleralt | 0:ded79d89abdf | 170 | case(map):{ |
tyleralt | 0:ded79d89abdf | 171 | mode = lineDraw; |
tyleralt | 0:ded79d89abdf | 172 | bt.printf("we are drawing Radians"); |
tyleralt | 0:ded79d89abdf | 173 | break; |
tyleralt | 0:ded79d89abdf | 174 | } |
tyleralt | 0:ded79d89abdf | 175 | case(eucDraw):{ |
tyleralt | 0:ded79d89abdf | 176 | mode = map; |
tyleralt | 0:ded79d89abdf | 177 | bt.printf("we are on a Map"); |
tyleralt | 2:891b3618be4f | 178 | updateMap(); |
tyleralt | 0:ded79d89abdf | 179 | break; |
tyleralt | 0:ded79d89abdf | 180 | } |
tyleralt | 0:ded79d89abdf | 181 | default: { |
tyleralt | 0:ded79d89abdf | 182 | return; |
tyleralt | 0:ded79d89abdf | 183 | } |
tyleralt | 0:ded79d89abdf | 184 | } |
tyleralt | 0:ded79d89abdf | 185 | return; |
tyleralt | 0:ded79d89abdf | 186 | } |
tyleralt | 0:ded79d89abdf | 187 | |
tyleralt | 0:ded79d89abdf | 188 | if (mode == lineDraw){ |
tyleralt | 0:ded79d89abdf | 189 | parseLineMode(c); |
tyleralt | 0:ded79d89abdf | 190 | } |
tyleralt | 0:ded79d89abdf | 191 | if (mode == map){ |
tyleralt | 0:ded79d89abdf | 192 | parseMapMode(c); |
tyleralt | 0:ded79d89abdf | 193 | } |
tyleralt | 0:ded79d89abdf | 194 | if (mode == eucDraw){ |
tyleralt | 0:ded79d89abdf | 195 | parseEucMode(c); |
tyleralt | 0:ded79d89abdf | 196 | } |
tyleralt | 0:ded79d89abdf | 197 | } |
tyleralt | 0:ded79d89abdf | 198 | |
tyleralt | 0:ded79d89abdf | 199 | |
tyleralt | 0:ded79d89abdf | 200 | |
tyleralt | 0:ded79d89abdf | 201 | int main() { |
tyleralt | 2:891b3618be4f | 202 | blocks[0] = Block( 3, 4, 0, 4,6); |
tyleralt | 2:891b3618be4f | 203 | blocks[1] = Block ( -4, -4, 1 ,4, 3); |
tyleralt | 2:891b3618be4f | 204 | blocks[2] = Block ( 5, -7, 2 ,5, 4); |
tyleralt | 0:ded79d89abdf | 205 | pc.printf("started"); |
tyleralt | 0:ded79d89abdf | 206 | bt.printf("bluetooth started"); |
tyleralt | 0:ded79d89abdf | 207 | mode = eucDraw; |
tyleralt | 0:ded79d89abdf | 208 | firstTime = true; |
tyleralt | 0:ded79d89abdf | 209 | current_slice = 100; |
tyleralt | 0:ded79d89abdf | 210 | display.resetDisplay(); |
tyleralt | 0:ded79d89abdf | 211 | |
tyleralt | 0:ded79d89abdf | 212 | hallSensor.fall(&rotate_sense); |
tyleralt | 0:ded79d89abdf | 213 | |
tyleralt | 0:ded79d89abdf | 214 | while(true) { |
tyleralt | 0:ded79d89abdf | 215 | if (bt.readable()){ |
tyleralt | 0:ded79d89abdf | 216 | parseBt(bt.getc()); |
tyleralt | 0:ded79d89abdf | 217 | } |
tyleralt | 0:ded79d89abdf | 218 | } |
tyleralt | 0:ded79d89abdf | 219 | } |