Skip to content

Instantly share code, notes, and snippets.

@nrjn
Created October 29, 2016 09:36
Show Gist options
  • Save nrjn/ac70521b5bd7d542138eee1554523219 to your computer and use it in GitHub Desktop.
Save nrjn/ac70521b5bd7d542138eee1554523219 to your computer and use it in GitHub Desktop.
#include "CalliopeDemos.h"
#include "MicroBit.h"
#include "CalliopeRGB.h"
#include "CalliopeSoundMotor.h"
#ifdef CALLIOPE_TEST_ALL
MicroBit uBit;
MicroBitImage full(
"255,255,255,255,255\n255,255,255,255,255\n255,255,255,255,255\n255,255,255,255,255\n255,255,255,255,255\n");
MicroBitImage tick("0,0,0,0,0\n0,0,0,0,255\n0,0,0,255,0\n255,0,255,0,0\n0,255,0,0,0\n");
const int arrow_h = 5;
const int arrow_w = 5;
const uint8_t left_arrow[] {
0, 0, 1, 0, 0,
0, 1, 0, 0, 0,
1, 1, 1, 1, 1,
0, 1, 0, 0, 0,
0, 0, 1, 0, 0,
};
const uint8_t right_arrow[] {
0, 0, 1, 0, 0,
0, 0, 0, 1, 0,
1, 1, 1, 1, 1,
0, 0, 0, 1, 0,
0, 0, 1, 0, 0,
};
const int heart_w = 10;
const int heart_h = 5;
const uint8_t heart[] = {
0, 1, 0, 1, 0, 0, 0, 0, 0, 0,
1, 1, 1, 1, 1, 0, 1, 0, 1, 0,
1, 1, 1, 1, 1, 0, 1, 1, 1, 0,
0, 1, 1, 1, 0, 0, 0, 1, 0, 0,
0, 0, 1, 0, 0, 0, 0, 0, 0, 0,
};
MicroBitImage SMILEY("0,255,0,255, 0\n0,255,0,255,0\n0,0,0,0,0\n255,0,0,0,255\n0,255,255,255,0\n");
MicroBitImage HEART(heart_w,heart_h,heart);
MicroBitImage ARROW_TO_LEFT(5, 5, left_arrow);
MicroBitImage ARROW_TO_RIGHT(5, 5, right_arrow);
volatile bool pressed_A = false;
volatile bool pressed_B = false;
void on_button_A(MicroBitEvent){
pressed_A = true;
}
void on_button_B(MicroBitEvent){
pressed_B = true;
}
void game_1(void){
uBit.display.scroll("Game 1");
}
void game_2(void){
uBit.display.scroll("Game 2");
}
int main() {
int counter = 0;
// Initialise the micro:bit runtime.
uBit.init();
uBit.messageBus.listen(MICROBIT_ID_BUTTON_A, MICROBIT_BUTTON_EVT_CLICK, on_button_A);
uBit.messageBus.listen(MICROBIT_ID_BUTTON_B, MICROBIT_BUTTON_EVT_CLICK, on_button_B);
uBit.serial.baud(115200);
uBit.serial.send("TEST\r\n");
uBit.serial.send("display\r\n");
uBit.display.clear();
uBit.display.print(full);
uBit.display.print(tick);
uBit.sleep(500);
uBit.serial.send("Say Hallo!!\r\n");
uBit.display.scroll("HALLO!!");
uBit.display.print(tick);
// Print the heart
uBit.display.animate(HEART,100,5);
uBit.sleep(100);
// Bring the smiley in
for (int y=4; y >= 0; y--)
{
uBit.display.image.paste(SMILEY,y,0);
uBit.sleep(200);
}
uBit.serial.send("buttons\r\n");
uBit.display.print("A");
while(!pressed_A)
{
for (int y=4; y >=0; y--)
{
uBit.display.image.paste(ARROW_TO_LEFT,y,0);
uBit.sleep(100);
}
}
pressed_A = false;
//Disable the event handler
// uBit.messageBus.ignore(MICROBIT_ID_BUTTON_A, MICROBIT_BUTTON_EVT_CLICK, on_button_A);
uBit.serial.send("A");
uBit.display.print(SMILEY);
uBit.sleep(100);
uBit.display.print(tick);
uBit.display.print("B");
while(!pressed_B)
{
for (int y=0; y < 5; y++)
{
uBit.display.image.paste(ARROW_TO_RIGHT,y,0);
uBit.sleep(100);
}
}
pressed_B = false;
// uBit.messageBus.ignore(MICROBIT_ID_BUTTON_B, MICROBIT_BUTTON_EVT_CLICK, on_button_B);
uBit.serial.send("B");
uBit.display.print(SMILEY);
uBit.sleep(100);
uBit.display.print(tick);
uBit.display.scroll("SUPER");
uint16_t gesture = uBit.accelerometer.getGesture();
while(!(gesture == MICROBIT_ACCELEROMETER_EVT_FACE_UP))
{
uBit.display.print("---");
uBit.sleep(100);
if (pressed_A)
{
counter = counter >= 3 ? 0 : counter + 1;
uBit.display.print("A");
pressed_A = false;
}
if (pressed_B)
{
counter = counter == 0 ? 0 : counter - 1;
uBit.display.print("B");
pressed_B = false;
}
}
switch (counter) {
case 0:
uBit.display.print("G-A");
break;
case 1:
uBit.display.print("G-B");
break;
case 2:
uBit.display.print("G-C");
break;
case 3:
uBit.display.print("G-4");
break;
default:
break;
}
#ifdef false
uBit.serial.send("RGB led\r\n");
uBit.display.print("R");
uBit.rgb.off();
uBit.rgb.setColour(255, 0, 0, 0);
uBit.sleep(500);
uBit.rgb.off();
uBit.rgb.setColour(0, 255, 0, 0);
uBit.sleep(500);
uBit.rgb.off();
uBit.rgb.setColour(0, 0, 255, 0);
uBit.sleep(500);
uBit.rgb.off();
uBit.serial.send("sound\r\n");
uBit.display.print("S");
uBit.soundmotor.Sound_Set_Silent_Mode(true);
uBit.soundmotor.Sound_On(500);
uBit.sleep(100);
uBit.soundmotor.Sound_On(2000);
uBit.sleep(100);
uBit.soundmotor.Sound_On(3000);
uBit.sleep(100);
uBit.soundmotor.Sound_Off();
#endif
uBit.display.clear();
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment