Letter, points and light. Stars demo, scroll demo, flare demo, bounce demo
Dependencies: microbit
MicroBit Graphics Demo
- FunctionPointFlare();
- FunctionBounceTest();
- FunctionImageTest();
- FunctionPrintTest();
- FunctionStarTest();
- FunctionImageScroll();
Functions.h@4:47230ba0a5f0, 2020-01-01 (annotated)
- Committer:
- MaxScorda
- Date:
- Wed Jan 01 10:23:12 2020 +0000
- Revision:
- 4:47230ba0a5f0
- Parent:
- 3:7fb07f1eeffe
Publish
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
MaxScorda | 1:ed77b498d5c8 | 1 | void FunctionPointFlare() |
MaxScorda | 1:ed77b498d5c8 | 2 | { |
MaxScorda | 1:ed77b498d5c8 | 3 | |
MaxScorda | 1:ed77b498d5c8 | 4 | int value = 64 ; |
MaxScorda | 1:ed77b498d5c8 | 5 | for(int xx=-1 ; xx<=1; xx++) { |
MaxScorda | 1:ed77b498d5c8 | 6 | for( int yy=-1; yy<=1; yy++) { |
MaxScorda | 1:ed77b498d5c8 | 7 | uBit.display.image.setPixelValue(oldx+yy,oldy+xx, 0); |
MaxScorda | 1:ed77b498d5c8 | 8 | } |
MaxScorda | 1:ed77b498d5c8 | 9 | } |
MaxScorda | 1:ed77b498d5c8 | 10 | |
MaxScorda | 1:ed77b498d5c8 | 11 | |
MaxScorda | 1:ed77b498d5c8 | 12 | for(int xx=-1 ; xx<=1; xx++) { |
MaxScorda | 1:ed77b498d5c8 | 13 | for( int yy=-1; yy<=1; yy++) { |
MaxScorda | 1:ed77b498d5c8 | 14 | uBit.display.image.setPixelValue(i+yy,k+xx, 2); |
MaxScorda | 1:ed77b498d5c8 | 15 | // uBit.serial.printf("%d - %d", yy,xx); |
MaxScorda | 1:ed77b498d5c8 | 16 | } |
MaxScorda | 1:ed77b498d5c8 | 17 | } |
MaxScorda | 1:ed77b498d5c8 | 18 | uBit.display.image.setPixelValue(i,k, value); |
MaxScorda | 1:ed77b498d5c8 | 19 | |
MaxScorda | 1:ed77b498d5c8 | 20 | oldx=i; |
MaxScorda | 1:ed77b498d5c8 | 21 | oldy=k; |
MaxScorda | 1:ed77b498d5c8 | 22 | |
MaxScorda | 1:ed77b498d5c8 | 23 | i=(i+sign); |
MaxScorda | 1:ed77b498d5c8 | 24 | if ((i<0) || (i>4)) { |
MaxScorda | 1:ed77b498d5c8 | 25 | sign=-sign; |
MaxScorda | 1:ed77b498d5c8 | 26 | i=(i+sign); |
MaxScorda | 1:ed77b498d5c8 | 27 | k++; |
MaxScorda | 1:ed77b498d5c8 | 28 | k=k%5; |
MaxScorda | 1:ed77b498d5c8 | 29 | } |
MaxScorda | 1:ed77b498d5c8 | 30 | |
MaxScorda | 1:ed77b498d5c8 | 31 | // uBit.serial.printf("\nAccleremoter "); |
MaxScorda | 1:ed77b498d5c8 | 32 | // uBit.serial.printf("x:%d y:%d", uBit.accelerometer.getX(),uBit.accelerometer.getY()); |
MaxScorda | 1:ed77b498d5c8 | 33 | |
MaxScorda | 1:ed77b498d5c8 | 34 | uBit.sleep(100); |
MaxScorda | 1:ed77b498d5c8 | 35 | } |
MaxScorda | 1:ed77b498d5c8 | 36 | |
MaxScorda | 1:ed77b498d5c8 | 37 | void FunctionPrintTest() |
MaxScorda | 1:ed77b498d5c8 | 38 | { |
MaxScorda | 1:ed77b498d5c8 | 39 | // uBit.display.scrollAsync("Scorda",500); |
MaxScorda | 2:b767fa1c9241 | 40 | uBit.display.scrollAsync("Scorda"); |
MaxScorda | 2:b767fa1c9241 | 41 | uBit.sleep(500); |
MaxScorda | 1:ed77b498d5c8 | 42 | } |
MaxScorda | 1:ed77b498d5c8 | 43 | |
MaxScorda | 1:ed77b498d5c8 | 44 | void FunctionImageTest() |
MaxScorda | 1:ed77b498d5c8 | 45 | { |
MaxScorda | 4:47230ba0a5f0 | 46 | if (ccont%3==0) { |
MaxScorda | 4:47230ba0a5f0 | 47 | display.setBrightness(255); |
MaxScorda | 1:ed77b498d5c8 | 48 | uBit.display.print(C); |
MaxScorda | 4:47230ba0a5f0 | 49 | } else if (ccont%3==1) { |
MaxScorda | 4:47230ba0a5f0 | 50 | display.setBrightness(127); |
MaxScorda | 1:ed77b498d5c8 | 51 | uBit.display.print(Cb); |
MaxScorda | 4:47230ba0a5f0 | 52 | } else if (ccont%3==2) { |
MaxScorda | 4:47230ba0a5f0 | 53 | display.setBrightness(64) ; |
MaxScorda | 1:ed77b498d5c8 | 54 | uBit.display.print(Cd); |
MaxScorda | 4:47230ba0a5f0 | 55 | } |
MaxScorda | 1:ed77b498d5c8 | 56 | |
MaxScorda | 1:ed77b498d5c8 | 57 | uBit.sleep(500); |
MaxScorda | 1:ed77b498d5c8 | 58 | |
MaxScorda | 1:ed77b498d5c8 | 59 | } |
MaxScorda | 1:ed77b498d5c8 | 60 | |
MaxScorda | 1:ed77b498d5c8 | 61 | |
MaxScorda | 1:ed77b498d5c8 | 62 | void FunctionBounceTest() |
MaxScorda | 1:ed77b498d5c8 | 63 | { |
MaxScorda | 1:ed77b498d5c8 | 64 | static int xx[]= {2,0,0}; |
MaxScorda | 1:ed77b498d5c8 | 65 | static int yy[]= {3,0,0}; |
MaxScorda | 1:ed77b498d5c8 | 66 | static int tempidx, idxvett=0; |
MaxScorda | 1:ed77b498d5c8 | 67 | static int xost; |
MaxScorda | 1:ed77b498d5c8 | 68 | static int yost; |
MaxScorda | 1:ed77b498d5c8 | 69 | static int xcont=0; |
MaxScorda | 1:ed77b498d5c8 | 70 | static int signx=1, signy=1; |
MaxScorda | 4:47230ba0a5f0 | 71 | int value= 256; |
MaxScorda | 1:ed77b498d5c8 | 72 | bool cambiay=false; |
MaxScorda | 1:ed77b498d5c8 | 73 | int tempcolor; |
MaxScorda | 1:ed77b498d5c8 | 74 | |
MaxScorda | 1:ed77b498d5c8 | 75 | tempidx=idxvett; |
MaxScorda | 1:ed77b498d5c8 | 76 | idxvett=(idxvett+1)%3; |
MaxScorda | 1:ed77b498d5c8 | 77 | uBit.display.image.setPixelValue(xx[idxvett],yy[idxvett], 0); |
MaxScorda | 1:ed77b498d5c8 | 78 | xx[idxvett]=(xx[tempidx]+signx)%5; |
MaxScorda | 1:ed77b498d5c8 | 79 | yy[idxvett]=(yy[tempidx]+signy)%5; |
MaxScorda | 1:ed77b498d5c8 | 80 | |
MaxScorda | 1:ed77b498d5c8 | 81 | for(int i=0 ; i<=2; i++) { |
MaxScorda | 1:ed77b498d5c8 | 82 | tempidx=(tempidx+i)%3; |
MaxScorda | 4:47230ba0a5f0 | 83 | tempcolor=value/((i*32)+1); |
MaxScorda | 1:ed77b498d5c8 | 84 | uBit.display.image.setPixelValue(xx[tempidx],yy[tempidx], tempcolor); |
MaxScorda | 1:ed77b498d5c8 | 85 | } |
MaxScorda | 3:7fb07f1eeffe | 86 | //se sbatte sul punto ostacolo o ha fatto due |
MaxScorda | 1:ed77b498d5c8 | 87 | if (((xx[idxvett]==xost)&&(yy[idxvett]==yost))||(xcont==2)) { |
MaxScorda | 1:ed77b498d5c8 | 88 | uBit.display.image.setPixelValue(xost,yost, 0); |
MaxScorda | 1:ed77b498d5c8 | 89 | if ((xx[idxvett]==xost)&&(yy[idxvett]==yost)) |
MaxScorda | 1:ed77b498d5c8 | 90 | cambiay=true; |
MaxScorda | 1:ed77b498d5c8 | 91 | xost=rand()%5; |
MaxScorda | 1:ed77b498d5c8 | 92 | yost=rand()%5; |
MaxScorda | 1:ed77b498d5c8 | 93 | uBit.display.image.setPixelValue(xost,yost, value/4); |
MaxScorda | 1:ed77b498d5c8 | 94 | xcont=0; |
MaxScorda | 1:ed77b498d5c8 | 95 | } |
MaxScorda | 1:ed77b498d5c8 | 96 | |
MaxScorda | 1:ed77b498d5c8 | 97 | if ((yy[idxvett]==0) || (yy[idxvett]==4) || (cambiay==true)) { |
MaxScorda | 1:ed77b498d5c8 | 98 | signy=-signy; |
MaxScorda | 1:ed77b498d5c8 | 99 | } |
MaxScorda | 1:ed77b498d5c8 | 100 | |
MaxScorda | 1:ed77b498d5c8 | 101 | if ((xx[idxvett]==0) || (xx[idxvett]==4)) |
MaxScorda | 1:ed77b498d5c8 | 102 | signx=-signx; |
MaxScorda | 1:ed77b498d5c8 | 103 | |
MaxScorda | 1:ed77b498d5c8 | 104 | if (xx[idxvett]==0) |
MaxScorda | 1:ed77b498d5c8 | 105 | xcont++; |
MaxScorda | 1:ed77b498d5c8 | 106 | |
MaxScorda | 3:7fb07f1eeffe | 107 | uBit.sleep(250); |
MaxScorda | 3:7fb07f1eeffe | 108 | |
MaxScorda | 3:7fb07f1eeffe | 109 | } |
MaxScorda | 3:7fb07f1eeffe | 110 | |
MaxScorda | 3:7fb07f1eeffe | 111 | void FunctionStarTest() |
MaxScorda | 3:7fb07f1eeffe | 112 | { |
MaxScorda | 3:7fb07f1eeffe | 113 | // stars[x][0] x |
MaxScorda | 3:7fb07f1eeffe | 114 | // stars[x][1] y |
MaxScorda | 3:7fb07f1eeffe | 115 | // stars[x][2] speed |
MaxScorda | 3:7fb07f1eeffe | 116 | // stars[x][3] light |
MaxScorda | 3:7fb07f1eeffe | 117 | static int stars[5][4]; |
MaxScorda | 3:7fb07f1eeffe | 118 | static bool first=true; |
MaxScorda | 3:7fb07f1eeffe | 119 | int tempspeed; |
MaxScorda | 3:7fb07f1eeffe | 120 | //azzero primo giro |
MaxScorda | 3:7fb07f1eeffe | 121 | if (first==true) { |
MaxScorda | 3:7fb07f1eeffe | 122 | for(int i=0 ; i<5; i++) { |
MaxScorda | 3:7fb07f1eeffe | 123 | stars[i][1]=-1; |
MaxScorda | 3:7fb07f1eeffe | 124 | } |
MaxScorda | 3:7fb07f1eeffe | 125 | first=false; |
MaxScorda | 3:7fb07f1eeffe | 126 | } |
MaxScorda | 3:7fb07f1eeffe | 127 | |
MaxScorda | 3:7fb07f1eeffe | 128 | for(int i=0 ; i<5; i++) { |
MaxScorda | 3:7fb07f1eeffe | 129 | if (stars[i][1]==-1) { |
MaxScorda | 3:7fb07f1eeffe | 130 | stars[i][0]=i; //inutile ma ad uso futuro |
MaxScorda | 3:7fb07f1eeffe | 131 | stars[i][1]=uBit.random(2); //y |
MaxScorda | 3:7fb07f1eeffe | 132 | stars[i][2]=uBit.random(2); //speed |
MaxScorda | 3:7fb07f1eeffe | 133 | stars[i][3]=((uBit.random(8)+1)*32)-1; //light |
MaxScorda | 3:7fb07f1eeffe | 134 | } else { |
MaxScorda | 3:7fb07f1eeffe | 135 | uBit.display.image.setPixelValue(stars[i][0],stars[i][1], 0); |
MaxScorda | 3:7fb07f1eeffe | 136 | if (stars[i][2]==0) { |
MaxScorda | 3:7fb07f1eeffe | 137 | tempspeed=(ccont%2==0?1:0); |
MaxScorda | 3:7fb07f1eeffe | 138 | } else { |
MaxScorda | 3:7fb07f1eeffe | 139 | tempspeed=stars[i][2]; |
MaxScorda | 3:7fb07f1eeffe | 140 | } |
MaxScorda | 3:7fb07f1eeffe | 141 | stars[i][1]=stars[i][1]+tempspeed; |
MaxScorda | 3:7fb07f1eeffe | 142 | if (stars[i][1]>4) |
MaxScorda | 3:7fb07f1eeffe | 143 | stars[i][1]=-1; |
MaxScorda | 3:7fb07f1eeffe | 144 | } |
MaxScorda | 3:7fb07f1eeffe | 145 | uBit.display.image.setPixelValue(stars[i][0],stars[i][1], stars[i][3]); |
MaxScorda | 4:47230ba0a5f0 | 146 | // uBit.serial.printf("\n%d: %d ",i,stars[i][1]); |
MaxScorda | 3:7fb07f1eeffe | 147 | } |
MaxScorda | 4:47230ba0a5f0 | 148 | uBit.sleep(250); |
MaxScorda | 4:47230ba0a5f0 | 149 | } |
MaxScorda | 1:ed77b498d5c8 | 150 | |
MaxScorda | 4:47230ba0a5f0 | 151 | void FunctionImageScroll() |
MaxScorda | 4:47230ba0a5f0 | 152 | { |
MaxScorda | 4:47230ba0a5f0 | 153 | display.setBrightness(255); |
MaxScorda | 4:47230ba0a5f0 | 154 | uBit.display.scrollAsync(W1,100); |
MaxScorda | 4:47230ba0a5f0 | 155 | uBit.sleep(500); |
MaxScorda | 1:ed77b498d5c8 | 156 | |
MaxScorda | 1:ed77b498d5c8 | 157 | } |
MaxScorda | 1:ed77b498d5c8 | 158 | |
MaxScorda | 4:47230ba0a5f0 | 159 | |
MaxScorda | 1:ed77b498d5c8 | 160 | void onButton(MicroBitEvent e) |
MaxScorda | 1:ed77b498d5c8 | 161 | { |
MaxScorda | 4:47230ba0a5f0 | 162 | static bool premuto=true; |
MaxScorda | 4:47230ba0a5f0 | 163 | int numfun=6; |
MaxScorda | 1:ed77b498d5c8 | 164 | if ((e.source == MICROBIT_ID_BUTTON_A) && (premuto==false)) { |
MaxScorda | 1:ed77b498d5c8 | 165 | sceltavar--; |
MaxScorda | 1:ed77b498d5c8 | 166 | uBit.display.clear(); |
MaxScorda | 2:b767fa1c9241 | 167 | uBit.display.stopAnimation(); |
MaxScorda | 1:ed77b498d5c8 | 168 | uBit.serial.printf("\nBUTTON A: "); |
MaxScorda | 2:b767fa1c9241 | 169 | premuto=true; |
MaxScorda | 1:ed77b498d5c8 | 170 | } |
MaxScorda | 1:ed77b498d5c8 | 171 | |
MaxScorda | 1:ed77b498d5c8 | 172 | if ((e.source == MICROBIT_ID_BUTTON_B)&& (premuto==false)) { |
MaxScorda | 1:ed77b498d5c8 | 173 | sceltavar++; |
MaxScorda | 1:ed77b498d5c8 | 174 | uBit.display.clear(); |
MaxScorda | 3:7fb07f1eeffe | 175 | uBit.display.stopAnimation(); |
MaxScorda | 1:ed77b498d5c8 | 176 | uBit.serial.printf("\nBUTTON B: "); |
MaxScorda | 1:ed77b498d5c8 | 177 | premuto=true; |
MaxScorda | 1:ed77b498d5c8 | 178 | } |
MaxScorda | 2:b767fa1c9241 | 179 | /* |
MaxScorda | 2:b767fa1c9241 | 180 | if (e.value == MICROBIT_BUTTON_EVT_CLICK) { |
MaxScorda | 2:b767fa1c9241 | 181 | uBit.serial.printf("\nCLICK"); |
MaxScorda | 2:b767fa1c9241 | 182 | premuto=true; |
MaxScorda | 2:b767fa1c9241 | 183 | } |
MaxScorda | 1:ed77b498d5c8 | 184 | |
MaxScorda | 2:b767fa1c9241 | 185 | if (e.value == MICROBIT_BUTTON_EVT_HOLD) { |
MaxScorda | 2:b767fa1c9241 | 186 | uBit.serial.printf("\nHOLD"); |
MaxScorda | 2:b767fa1c9241 | 187 | premuto=true; |
MaxScorda | 2:b767fa1c9241 | 188 | } |
MaxScorda | 2:b767fa1c9241 | 189 | if (e.value == MICROBIT_BUTTON_EVT_DOWN) { |
MaxScorda | 2:b767fa1c9241 | 190 | uBit.serial.printf("\nDOWN"); |
MaxScorda | 2:b767fa1c9241 | 191 | premuto=true; |
MaxScorda | 2:b767fa1c9241 | 192 | } |
MaxScorda | 2:b767fa1c9241 | 193 | */ |
MaxScorda | 1:ed77b498d5c8 | 194 | if (e.value == MICROBIT_BUTTON_EVT_UP) { |
MaxScorda | 1:ed77b498d5c8 | 195 | uBit.serial.printf("UP"); |
MaxScorda | 1:ed77b498d5c8 | 196 | premuto=false; |
MaxScorda | 1:ed77b498d5c8 | 197 | } |
MaxScorda | 1:ed77b498d5c8 | 198 | |
MaxScorda | 1:ed77b498d5c8 | 199 | sceltavar=(sceltavar+numfun)%numfun; |
MaxScorda | 2:b767fa1c9241 | 200 | // uBit.serial.printf("--%d--", sceltavar); |
MaxScorda | 1:ed77b498d5c8 | 201 | |
MaxScorda | 1:ed77b498d5c8 | 202 | } |
MaxScorda | 1:ed77b498d5c8 | 203 |