Martin Woolley / microbit-animator

Dependencies:   microbit

Fork of microbit-animator by Martin Woolley

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Animator.cpp Source File

Animator.cpp

00001 /*
00002 The MIT License (MIT)
00003 
00004 Copyright (c) 2016 British Broadcasting Corporation.
00005 This software is provided by Lancaster University by arrangement with the BBC.
00006 
00007 Permission is hereby granted, free of charge, to any person obtaining a
00008 copy of this software and associated documentation files (the "Software"),
00009 to deal in the Software without restriction, including without limitation
00010 the rights to use, copy, modify, merge, publish, distribute, sublicense,
00011 and/or sell copies of the Software, and to permit persons to whom the
00012 Software is furnished to do so, subject to the following conditions:
00013 
00014 The above copyright notice and this permission notice shall be included in
00015 all copies or substantial portions of the Software.
00016 
00017 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00018 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00019 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO Animation SHALL
00020 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00021 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
00022 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
00023 DEALINGS IN THE SOFTWARE.
00024 */
00025 
00026 #include "Animator.h"
00027 #include "MicroBit.h"
00028 #include <math.h>
00029 #include <mbed.h>
00030 
00031 MicroBitImage pattern(5,5);
00032 
00033 Serial pc(USBTX, USBRX);
00034 
00035 int inx=0;
00036 int delta = 1;
00037 int pixel_value=255;
00038 int flash_state=1;
00039 int num_rnd_pixels = 4;
00040 
00041 int ripple_pixels[4][25] = {
00042     {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}, 
00043     {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}, 
00044     {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},
00045     {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} 
00046 };
00047 
00048 int spiral_pixels[25] = {
00049     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
00050 };
00051 
00052 Animator::Animator(MicroBitDisplay &_display) :
00053   display(_display), sleep_time(500)
00054 {
00055 }
00056 
00057 void Animator::setAnimationType(uint16_t animation) 
00058 {
00059     pc.printf("Animator::setAnimationType %d\n",animation);
00060     if (animation <= NUMBER_OF_ANIMATIONS) {
00061         current_animation = animation;
00062         pattern.clear();
00063         display.clear();
00064         switch (current_animation) {
00065           case ANIMATION_TYPE_FLASH:
00066             sleep_time = 500;
00067             break;
00068           case ANIMATION_TYPE_RIPPLE:
00069             sleep_time = 100;
00070             inx=0;
00071             delta = 1;
00072             break;
00073           case ANIMATION_TYPE_SPIRAL:
00074             sleep_time = 50;
00075             break;
00076         }        
00077     }
00078 }
00079 
00080 void Animator::start()
00081 {
00082     playing = 1;
00083     MicroBitEvent(ANIMATION_STATUS_EVENT,ANIMATION_STATUS_BUSY);
00084 }
00085     
00086 void Animator::stop()
00087 {
00088     playing = 0;
00089     display.clear();
00090     MicroBitEvent(ANIMATION_STATUS_EVENT,ANIMATION_STATUS_FREE);
00091 }
00092     
00093 void Animator::faster() 
00094 {
00095 }
00096     
00097 void Animator::slower()
00098 {
00099 }
00100 
00101 void Animator::ripple() {
00102     int i=0;
00103     for (i=0;i<25;i++) {
00104         pattern.setPixelValue(i % 5 , floor(static_cast<double>(i / 5)), ripple_pixels[inx][i]);
00105     }
00106     display.image.paste(pattern);
00107     inx = inx + delta;
00108     if (inx == 3 || inx == 0) {
00109         delta = delta * -1;
00110     }
00111 }
00112 
00113 void Animator::flash() {
00114     int i;
00115     for (i=0;i<25;i++) {
00116         pattern.setPixelValue(i % 5 , floor(static_cast<double>(i / 5)), flash_state * 255);
00117     }
00118     display.image.paste(pattern);
00119     flash_state = !flash_state;
00120 }
00121 
00122 void Animator::spiral() {
00123     int x = spiral_pixels[inx] % 5;
00124     int y = floor(static_cast<double>(spiral_pixels[inx] / 5));
00125     pattern.setPixelValue(x,y, pixel_value);
00126     display.image.paste(pattern);
00127     inx = inx + delta;
00128     if (inx == 25 || inx == -1) {
00129         delta = delta * -1;
00130         inx = inx + delta;
00131         if (pixel_value == 255) {
00132             pixel_value = 0;
00133         } else {
00134             pixel_value = 255;
00135         }
00136     }
00137 }