![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
1D-Pong game based on a LED strip with 150 LPD6803-controlled pixels. Game keeps score for 'best-of-21' game. Written for KL25Z
main.cpp@7:e605dbca7330, 2013-08-21 (annotated)
- Committer:
- vsluiter
- Date:
- Wed Aug 21 21:10:38 2013 +0000
- Revision:
- 7:e605dbca7330
- Parent:
- 6:c2040b4cf589
- Child:
- 8:f46db1ff5ec9
Paddle now updates its own position;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
vsluiter | 0:cde34c55fa20 | 1 | #include "mbed.h" |
vsluiter | 0:cde34c55fa20 | 2 | |
vsluiter | 0:cde34c55fa20 | 3 | #define NUMBER_OF_PIXELS 50 |
vsluiter | 0:cde34c55fa20 | 4 | #define PADDLE_LENGTH 5 |
vsluiter | 2:d2f8772a8db5 | 5 | #define LEFT false |
vsluiter | 2:d2f8772a8db5 | 6 | #define RIGHT true |
vsluiter | 5:d2e819018807 | 7 | |
vsluiter | 5:d2e819018807 | 8 | #define MAX_PADDLE_SIZE 10 |
vsluiter | 0:cde34c55fa20 | 9 | void Randomblinks(float seconds, bool colored = false); |
vsluiter | 0:cde34c55fa20 | 10 | void PaddleDemo(float seconds, uint8_t red, uint8_t green, uint8_t blue); |
vsluiter | 1:7a2ec350cdf5 | 11 | void WinLoose(float seconds, bool side); |
vsluiter | 5:d2e819018807 | 12 | void UpdateDemoPaddle(void); |
vsluiter | 2:d2f8772a8db5 | 13 | void Score(uint8_t left, uint8_t right); |
vsluiter | 5:d2e819018807 | 14 | void DrawGamePaddle(void); |
vsluiter | 5:d2e819018807 | 15 | |
vsluiter | 0:cde34c55fa20 | 16 | uint16_t totalstrip[NUMBER_OF_PIXELS]; |
vsluiter | 0:cde34c55fa20 | 17 | volatile int8_t paddlestart= 0; |
vsluiter | 0:cde34c55fa20 | 18 | SPI ledstrip(PTD2,NC,PTD1); |
vsluiter | 0:cde34c55fa20 | 19 | |
vsluiter | 5:d2e819018807 | 20 | class Paddle |
vsluiter | 5:d2e819018807 | 21 | { |
vsluiter | 5:d2e819018807 | 22 | public: |
vsluiter | 5:d2e819018807 | 23 | Paddle();//constructor |
vsluiter | 5:d2e819018807 | 24 | // ~Paddle();//deconstructor |
vsluiter | 5:d2e819018807 | 25 | int8_t position; |
vsluiter | 5:d2e819018807 | 26 | uint8_t direction; |
vsluiter | 7:e605dbca7330 | 27 | void setSpeed(float speed); |
vsluiter | 5:d2e819018807 | 28 | void setColor(uint8_t red, uint8_t green, uint8_t blue); |
vsluiter | 5:d2e819018807 | 29 | uint8_t getSize(void); |
vsluiter | 5:d2e819018807 | 30 | void setSize(uint8_t size); |
vsluiter | 5:d2e819018807 | 31 | uint8_t getColor(uint8_t pixel, uint8_t color); |
vsluiter | 5:d2e819018807 | 32 | private: |
vsluiter | 5:d2e819018807 | 33 | uint8_t m_red ,m_green ,m_blue; |
vsluiter | 5:d2e819018807 | 34 | uint8_t m_size; |
vsluiter | 5:d2e819018807 | 35 | uint8_t m_paddle[MAX_PADDLE_SIZE][3]; |
vsluiter | 7:e605dbca7330 | 36 | float m_speed; |
vsluiter | 7:e605dbca7330 | 37 | Ticker UpdatePosition; |
vsluiter | 7:e605dbca7330 | 38 | void PositionUpdater(void); |
vsluiter | 5:d2e819018807 | 39 | }; |
vsluiter | 5:d2e819018807 | 40 | |
vsluiter | 5:d2e819018807 | 41 | Paddle::Paddle() |
vsluiter | 5:d2e819018807 | 42 | { |
vsluiter | 5:d2e819018807 | 43 | setColor(255,255,255); |
vsluiter | 5:d2e819018807 | 44 | setSize(4); |
vsluiter | 5:d2e819018807 | 45 | position = -getSize(); |
vsluiter | 5:d2e819018807 | 46 | direction = 1; |
vsluiter | 7:e605dbca7330 | 47 | setSpeed(50); |
vsluiter | 7:e605dbca7330 | 48 | } |
vsluiter | 7:e605dbca7330 | 49 | |
vsluiter | 7:e605dbca7330 | 50 | void Paddle::setSpeed(float speed) |
vsluiter | 7:e605dbca7330 | 51 | { |
vsluiter | 7:e605dbca7330 | 52 | //truncate |
vsluiter | 7:e605dbca7330 | 53 | float time; |
vsluiter | 7:e605dbca7330 | 54 | if(speed > 100000) |
vsluiter | 7:e605dbca7330 | 55 | speed = 100000; |
vsluiter | 7:e605dbca7330 | 56 | if(speed <= 0) |
vsluiter | 7:e605dbca7330 | 57 | speed = 0.001; |
vsluiter | 7:e605dbca7330 | 58 | m_speed = speed; |
vsluiter | 7:e605dbca7330 | 59 | time = 1/m_speed; |
vsluiter | 7:e605dbca7330 | 60 | UpdatePosition.attach(this,&Paddle::PositionUpdater, time); |
vsluiter | 7:e605dbca7330 | 61 | } |
vsluiter | 7:e605dbca7330 | 62 | |
vsluiter | 7:e605dbca7330 | 63 | void Paddle::PositionUpdater(void) |
vsluiter | 7:e605dbca7330 | 64 | { |
vsluiter | 7:e605dbca7330 | 65 | if(direction == 1) |
vsluiter | 7:e605dbca7330 | 66 | position++; |
vsluiter | 7:e605dbca7330 | 67 | else |
vsluiter | 7:e605dbca7330 | 68 | position--; |
vsluiter | 5:d2e819018807 | 69 | } |
vsluiter | 5:d2e819018807 | 70 | |
vsluiter | 5:d2e819018807 | 71 | uint8_t Paddle::getColor(uint8_t pixel, uint8_t color) |
vsluiter | 5:d2e819018807 | 72 | { |
vsluiter | 5:d2e819018807 | 73 | if(pixel<MAX_PADDLE_SIZE && color < 3) |
vsluiter | 5:d2e819018807 | 74 | return m_paddle[pixel][color]; |
vsluiter | 5:d2e819018807 | 75 | else |
vsluiter | 5:d2e819018807 | 76 | return 0; |
vsluiter | 5:d2e819018807 | 77 | } |
vsluiter | 5:d2e819018807 | 78 | |
vsluiter | 5:d2e819018807 | 79 | uint8_t Paddle::getSize(void) |
vsluiter | 5:d2e819018807 | 80 | { |
vsluiter | 5:d2e819018807 | 81 | return m_size; |
vsluiter | 5:d2e819018807 | 82 | } |
vsluiter | 5:d2e819018807 | 83 | |
vsluiter | 5:d2e819018807 | 84 | void Paddle::setSize(uint8_t size) |
vsluiter | 5:d2e819018807 | 85 | { |
vsluiter | 5:d2e819018807 | 86 | if(size > MAX_PADDLE_SIZE) |
vsluiter | 5:d2e819018807 | 87 | size = MAX_PADDLE_SIZE; |
vsluiter | 5:d2e819018807 | 88 | m_size = size; |
vsluiter | 5:d2e819018807 | 89 | setColor(m_red, m_green, m_blue); |
vsluiter | 5:d2e819018807 | 90 | } |
vsluiter | 5:d2e819018807 | 91 | |
vsluiter | 5:d2e819018807 | 92 | void Paddle::setColor(uint8_t red, uint8_t green, uint8_t blue) |
vsluiter | 5:d2e819018807 | 93 | { |
vsluiter | 5:d2e819018807 | 94 | uint8_t paddlepixel; |
vsluiter | 5:d2e819018807 | 95 | m_red = red; |
vsluiter | 5:d2e819018807 | 96 | m_green = green; |
vsluiter | 5:d2e819018807 | 97 | m_blue = blue; |
vsluiter | 5:d2e819018807 | 98 | for(paddlepixel = 0 ; paddlepixel < MAX_PADDLE_SIZE ; paddlepixel++) |
vsluiter | 5:d2e819018807 | 99 | { |
vsluiter | 5:d2e819018807 | 100 | float factor; |
vsluiter | 5:d2e819018807 | 101 | factor = 1.0*paddlepixel/(m_size*1.0); |
vsluiter | 5:d2e819018807 | 102 | if (factor > 1) |
vsluiter | 5:d2e819018807 | 103 | factor = 1; |
vsluiter | 5:d2e819018807 | 104 | factor = factor*factor*factor;// make the effect more dramatic |
vsluiter | 5:d2e819018807 | 105 | m_paddle[paddlepixel][0] = (float)m_red * factor; |
vsluiter | 5:d2e819018807 | 106 | m_paddle[paddlepixel][1] = (float)m_green * factor; |
vsluiter | 5:d2e819018807 | 107 | m_paddle[paddlepixel][2] = (float)m_blue * factor; |
vsluiter | 5:d2e819018807 | 108 | } |
vsluiter | 5:d2e819018807 | 109 | } |
vsluiter | 0:cde34c55fa20 | 110 | |
vsluiter | 0:cde34c55fa20 | 111 | void UpdateLEDstrip(void) |
vsluiter | 0:cde34c55fa20 | 112 | { |
vsluiter | 0:cde34c55fa20 | 113 | uint8_t pixelcounter; |
vsluiter | 0:cde34c55fa20 | 114 | /*start by writing 32 zeroes */ |
vsluiter | 0:cde34c55fa20 | 115 | ledstrip.write(0); |
vsluiter | 0:cde34c55fa20 | 116 | ledstrip.write(0); |
vsluiter | 0:cde34c55fa20 | 117 | ledstrip.write(0); |
vsluiter | 0:cde34c55fa20 | 118 | ledstrip.write(0); |
vsluiter | 0:cde34c55fa20 | 119 | for(pixelcounter = 0 ; pixelcounter < NUMBER_OF_PIXELS; pixelcounter++) { |
vsluiter | 0:cde34c55fa20 | 120 | ledstrip.write( uint8_t(totalstrip[pixelcounter]>>8));//uint8_t(temp16));//(totalstrip.ledcounter[pixelcounter].red << 2) | (totalstrip.ledcounter[pixelcounter].high << 7) |(totalstrip.ledcounter[pixelcounter].green & 0x << 2) ); |
vsluiter | 0:cde34c55fa20 | 121 | ledstrip.write( uint8_t(totalstrip[pixelcounter]));//(*(uint16_t *)(&totalstrip[pixelcounter]))>>8); |
vsluiter | 0:cde34c55fa20 | 122 | } |
vsluiter | 0:cde34c55fa20 | 123 | } |
vsluiter | 0:cde34c55fa20 | 124 | |
vsluiter | 0:cde34c55fa20 | 125 | void write_led(uint16_t * led, uint8_t red, uint8_t green, uint8_t blue) |
vsluiter | 0:cde34c55fa20 | 126 | { |
vsluiter | 0:cde34c55fa20 | 127 | *led = (1<<15) | ((green >> 3)<<10) | ((red >>3)<< 5) | (blue >>3); |
vsluiter | 0:cde34c55fa20 | 128 | } |
vsluiter | 5:d2e819018807 | 129 | |
vsluiter | 5:d2e819018807 | 130 | Paddle paddle; |
vsluiter | 6:c2040b4cf589 | 131 | bool leftpushed = false, rightpushed = false; |
vsluiter | 6:c2040b4cf589 | 132 | void left_pushed(void) |
vsluiter | 6:c2040b4cf589 | 133 | { |
vsluiter | 6:c2040b4cf589 | 134 | if(paddle.direction == 1) |
vsluiter | 6:c2040b4cf589 | 135 | leftpushed = true; |
vsluiter | 6:c2040b4cf589 | 136 | } |
vsluiter | 6:c2040b4cf589 | 137 | |
vsluiter | 6:c2040b4cf589 | 138 | void right_pushed(void) |
vsluiter | 6:c2040b4cf589 | 139 | { |
vsluiter | 6:c2040b4cf589 | 140 | if(paddle.direction != 1) |
vsluiter | 6:c2040b4cf589 | 141 | rightpushed = true; |
vsluiter | 6:c2040b4cf589 | 142 | } |
vsluiter | 5:d2e819018807 | 143 | |
vsluiter | 0:cde34c55fa20 | 144 | int main() |
vsluiter | 0:cde34c55fa20 | 145 | { |
vsluiter | 0:cde34c55fa20 | 146 | Ticker updater; |
vsluiter | 5:d2e819018807 | 147 | Ticker demopaddlepos; |
vsluiter | 6:c2040b4cf589 | 148 | InterruptIn buttonleft(PTD5); |
vsluiter | 6:c2040b4cf589 | 149 | InterruptIn buttonright(PTD0); |
vsluiter | 0:cde34c55fa20 | 150 | uint8_t ledcounter; |
vsluiter | 6:c2040b4cf589 | 151 | buttonleft.mode(PullUp); |
vsluiter | 6:c2040b4cf589 | 152 | buttonright.mode(PullUp); |
vsluiter | 6:c2040b4cf589 | 153 | buttonleft.fall(left_pushed); |
vsluiter | 6:c2040b4cf589 | 154 | buttonright.fall(right_pushed); |
vsluiter | 4:119537f0ff8e | 155 | updater.attach(UpdateLEDstrip, .02); |
vsluiter | 5:d2e819018807 | 156 | demopaddlepos.attach(UpdateDemoPaddle, .03); |
vsluiter | 0:cde34c55fa20 | 157 | ledstrip.format(8,0); //15 bits, mode '0' |
vsluiter | 4:119537f0ff8e | 158 | ledstrip.frequency(1000000); |
vsluiter | 4:119537f0ff8e | 159 | for(ledcounter = 0; ledcounter < NUMBER_OF_PIXELS; ledcounter++) {//turn off leds |
vsluiter | 0:cde34c55fa20 | 160 | write_led(&totalstrip[ledcounter], 0,0,0); |
vsluiter | 0:cde34c55fa20 | 161 | } |
vsluiter | 5:d2e819018807 | 162 | paddle.setSize(6); |
vsluiter | 5:d2e819018807 | 163 | paddle.setColor(255,0,255); |
vsluiter | 0:cde34c55fa20 | 164 | while(1) { |
vsluiter | 2:d2f8772a8db5 | 165 | //PaddleDemo(2,255,10,100); |
vsluiter | 5:d2e819018807 | 166 | DrawGamePaddle(); |
vsluiter | 7:e605dbca7330 | 167 | //wait(.05); |
vsluiter | 5:d2e819018807 | 168 | //Score(3,2); |
vsluiter | 5:d2e819018807 | 169 | //WinLoose(2.5, LEFT); |
vsluiter | 5:d2e819018807 | 170 | if(paddle.direction == 1) |
vsluiter | 6:c2040b4cf589 | 171 | { |
vsluiter | 6:c2040b4cf589 | 172 | if(leftpushed) |
vsluiter | 6:c2040b4cf589 | 173 | { |
vsluiter | 6:c2040b4cf589 | 174 | leftpushed = false; |
vsluiter | 6:c2040b4cf589 | 175 | paddle.direction = 0; |
vsluiter | 6:c2040b4cf589 | 176 | } |
vsluiter | 6:c2040b4cf589 | 177 | } |
vsluiter | 5:d2e819018807 | 178 | else |
vsluiter | 6:c2040b4cf589 | 179 | { |
vsluiter | 6:c2040b4cf589 | 180 | if(rightpushed) |
vsluiter | 6:c2040b4cf589 | 181 | { |
vsluiter | 6:c2040b4cf589 | 182 | rightpushed = false; |
vsluiter | 6:c2040b4cf589 | 183 | paddle.direction = 1; |
vsluiter | 6:c2040b4cf589 | 184 | } |
vsluiter | 6:c2040b4cf589 | 185 | } |
vsluiter | 5:d2e819018807 | 186 | if(paddle.position == NUMBER_OF_PIXELS) |
vsluiter | 5:d2e819018807 | 187 | paddle.direction = 0; |
vsluiter | 5:d2e819018807 | 188 | if(paddle.position == -7) |
vsluiter | 5:d2e819018807 | 189 | paddle.direction = 1; |
vsluiter | 5:d2e819018807 | 190 | // paddle.position = -paddle.getSize(); |
vsluiter | 2:d2f8772a8db5 | 191 | //Randomblinks(5, true); |
vsluiter | 2:d2f8772a8db5 | 192 | //WinLoose(3, false); |
vsluiter | 0:cde34c55fa20 | 193 | } |
vsluiter | 0:cde34c55fa20 | 194 | } |
vsluiter | 0:cde34c55fa20 | 195 | |
vsluiter | 5:d2e819018807 | 196 | void DrawGamePaddle(void) |
vsluiter | 5:d2e819018807 | 197 | { |
vsluiter | 5:d2e819018807 | 198 | uint8_t ledcounter; |
vsluiter | 5:d2e819018807 | 199 | for(ledcounter = 0; ledcounter< NUMBER_OF_PIXELS; ledcounter++) |
vsluiter | 5:d2e819018807 | 200 | { |
vsluiter | 5:d2e819018807 | 201 | if(ledcounter >= paddle.position && ledcounter <= paddle.position+paddle.getSize()) |
vsluiter | 5:d2e819018807 | 202 | { |
vsluiter | 5:d2e819018807 | 203 | uint8_t colorpos; |
vsluiter | 5:d2e819018807 | 204 | if(paddle.direction ==1) |
vsluiter | 5:d2e819018807 | 205 | { |
vsluiter | 5:d2e819018807 | 206 | colorpos = ledcounter-paddle.position; |
vsluiter | 5:d2e819018807 | 207 | write_led(&totalstrip[ledcounter],paddle.getColor(colorpos,0),paddle.getColor(colorpos,1),paddle.getColor(colorpos,2)); |
vsluiter | 5:d2e819018807 | 208 | } |
vsluiter | 5:d2e819018807 | 209 | else |
vsluiter | 5:d2e819018807 | 210 | { |
vsluiter | 5:d2e819018807 | 211 | colorpos = paddle.getSize()-(ledcounter-paddle.position); |
vsluiter | 5:d2e819018807 | 212 | write_led(&totalstrip[ledcounter],paddle.getColor(colorpos,0),paddle.getColor(colorpos,1),paddle.getColor(colorpos,2)); |
vsluiter | 5:d2e819018807 | 213 | } |
vsluiter | 5:d2e819018807 | 214 | } |
vsluiter | 5:d2e819018807 | 215 | else |
vsluiter | 5:d2e819018807 | 216 | write_led(&totalstrip[ledcounter], 0,0,0); |
vsluiter | 5:d2e819018807 | 217 | } |
vsluiter | 5:d2e819018807 | 218 | } |
vsluiter | 5:d2e819018807 | 219 | |
vsluiter | 2:d2f8772a8db5 | 220 | void Score(uint8_t left, uint8_t right) |
vsluiter | 2:d2f8772a8db5 | 221 | { |
vsluiter | 2:d2f8772a8db5 | 222 | uint8_t maxscore; |
vsluiter | 2:d2f8772a8db5 | 223 | int8_t ledcounter; |
vsluiter | 2:d2f8772a8db5 | 224 | uint8_t scorecounter; |
vsluiter | 3:a2b58da5c03b | 225 | typedef struct ledcolor |
vsluiter | 3:a2b58da5c03b | 226 | { |
vsluiter | 3:a2b58da5c03b | 227 | uint8_t red; |
vsluiter | 3:a2b58da5c03b | 228 | uint8_t green; |
vsluiter | 3:a2b58da5c03b | 229 | uint8_t blue; |
vsluiter | 3:a2b58da5c03b | 230 | }ledcolor_t; |
vsluiter | 3:a2b58da5c03b | 231 | ledcolor_t rightled={0,0,0}, leftled={0,0,0}; |
vsluiter | 3:a2b58da5c03b | 232 | if(left >= right) |
vsluiter | 3:a2b58da5c03b | 233 | { |
vsluiter | 3:a2b58da5c03b | 234 | maxscore = left; |
vsluiter | 3:a2b58da5c03b | 235 | leftled.green = 255; |
vsluiter | 3:a2b58da5c03b | 236 | rightled.red = 255; |
vsluiter | 3:a2b58da5c03b | 237 | } |
vsluiter | 3:a2b58da5c03b | 238 | if(right >= left) |
vsluiter | 3:a2b58da5c03b | 239 | { |
vsluiter | 3:a2b58da5c03b | 240 | maxscore = right; |
vsluiter | 3:a2b58da5c03b | 241 | leftled.red = 255; |
vsluiter | 3:a2b58da5c03b | 242 | rightled.green = 255; |
vsluiter | 3:a2b58da5c03b | 243 | } |
vsluiter | 2:d2f8772a8db5 | 244 | for(scorecounter = 0 ; scorecounter <= maxscore ; scorecounter++) |
vsluiter | 2:d2f8772a8db5 | 245 | { |
vsluiter | 2:d2f8772a8db5 | 246 | uint8_t templeft,tempright; |
vsluiter | 2:d2f8772a8db5 | 247 | templeft = left>scorecounter?scorecounter:left; |
vsluiter | 2:d2f8772a8db5 | 248 | tempright = right>scorecounter?scorecounter:right; |
vsluiter | 2:d2f8772a8db5 | 249 | uint8_t sidecounter; |
vsluiter | 2:d2f8772a8db5 | 250 | for(ledcounter = 0; ledcounter < NUMBER_OF_PIXELS; ledcounter++) |
vsluiter | 2:d2f8772a8db5 | 251 | { |
vsluiter | 2:d2f8772a8db5 | 252 | write_led(&totalstrip[ledcounter], 0, 0 ,0); |
vsluiter | 2:d2f8772a8db5 | 253 | } |
vsluiter | 2:d2f8772a8db5 | 254 | for(sidecounter = 0 ; sidecounter < templeft; sidecounter++) |
vsluiter | 2:d2f8772a8db5 | 255 | { |
vsluiter | 3:a2b58da5c03b | 256 | write_led(&totalstrip[sidecounter*2],leftled.red,leftled.green,leftled.blue); |
vsluiter | 2:d2f8772a8db5 | 257 | } |
vsluiter | 2:d2f8772a8db5 | 258 | for(sidecounter = 0 ; sidecounter < tempright ; sidecounter++) |
vsluiter | 2:d2f8772a8db5 | 259 | { |
vsluiter | 3:a2b58da5c03b | 260 | write_led(&totalstrip[(NUMBER_OF_PIXELS-1)-(sidecounter*2)],rightled.red,rightled.green,rightled.blue); |
vsluiter | 2:d2f8772a8db5 | 261 | } |
vsluiter | 3:a2b58da5c03b | 262 | wait(0.7); |
vsluiter | 2:d2f8772a8db5 | 263 | } |
vsluiter | 3:a2b58da5c03b | 264 | wait(0.5); |
vsluiter | 2:d2f8772a8db5 | 265 | } |
vsluiter | 2:d2f8772a8db5 | 266 | |
vsluiter | 1:7a2ec350cdf5 | 267 | void WinLoose(float seconds, bool side) |
vsluiter | 1:7a2ec350cdf5 | 268 | { |
vsluiter | 1:7a2ec350cdf5 | 269 | uint8_t ledcounter; |
vsluiter | 1:7a2ec350cdf5 | 270 | Timer timer; |
vsluiter | 1:7a2ec350cdf5 | 271 | timer.start(); |
vsluiter | 1:7a2ec350cdf5 | 272 | while( timer.read() < seconds) |
vsluiter | 1:7a2ec350cdf5 | 273 | { |
vsluiter | 1:7a2ec350cdf5 | 274 | uint8_t redvalue = 255-(255.0*(timer.read()/(seconds/2))); |
vsluiter | 1:7a2ec350cdf5 | 275 | for(ledcounter = 0; ledcounter < NUMBER_OF_PIXELS; ledcounter++) |
vsluiter | 1:7a2ec350cdf5 | 276 | { |
vsluiter | 1:7a2ec350cdf5 | 277 | if(ledcounter < NUMBER_OF_PIXELS / 2) |
vsluiter | 1:7a2ec350cdf5 | 278 | { |
vsluiter | 1:7a2ec350cdf5 | 279 | if(side) |
vsluiter | 1:7a2ec350cdf5 | 280 | write_led(&totalstrip[ledcounter], redvalue,0,0); |
vsluiter | 1:7a2ec350cdf5 | 281 | else |
vsluiter | 1:7a2ec350cdf5 | 282 | write_led(&totalstrip[ledcounter], 0,255,0); |
vsluiter | 1:7a2ec350cdf5 | 283 | } |
vsluiter | 1:7a2ec350cdf5 | 284 | else |
vsluiter | 1:7a2ec350cdf5 | 285 | { |
vsluiter | 1:7a2ec350cdf5 | 286 | if(side) |
vsluiter | 1:7a2ec350cdf5 | 287 | write_led(&totalstrip[ledcounter], 0,255,0); |
vsluiter | 1:7a2ec350cdf5 | 288 | else |
vsluiter | 1:7a2ec350cdf5 | 289 | write_led(&totalstrip[ledcounter], redvalue ,0,0); |
vsluiter | 1:7a2ec350cdf5 | 290 | |
vsluiter | 1:7a2ec350cdf5 | 291 | } |
vsluiter | 1:7a2ec350cdf5 | 292 | } |
vsluiter | 1:7a2ec350cdf5 | 293 | } |
vsluiter | 1:7a2ec350cdf5 | 294 | } |
vsluiter | 0:cde34c55fa20 | 295 | void PaddleDemo(float seconds, uint8_t red, uint8_t green, uint8_t blue) |
vsluiter | 0:cde34c55fa20 | 296 | { |
vsluiter | 0:cde34c55fa20 | 297 | uint8_t ledcounter; |
vsluiter | 0:cde34c55fa20 | 298 | Timer timer; |
vsluiter | 0:cde34c55fa20 | 299 | timer.start(); |
vsluiter | 0:cde34c55fa20 | 300 | while( timer.read() < seconds) |
vsluiter | 0:cde34c55fa20 | 301 | { |
vsluiter | 0:cde34c55fa20 | 302 | for(ledcounter = 0; ledcounter < NUMBER_OF_PIXELS; ledcounter++) |
vsluiter | 0:cde34c55fa20 | 303 | { |
vsluiter | 0:cde34c55fa20 | 304 | if((ledcounter >= paddlestart) && ( ledcounter <= paddlestart+PADDLE_LENGTH)) |
vsluiter | 0:cde34c55fa20 | 305 | write_led(&totalstrip[ledcounter], red,green,blue); |
vsluiter | 0:cde34c55fa20 | 306 | else |
vsluiter | 0:cde34c55fa20 | 307 | write_led(&totalstrip[ledcounter], 0,0,0); |
vsluiter | 0:cde34c55fa20 | 308 | } |
vsluiter | 0:cde34c55fa20 | 309 | } |
vsluiter | 0:cde34c55fa20 | 310 | } |
vsluiter | 0:cde34c55fa20 | 311 | |
vsluiter | 0:cde34c55fa20 | 312 | void Randomblinks(float seconds, bool colored) |
vsluiter | 0:cde34c55fa20 | 313 | { |
vsluiter | 0:cde34c55fa20 | 314 | uint8_t ledcounter; |
vsluiter | 0:cde34c55fa20 | 315 | uint8_t test; |
vsluiter | 0:cde34c55fa20 | 316 | Timer timer; |
vsluiter | 0:cde34c55fa20 | 317 | timer.start(); |
vsluiter | 0:cde34c55fa20 | 318 | while( timer.read() < seconds ) |
vsluiter | 0:cde34c55fa20 | 319 | { |
vsluiter | 0:cde34c55fa20 | 320 | test = 50.0*rand()/(RAND_MAX*1.0); |
vsluiter | 0:cde34c55fa20 | 321 | for(ledcounter = 0; ledcounter < NUMBER_OF_PIXELS; ledcounter++) |
vsluiter | 0:cde34c55fa20 | 322 | { |
vsluiter | 0:cde34c55fa20 | 323 | if(ledcounter == test) |
vsluiter | 0:cde34c55fa20 | 324 | { |
vsluiter | 0:cde34c55fa20 | 325 | if(colored) |
vsluiter | 0:cde34c55fa20 | 326 | write_led(&totalstrip[ledcounter], test*5,(test%10)*25,(test%15)*15); |
vsluiter | 0:cde34c55fa20 | 327 | else |
vsluiter | 0:cde34c55fa20 | 328 | write_led(&totalstrip[ledcounter], 255,255,255); |
vsluiter | 0:cde34c55fa20 | 329 | } |
vsluiter | 0:cde34c55fa20 | 330 | else |
vsluiter | 0:cde34c55fa20 | 331 | write_led(&totalstrip[ledcounter], 0,0,0); |
vsluiter | 0:cde34c55fa20 | 332 | } |
vsluiter | 0:cde34c55fa20 | 333 | } |
vsluiter | 0:cde34c55fa20 | 334 | } |
vsluiter | 0:cde34c55fa20 | 335 | |
vsluiter | 5:d2e819018807 | 336 | void UpdateDemoPaddle(void) |
vsluiter | 1:7a2ec350cdf5 | 337 | { |
vsluiter | 1:7a2ec350cdf5 | 338 | static uint8_t direction = 1; |
vsluiter | 1:7a2ec350cdf5 | 339 | if(direction) { |
vsluiter | 1:7a2ec350cdf5 | 340 | paddlestart++; |
vsluiter | 1:7a2ec350cdf5 | 341 | } else { |
vsluiter | 1:7a2ec350cdf5 | 342 | paddlestart--; |
vsluiter | 1:7a2ec350cdf5 | 343 | } |
vsluiter | 1:7a2ec350cdf5 | 344 | |
vsluiter | 1:7a2ec350cdf5 | 345 | if(paddlestart > (NUMBER_OF_PIXELS - PADDLE_LENGTH)) |
vsluiter | 1:7a2ec350cdf5 | 346 | direction = 0; |
vsluiter | 1:7a2ec350cdf5 | 347 | if(paddlestart < 0) |
vsluiter | 1:7a2ec350cdf5 | 348 | direction = 1; |
vsluiter | 1:7a2ec350cdf5 | 349 | } |
vsluiter | 1:7a2ec350cdf5 | 350 | |
vsluiter | 2:d2f8772a8db5 | 351 | |
vsluiter | 0:cde34c55fa20 | 352 | //DigitalOut myled(LED1); |
vsluiter | 0:cde34c55fa20 | 353 | |
vsluiter | 0:cde34c55fa20 | 354 | //int main() { |
vsluiter | 0:cde34c55fa20 | 355 | // while(1) { |
vsluiter | 0:cde34c55fa20 | 356 | // myled = 1; |
vsluiter | 0:cde34c55fa20 | 357 | // wait(0.2); |
vsluiter | 0:cde34c55fa20 | 358 | // myled = 0; |
vsluiter | 0:cde34c55fa20 | 359 | // wait(0.2); |
vsluiter | 0:cde34c55fa20 | 360 | // } |
vsluiter | 0:cde34c55fa20 | 361 | //} |