calliope-edu / microbit-v2-samples

CODAL build tools and sample programs for the micro:bit v2.x
MIT License
0 stars 1 forks source link

Demo Program Documentation #2

Open fabianhugo opened 1 year ago

fabianhugo commented 1 year ago

Links: Legacy Mini Demo Programm: https://github.com/calliope-mini/calliope-demo Doku für die Microbit Runtime: https://lancaster-university.github.io/microbit-docs/ubit/ Um zu Speichern, dass der Calliope bereits einmal an war: https://lancaster-university.github.io/microbit-docs/ubit/storage/ Unofficial get started documentation: https://microbit.c272.org/

RGB LEDs können mit internem Treiber verwendet werden, z.B.

    int length = 3;
    // Brightness of pixels
    int brightness = 32;
    // Create a buffer to hold pixel data of the appropriate size
    ManagedBuffer b(length*3);
    b.fill(0);
    b[0]=brightness; //turns 1st LED green
    b[1]=0;
    b[2]=0;
    b[3]=0;
    b[4]=brightness; //turns 2nd LED red
    b[5]=0;
    b[6]=0;
    b[7]=0;
    b[8]=brightness; //turns 3rd LED blue
    neopixel_send_buffer(uBit.io.RGB, b);

Ziel ist calliope mini 2 Demo Programm nachzubauen ab dem Moment wo das Hi auf dem Display scrollt.

Programm Abschnitte:

  1. Test Routine (Display,RGB Leds, eCompass, Button A, Speaker)
  2. Willkommens Programm (Hi, Button Presses, Shake, LED Fade)
  3. Programmauswahl Wenn die Programmabschnitte 1 und 2 durchlaufen sind, wird bei Neustart immer am nächsten Programm Abschnitt gestartet. z.B: Wenn der Test durchgelaufen ist, startet immer das Willkommens Programm nach Neustart. Wenn Das Willkommensprogramm durchlaufen ist, startet immer die Programmauswahl.
fabianhugo commented 1 year ago

Mit diesem Code kommen Werte aus dem Mikro.

#include "MicroBit.h"
#include "samples/Tests.h"

MicroBit uBit;
int v;

int main()
{

    uBit.audio.activateMic();
    uBit.sleep(100); // seems to be necessary
    while(1){    

        v=uBit.audio.levelSPL->getValue(); //what is SPL?
        uBit.serial.printf("%d \n",v);
        uBit.sleep(100);
    }

    // out_of_box_experience();
}

Fehlermeldung: RGB Pin not found:

fabianhugo commented 1 year ago

damit kommen gute werte aus dem microphon

#include "MicroBit.h"
#include "samples/Tests.h"
#include "LevelDetector.h"
#include "LevelDetectorSPL.h"

#define MICROPHONE_MIN 52.0f
#define MICROPHONE_MAX 120.0f

MicroBit uBit;

int soundLevel() {

    LevelDetectorSPL* level = uBit.audio.levelSPL;
    if (NULL == level)
        return 0;
    const int micValue = level->getValue();
    const int scaled = max(MICROPHONE_MIN, min(micValue, MICROPHONE_MAX)) - MICROPHONE_MIN;
    return min(0xff, scaled * 0xff / (MICROPHONE_MAX - MICROPHONE_MIN));

}

int main()
{

    uBit.audio.activateMic();

    uBit.sleep(100); // seems to be necessary
    while(1){    

        uBit.serial.printf("%d \n",soundLevel());
        uBit.sleep(100);
    }

    // out_of_box_experience();
}
fabianhugo commented 1 year ago

HSL color transition:

typedef struct {
    double r;       // a fraction between 0 and 1
    double g;       // a fraction between 0 and 1
    double b;       // a fraction between 0 and 1
} rgb;

typedef struct {
    double h;       // angle in degrees
    double s;       // a fraction between 0 and 1
    double v;       // a fraction between 0 and 1
} hsv;

static rgb   hsv2rgb(hsv in);

rgb hsv2rgb(hsv in)
{
    double      hh, p, q, t, ff;
    long        i;
    rgb         out;

    if(in.s <= 0.0) {       // < is bogus, just shuts up warnings
        out.r = in.v;
        out.g = in.v;
        out.b = in.v;
        return out;
    }
    hh = in.h;
    if(hh >= 360.0) hh = 0.0;
    hh /= 60.0;
    i = (long)hh;
    ff = hh - i;
    p = in.v * (1.0 - in.s);
    q = in.v * (1.0 - (in.s * ff));
    t = in.v * (1.0 - (in.s * (1.0 - ff)));

    switch(i) {
    case 0:
        out.r = in.v;
        out.g = t;
        out.b = p;
        break;
    case 1:
        out.r = q;
        out.g = in.v;
        out.b = p;
        break;
    case 2:
        out.r = p;
        out.g = in.v;
        out.b = t;
        break;

    case 3:
        out.r = p;
        out.g = q;
        out.b = in.v;
        break;
    case 4:
        out.r = t;
        out.g = p;
        out.b = in.v;
        break;
    case 5:
    default:
        out.r = in.v;
        out.g = p;
        out.b = q;
        break;
    }
    return out;     
}
void rainbow(void){
    ManagedBuffer b(3 * 3);
    hsv color;

    uBit.display.setBrightness(0);
    uBit.display.print(smiley);
    color.h=0; // angle
    color.s=1; // saturation
    color.v=0.16; //brightness
    for(int j=0; j<300; j++) { // 
        for(int i=0; i<3; i++) {
            color.h = color.h +i*30; // different color for each RGB led
            b[0+i*3]= (int)(hsv2rgb(color).g*255);
            b[1+i*3]= (int)(hsv2rgb(color).b*255);
            b[2+i*3]= (int)(hsv2rgb(color).r*255);

        }
        color.h=j; // increment angle
        uBit.sleep(10); // speed of transition
        neopixel_send_buffer(uBit.io.RGB, b);
        uBit.display.setBrightness(j);
    } 
    for(int j=0; j<31; j++) { // brightness is 0.16, slowly reduce
        color.h = 300; // start with previous color
        color.v = color.v - 0.005; // dim rgb
        for(int i=0; i<3; i++) {
            color.h = color.h + i*30; // different color for each RGB led
            b[0+i*3]= (int)(hsv2rgb(color).g*255);
            b[1+i*3]= (int)(hsv2rgb(color).b*255);
            b[2+i*3]= (int)(hsv2rgb(color).r*255);

        }
        neopixel_send_buffer(uBit.io.RGB, b);
        uBit.display.setBrightness(300-j*10); //dim display
        uBit.sleep(10);
    }
    uBit.display.clear();
    uBit.display.setBrightness(100); // restore brightness
 }