Solution to the Bluetooth animation service exercise
Fork of microbit-animator by
Animator.cpp@7:1878a4427199, 2017-02-06 (annotated)
- Committer:
- bluetooth_mdw
- Date:
- Mon Feb 06 09:44:09 2017 +0000
- Revision:
- 7:1878a4427199
- Parent:
- 0:394f7f99082b
Solution for Bluetooth SIG hands-on training course
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bluetooth_mdw | 0:394f7f99082b | 1 | /* |
bluetooth_mdw | 0:394f7f99082b | 2 | The MIT License (MIT) |
bluetooth_mdw | 0:394f7f99082b | 3 | |
bluetooth_mdw | 0:394f7f99082b | 4 | Copyright (c) 2016 British Broadcasting Corporation. |
bluetooth_mdw | 0:394f7f99082b | 5 | This software is provided by Lancaster University by arrangement with the BBC. |
bluetooth_mdw | 0:394f7f99082b | 6 | |
bluetooth_mdw | 0:394f7f99082b | 7 | Permission is hereby granted, free of charge, to any person obtaining a |
bluetooth_mdw | 0:394f7f99082b | 8 | copy of this software and associated documentation files (the "Software"), |
bluetooth_mdw | 0:394f7f99082b | 9 | to deal in the Software without restriction, including without limitation |
bluetooth_mdw | 0:394f7f99082b | 10 | the rights to use, copy, modify, merge, publish, distribute, sublicense, |
bluetooth_mdw | 0:394f7f99082b | 11 | and/or sell copies of the Software, and to permit persons to whom the |
bluetooth_mdw | 0:394f7f99082b | 12 | Software is furnished to do so, subject to the following conditions: |
bluetooth_mdw | 0:394f7f99082b | 13 | |
bluetooth_mdw | 0:394f7f99082b | 14 | The above copyright notice and this permission notice shall be included in |
bluetooth_mdw | 0:394f7f99082b | 15 | all copies or substantial portions of the Software. |
bluetooth_mdw | 0:394f7f99082b | 16 | |
bluetooth_mdw | 0:394f7f99082b | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
bluetooth_mdw | 0:394f7f99082b | 18 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
bluetooth_mdw | 0:394f7f99082b | 19 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO Animation SHALL |
bluetooth_mdw | 0:394f7f99082b | 20 | THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
bluetooth_mdw | 0:394f7f99082b | 21 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
bluetooth_mdw | 0:394f7f99082b | 22 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
bluetooth_mdw | 0:394f7f99082b | 23 | DEALINGS IN THE SOFTWARE. |
bluetooth_mdw | 0:394f7f99082b | 24 | */ |
bluetooth_mdw | 0:394f7f99082b | 25 | |
bluetooth_mdw | 0:394f7f99082b | 26 | #include "Animator.h" |
bluetooth_mdw | 0:394f7f99082b | 27 | #include "MicroBit.h" |
bluetooth_mdw | 0:394f7f99082b | 28 | #include <math.h> |
bluetooth_mdw | 0:394f7f99082b | 29 | #include <mbed.h> |
bluetooth_mdw | 0:394f7f99082b | 30 | |
bluetooth_mdw | 0:394f7f99082b | 31 | MicroBitImage pattern(5,5); |
bluetooth_mdw | 0:394f7f99082b | 32 | |
bluetooth_mdw | 0:394f7f99082b | 33 | Serial pc(USBTX, USBRX); |
bluetooth_mdw | 0:394f7f99082b | 34 | |
bluetooth_mdw | 0:394f7f99082b | 35 | int inx=0; |
bluetooth_mdw | 0:394f7f99082b | 36 | int delta = 1; |
bluetooth_mdw | 0:394f7f99082b | 37 | int pixel_value=255; |
bluetooth_mdw | 0:394f7f99082b | 38 | int flash_state=1; |
bluetooth_mdw | 0:394f7f99082b | 39 | int num_rnd_pixels = 4; |
bluetooth_mdw | 0:394f7f99082b | 40 | |
bluetooth_mdw | 0:394f7f99082b | 41 | int ripple_pixels[4][25] = { |
bluetooth_mdw | 0:394f7f99082b | 42 | {0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0}, |
bluetooth_mdw | 0:394f7f99082b | 43 | {0,0,0,0,0, 0,0,0,0,0, 0,0,1,0,0, 0,0,0,0,0, 0,0,0,0,0}, |
bluetooth_mdw | 0:394f7f99082b | 44 | {0,0,0,0,0, 0,1,1,1,0, 0,1,0,1,0, 0,1,1,1,0, 0,0,0,0,0}, |
bluetooth_mdw | 0:394f7f99082b | 45 | {1,1,1,1,1, 1,0,0,0,1, 1,0,0,0,1, 1,0,0,0,1, 1,1,1,1,1} |
bluetooth_mdw | 0:394f7f99082b | 46 | }; |
bluetooth_mdw | 0:394f7f99082b | 47 | |
bluetooth_mdw | 0:394f7f99082b | 48 | int spiral_pixels[25] = { |
bluetooth_mdw | 0:394f7f99082b | 49 | 12, 13, 8, 7, 6, 11, 16, 17, 18, 19, 14, 9, 4, 3, 2, 1, 0, 5, 10, 15, 20, 21, 22, 23, 24 |
bluetooth_mdw | 0:394f7f99082b | 50 | }; |
bluetooth_mdw | 0:394f7f99082b | 51 | |
bluetooth_mdw | 0:394f7f99082b | 52 | Animator::Animator(MicroBitDisplay &_display) : |
bluetooth_mdw | 0:394f7f99082b | 53 | display(_display), sleep_time(500) |
bluetooth_mdw | 0:394f7f99082b | 54 | { |
bluetooth_mdw | 0:394f7f99082b | 55 | } |
bluetooth_mdw | 0:394f7f99082b | 56 | |
bluetooth_mdw | 0:394f7f99082b | 57 | void Animator::setAnimationType(uint16_t animation) |
bluetooth_mdw | 0:394f7f99082b | 58 | { |
bluetooth_mdw | 0:394f7f99082b | 59 | pc.printf("Animator::setAnimationType %d\n",animation); |
bluetooth_mdw | 0:394f7f99082b | 60 | if (animation <= NUMBER_OF_ANIMATIONS) { |
bluetooth_mdw | 0:394f7f99082b | 61 | current_animation = animation; |
bluetooth_mdw | 0:394f7f99082b | 62 | pattern.clear(); |
bluetooth_mdw | 0:394f7f99082b | 63 | display.clear(); |
bluetooth_mdw | 0:394f7f99082b | 64 | switch (current_animation) { |
bluetooth_mdw | 0:394f7f99082b | 65 | case ANIMATION_TYPE_FLASH: |
bluetooth_mdw | 0:394f7f99082b | 66 | sleep_time = 500; |
bluetooth_mdw | 0:394f7f99082b | 67 | break; |
bluetooth_mdw | 0:394f7f99082b | 68 | case ANIMATION_TYPE_RIPPLE: |
bluetooth_mdw | 0:394f7f99082b | 69 | sleep_time = 100; |
bluetooth_mdw | 0:394f7f99082b | 70 | inx=0; |
bluetooth_mdw | 0:394f7f99082b | 71 | delta = 1; |
bluetooth_mdw | 0:394f7f99082b | 72 | break; |
bluetooth_mdw | 0:394f7f99082b | 73 | case ANIMATION_TYPE_SPIRAL: |
bluetooth_mdw | 0:394f7f99082b | 74 | sleep_time = 50; |
bluetooth_mdw | 0:394f7f99082b | 75 | break; |
bluetooth_mdw | 0:394f7f99082b | 76 | } |
bluetooth_mdw | 0:394f7f99082b | 77 | } |
bluetooth_mdw | 0:394f7f99082b | 78 | } |
bluetooth_mdw | 0:394f7f99082b | 79 | |
bluetooth_mdw | 0:394f7f99082b | 80 | void Animator::start() |
bluetooth_mdw | 0:394f7f99082b | 81 | { |
bluetooth_mdw | 0:394f7f99082b | 82 | playing = 1; |
bluetooth_mdw | 0:394f7f99082b | 83 | MicroBitEvent(ANIMATION_STATUS_EVENT,ANIMATION_STATUS_BUSY); |
bluetooth_mdw | 0:394f7f99082b | 84 | } |
bluetooth_mdw | 0:394f7f99082b | 85 | |
bluetooth_mdw | 0:394f7f99082b | 86 | void Animator::stop() |
bluetooth_mdw | 0:394f7f99082b | 87 | { |
bluetooth_mdw | 0:394f7f99082b | 88 | playing = 0; |
bluetooth_mdw | 0:394f7f99082b | 89 | display.clear(); |
bluetooth_mdw | 0:394f7f99082b | 90 | MicroBitEvent(ANIMATION_STATUS_EVENT,ANIMATION_STATUS_FREE); |
bluetooth_mdw | 0:394f7f99082b | 91 | } |
bluetooth_mdw | 0:394f7f99082b | 92 | |
bluetooth_mdw | 0:394f7f99082b | 93 | void Animator::faster() |
bluetooth_mdw | 0:394f7f99082b | 94 | { |
bluetooth_mdw | 0:394f7f99082b | 95 | } |
bluetooth_mdw | 0:394f7f99082b | 96 | |
bluetooth_mdw | 0:394f7f99082b | 97 | void Animator::slower() |
bluetooth_mdw | 0:394f7f99082b | 98 | { |
bluetooth_mdw | 0:394f7f99082b | 99 | } |
bluetooth_mdw | 0:394f7f99082b | 100 | |
bluetooth_mdw | 0:394f7f99082b | 101 | void Animator::ripple() { |
bluetooth_mdw | 0:394f7f99082b | 102 | int i=0; |
bluetooth_mdw | 0:394f7f99082b | 103 | for (i=0;i<25;i++) { |
bluetooth_mdw | 0:394f7f99082b | 104 | pattern.setPixelValue(i % 5 , floor(static_cast<double>(i / 5)), ripple_pixels[inx][i]); |
bluetooth_mdw | 0:394f7f99082b | 105 | } |
bluetooth_mdw | 0:394f7f99082b | 106 | display.image.paste(pattern); |
bluetooth_mdw | 0:394f7f99082b | 107 | inx = inx + delta; |
bluetooth_mdw | 0:394f7f99082b | 108 | if (inx == 3 || inx == 0) { |
bluetooth_mdw | 0:394f7f99082b | 109 | delta = delta * -1; |
bluetooth_mdw | 0:394f7f99082b | 110 | } |
bluetooth_mdw | 0:394f7f99082b | 111 | } |
bluetooth_mdw | 0:394f7f99082b | 112 | |
bluetooth_mdw | 0:394f7f99082b | 113 | void Animator::flash() { |
bluetooth_mdw | 0:394f7f99082b | 114 | int i; |
bluetooth_mdw | 0:394f7f99082b | 115 | for (i=0;i<25;i++) { |
bluetooth_mdw | 0:394f7f99082b | 116 | pattern.setPixelValue(i % 5 , floor(static_cast<double>(i / 5)), flash_state * 255); |
bluetooth_mdw | 0:394f7f99082b | 117 | } |
bluetooth_mdw | 0:394f7f99082b | 118 | display.image.paste(pattern); |
bluetooth_mdw | 0:394f7f99082b | 119 | flash_state = !flash_state; |
bluetooth_mdw | 0:394f7f99082b | 120 | } |
bluetooth_mdw | 0:394f7f99082b | 121 | |
bluetooth_mdw | 0:394f7f99082b | 122 | void Animator::spiral() { |
bluetooth_mdw | 0:394f7f99082b | 123 | int x = spiral_pixels[inx] % 5; |
bluetooth_mdw | 0:394f7f99082b | 124 | int y = floor(static_cast<double>(spiral_pixels[inx] / 5)); |
bluetooth_mdw | 0:394f7f99082b | 125 | pattern.setPixelValue(x,y, pixel_value); |
bluetooth_mdw | 0:394f7f99082b | 126 | display.image.paste(pattern); |
bluetooth_mdw | 0:394f7f99082b | 127 | inx = inx + delta; |
bluetooth_mdw | 0:394f7f99082b | 128 | if (inx == 25 || inx == -1) { |
bluetooth_mdw | 0:394f7f99082b | 129 | delta = delta * -1; |
bluetooth_mdw | 0:394f7f99082b | 130 | inx = inx + delta; |
bluetooth_mdw | 0:394f7f99082b | 131 | if (pixel_value == 255) { |
bluetooth_mdw | 0:394f7f99082b | 132 | pixel_value = 0; |
bluetooth_mdw | 0:394f7f99082b | 133 | } else { |
bluetooth_mdw | 0:394f7f99082b | 134 | pixel_value = 255; |
bluetooth_mdw | 0:394f7f99082b | 135 | } |
bluetooth_mdw | 0:394f7f99082b | 136 | } |
bluetooth_mdw | 0:394f7f99082b | 137 | } |