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@5:d7d16cb9c974, 2015-08-17 (annotated)
- Committer:
- tyleralt
- Date:
- Mon Aug 17 21:56:25 2015 +0000
- Revision:
- 5:d7d16cb9c974
- Parent:
- 2:891b3618be4f
export commit;
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 | 5:d7d16cb9c974 | 3 | #define BUFFER_ 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 | 5:d7d16cb9c974 | 32 | Block blocks [8]; |
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 | 5:d7d16cb9c974 | 45 | display.addBlock(blocks[3]); |
tyleralt | 5:d7d16cb9c974 | 46 | display.addBlock(blocks[4]); |
tyleralt | 5:d7d16cb9c974 | 47 | display.addBlock(blocks[5]); |
tyleralt | 5:d7d16cb9c974 | 48 | display.addBlock(blocks[6]); |
tyleralt | 5:d7d16cb9c974 | 49 | display.addBlock(blocks[7]); |
tyleralt | 2:891b3618be4f | 50 | } |
tyleralt | 2:891b3618be4f | 51 | |
tyleralt | 0:ded79d89abdf | 52 | void rotate_sense(){ |
tyleralt | 0:ded79d89abdf | 53 | if (firstTime){ |
tyleralt | 0:ded79d89abdf | 54 | rotationTime.reset(); |
tyleralt | 0:ded79d89abdf | 55 | rotationTime.start(); |
tyleralt | 0:ded79d89abdf | 56 | firstTime = false; |
tyleralt | 0:ded79d89abdf | 57 | display.current_slice = 180; |
tyleralt | 0:ded79d89abdf | 58 | return; |
tyleralt | 0:ded79d89abdf | 59 | } |
tyleralt | 0:ded79d89abdf | 60 | if(display.current_slice < NUMBER_OF_SLICES / 4){ |
tyleralt | 0:ded79d89abdf | 61 | return; |
tyleralt | 0:ded79d89abdf | 62 | } |
tyleralt | 0:ded79d89abdf | 63 | rotate_time = rotationTime.read_us(); |
tyleralt | 0:ded79d89abdf | 64 | rotationTime.reset(); |
tyleralt | 0:ded79d89abdf | 65 | rotationTime.start(); |
tyleralt | 0:ded79d89abdf | 66 | slice_time = (double) rotate_time/NUMBER_OF_SLICES; |
tyleralt | 0:ded79d89abdf | 67 | display.resetCount(); |
tyleralt | 0:ded79d89abdf | 68 | updateLeds.attach_us(&display, &View::nextLedPush, slice_time); |
tyleralt | 0:ded79d89abdf | 69 | } |
tyleralt | 0:ded79d89abdf | 70 | void moveUp(); |
tyleralt | 0:ded79d89abdf | 71 | void moveDown(); |
tyleralt | 0:ded79d89abdf | 72 | void rotateRight(); |
tyleralt | 0:ded79d89abdf | 73 | void rotateLeft(); |
tyleralt | 0:ded79d89abdf | 74 | void moveIn(); |
tyleralt | 0:ded79d89abdf | 75 | void moveOut(); |
tyleralt | 0:ded79d89abdf | 76 | |
tyleralt | 0:ded79d89abdf | 77 | void parseLineMode(char c){ |
tyleralt | 0:ded79d89abdf | 78 | switch(c){ |
tyleralt | 0:ded79d89abdf | 79 | case 'w': |
tyleralt | 0:ded79d89abdf | 80 | pointer.moveOut(); |
tyleralt | 0:ded79d89abdf | 81 | break; |
tyleralt | 0:ded79d89abdf | 82 | case 's': |
tyleralt | 0:ded79d89abdf | 83 | pointer.moveIn(); |
tyleralt | 0:ded79d89abdf | 84 | break; |
tyleralt | 0:ded79d89abdf | 85 | case 'a': |
tyleralt | 0:ded79d89abdf | 86 | pointer.rotateRight(); |
tyleralt | 0:ded79d89abdf | 87 | break; |
tyleralt | 0:ded79d89abdf | 88 | case 'd': |
tyleralt | 0:ded79d89abdf | 89 | pointer.rotateLeft(); |
tyleralt | 0:ded79d89abdf | 90 | break; |
tyleralt | 0:ded79d89abdf | 91 | case 'p': |
tyleralt | 0:ded79d89abdf | 92 | pointer.moveUp(); |
tyleralt | 0:ded79d89abdf | 93 | break; |
tyleralt | 0:ded79d89abdf | 94 | case 'l': |
tyleralt | 0:ded79d89abdf | 95 | pointer.moveDown(); |
tyleralt | 0:ded79d89abdf | 96 | break; |
tyleralt | 0:ded79d89abdf | 97 | default: return; |
tyleralt | 0:ded79d89abdf | 98 | } |
tyleralt | 0:ded79d89abdf | 99 | display.addPoint(pointer); |
tyleralt | 0:ded79d89abdf | 100 | } |
tyleralt | 0:ded79d89abdf | 101 | |
tyleralt | 0:ded79d89abdf | 102 | void parseMapMode(char c){ |
tyleralt | 2:891b3618be4f | 103 | switch(c){ |
tyleralt | 2:891b3618be4f | 104 | case 'w': |
tyleralt | 5:d7d16cb9c974 | 105 | for (int i = 0; i < 8; i ++){ |
tyleralt | 2:891b3618be4f | 106 | blocks[i].moveOut(); |
tyleralt | 2:891b3618be4f | 107 | } |
tyleralt | 2:891b3618be4f | 108 | updateMap(); |
tyleralt | 2:891b3618be4f | 109 | break; |
tyleralt | 2:891b3618be4f | 110 | case 's': |
tyleralt | 5:d7d16cb9c974 | 111 | for (int i = 0; i < 8; i ++){ |
tyleralt | 2:891b3618be4f | 112 | blocks[i].moveIn(); |
tyleralt | 2:891b3618be4f | 113 | } |
tyleralt | 2:891b3618be4f | 114 | updateMap(); |
tyleralt | 2:891b3618be4f | 115 | break; |
tyleralt | 2:891b3618be4f | 116 | case 'a': |
tyleralt | 5:d7d16cb9c974 | 117 | for (int i = 0; i < 8; i ++){ |
tyleralt | 2:891b3618be4f | 118 | blocks[i].moveLeft(); |
tyleralt | 2:891b3618be4f | 119 | } |
tyleralt | 2:891b3618be4f | 120 | updateMap(); |
tyleralt | 2:891b3618be4f | 121 | break; |
tyleralt | 2:891b3618be4f | 122 | case 'd': |
tyleralt | 5:d7d16cb9c974 | 123 | for (int i = 0; i < 8; i ++){ |
tyleralt | 2:891b3618be4f | 124 | blocks[i].moveRight(); |
tyleralt | 2:891b3618be4f | 125 | } |
tyleralt | 2:891b3618be4f | 126 | updateMap(); |
tyleralt | 2:891b3618be4f | 127 | break; |
tyleralt | 2:891b3618be4f | 128 | case 'p': |
tyleralt | 2:891b3618be4f | 129 | return; |
tyleralt | 2:891b3618be4f | 130 | case 'l': |
tyleralt | 2:891b3618be4f | 131 | return; |
tyleralt | 2:891b3618be4f | 132 | default: return; |
tyleralt | 2:891b3618be4f | 133 | } |
tyleralt | 0:ded79d89abdf | 134 | } |
tyleralt | 0:ded79d89abdf | 135 | void parseEucMode(char c){ |
tyleralt | 0:ded79d89abdf | 136 | switch(c){ |
tyleralt | 0:ded79d89abdf | 137 | case 'w': |
tyleralt | 0:ded79d89abdf | 138 | eucPointer.moveAway(); |
tyleralt | 0:ded79d89abdf | 139 | break; |
tyleralt | 0:ded79d89abdf | 140 | case 's': |
tyleralt | 0:ded79d89abdf | 141 | eucPointer.moveTowards(); |
tyleralt | 0:ded79d89abdf | 142 | break; |
tyleralt | 0:ded79d89abdf | 143 | case 'a': |
tyleralt | 0:ded79d89abdf | 144 | eucPointer.moveLeft(); |
tyleralt | 0:ded79d89abdf | 145 | break; |
tyleralt | 0:ded79d89abdf | 146 | case 'd': |
tyleralt | 0:ded79d89abdf | 147 | eucPointer.moveRight(); |
tyleralt | 0:ded79d89abdf | 148 | break; |
tyleralt | 0:ded79d89abdf | 149 | case 'p': |
tyleralt | 0:ded79d89abdf | 150 | eucPointer.moveUp(); |
tyleralt | 0:ded79d89abdf | 151 | break; |
tyleralt | 0:ded79d89abdf | 152 | case 'l': |
tyleralt | 0:ded79d89abdf | 153 | eucPointer.moveDown(); |
tyleralt | 0:ded79d89abdf | 154 | break; |
tyleralt | 0:ded79d89abdf | 155 | default: return; |
tyleralt | 0:ded79d89abdf | 156 | } |
tyleralt | 5:d7d16cb9c974 | 157 | display.addFilledEucPoint(eucPointer); |
tyleralt | 0:ded79d89abdf | 158 | } |
tyleralt | 0:ded79d89abdf | 159 | |
tyleralt | 0:ded79d89abdf | 160 | void parseBt(char c){ |
tyleralt | 0:ded79d89abdf | 161 | if (c == 'm'){ |
tyleralt | 1:bb1507f0bb64 | 162 | display.resetDisplay(); |
tyleralt | 1:bb1507f0bb64 | 163 | switch (mode){ |
tyleralt | 0:ded79d89abdf | 164 | case(lineDraw):{ |
tyleralt | 0:ded79d89abdf | 165 | mode = eucDraw; |
tyleralt | 0:ded79d89abdf | 166 | bt.printf("we are drawing Euclidian"); |
tyleralt | 0:ded79d89abdf | 167 | break; |
tyleralt | 0:ded79d89abdf | 168 | } |
tyleralt | 0:ded79d89abdf | 169 | case(map):{ |
tyleralt | 0:ded79d89abdf | 170 | mode = lineDraw; |
tyleralt | 0:ded79d89abdf | 171 | bt.printf("we are drawing Radians"); |
tyleralt | 0:ded79d89abdf | 172 | break; |
tyleralt | 0:ded79d89abdf | 173 | } |
tyleralt | 0:ded79d89abdf | 174 | case(eucDraw):{ |
tyleralt | 0:ded79d89abdf | 175 | mode = map; |
tyleralt | 0:ded79d89abdf | 176 | bt.printf("we are on a Map"); |
tyleralt | 2:891b3618be4f | 177 | updateMap(); |
tyleralt | 0:ded79d89abdf | 178 | break; |
tyleralt | 0:ded79d89abdf | 179 | } |
tyleralt | 0:ded79d89abdf | 180 | default: { |
tyleralt | 0:ded79d89abdf | 181 | return; |
tyleralt | 0:ded79d89abdf | 182 | } |
tyleralt | 0:ded79d89abdf | 183 | } |
tyleralt | 0:ded79d89abdf | 184 | return; |
tyleralt | 0:ded79d89abdf | 185 | } |
tyleralt | 0:ded79d89abdf | 186 | |
tyleralt | 0:ded79d89abdf | 187 | if (mode == lineDraw){ |
tyleralt | 0:ded79d89abdf | 188 | parseLineMode(c); |
tyleralt | 0:ded79d89abdf | 189 | } |
tyleralt | 0:ded79d89abdf | 190 | if (mode == map){ |
tyleralt | 0:ded79d89abdf | 191 | parseMapMode(c); |
tyleralt | 0:ded79d89abdf | 192 | } |
tyleralt | 0:ded79d89abdf | 193 | if (mode == eucDraw){ |
tyleralt | 0:ded79d89abdf | 194 | parseEucMode(c); |
tyleralt | 0:ded79d89abdf | 195 | } |
tyleralt | 0:ded79d89abdf | 196 | } |
tyleralt | 0:ded79d89abdf | 197 | |
tyleralt | 0:ded79d89abdf | 198 | |
tyleralt | 0:ded79d89abdf | 199 | |
tyleralt | 0:ded79d89abdf | 200 | int main() { |
tyleralt | 2:891b3618be4f | 201 | blocks[0] = Block( 3, 4, 0, 4,6); |
tyleralt | 2:891b3618be4f | 202 | blocks[1] = Block ( -4, -4, 1 ,4, 3); |
tyleralt | 2:891b3618be4f | 203 | blocks[2] = Block ( 5, -7, 2 ,5, 4); |
tyleralt | 5:d7d16cb9c974 | 204 | blocks[3] = Block (-5, 5, 0, 3, 7); |
tyleralt | 5:d7d16cb9c974 | 205 | blocks[4] = Block( 15, 8, 1, 4,6); |
tyleralt | 5:d7d16cb9c974 | 206 | blocks[5] = Block ( 15, -4, 0 ,5, 7); |
tyleralt | 5:d7d16cb9c974 | 207 | blocks[6] = Block ( 22, -7, 2 ,6, 4); |
tyleralt | 5:d7d16cb9c974 | 208 | blocks[7] = Block (21, 5, 2, 4, 4); |
tyleralt | 5:d7d16cb9c974 | 209 | |
tyleralt | 0:ded79d89abdf | 210 | pc.printf("started"); |
tyleralt | 0:ded79d89abdf | 211 | bt.printf("bluetooth started"); |
tyleralt | 5:d7d16cb9c974 | 212 | mode = map; |
tyleralt | 0:ded79d89abdf | 213 | firstTime = true; |
tyleralt | 0:ded79d89abdf | 214 | current_slice = 100; |
tyleralt | 0:ded79d89abdf | 215 | display.resetDisplay(); |
tyleralt | 0:ded79d89abdf | 216 | |
tyleralt | 0:ded79d89abdf | 217 | hallSensor.fall(&rotate_sense); |
tyleralt | 0:ded79d89abdf | 218 | |
tyleralt | 0:ded79d89abdf | 219 | while(true) { |
tyleralt | 0:ded79d89abdf | 220 | if (bt.readable()){ |
tyleralt | 0:ded79d89abdf | 221 | parseBt(bt.getc()); |
tyleralt | 0:ded79d89abdf | 222 | } |
tyleralt | 0:ded79d89abdf | 223 | } |
tyleralt | 0:ded79d89abdf | 224 | } |