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