From c79a5b41ce95d930f2ad3cc639d73e72f8c65f9b Mon Sep 17 00:00:00 2001 From: Jasper Date: Thu, 13 Jul 2023 15:39:31 +0200 Subject: [PATCH 01/18] adding demo files from old version --- source/samples/Images.cpp | 223 ++++++++++++++++++++++++ source/samples/Images.h | 30 ++++ source/samples/Localization.h | 11 ++ source/samples/Menu.cpp | 66 +++++++ source/samples/Menu.h | 16 ++ source/samples/RunMultiplication.cpp | 92 ++++++++++ source/samples/RunMultiplication.h | 6 + source/samples/RunOracle.cpp | 74 ++++++++ source/samples/RunOracle.h | 6 + source/samples/RunRockPaperScissors.cpp | 58 ++++++ source/samples/RunRockPaperScissors.h | 6 + source/samples/RunVolumeMeter.cpp | 94 ++++++++++ source/samples/RunVolumeMeter.h | 6 + source/samples/Storage.h | 31 ++++ source/samples/Utils.cpp | 67 +++++++ source/samples/Utils.h | 25 +++ 16 files changed, 811 insertions(+) create mode 100644 source/samples/Images.cpp create mode 100644 source/samples/Images.h create mode 100644 source/samples/Localization.h create mode 100644 source/samples/Menu.cpp create mode 100644 source/samples/Menu.h create mode 100644 source/samples/RunMultiplication.cpp create mode 100644 source/samples/RunMultiplication.h create mode 100644 source/samples/RunOracle.cpp create mode 100644 source/samples/RunOracle.h create mode 100644 source/samples/RunRockPaperScissors.cpp create mode 100644 source/samples/RunRockPaperScissors.h create mode 100644 source/samples/RunVolumeMeter.cpp create mode 100644 source/samples/RunVolumeMeter.h create mode 100644 source/samples/Storage.h create mode 100644 source/samples/Utils.cpp create mode 100644 source/samples/Utils.h diff --git a/source/samples/Images.cpp b/source/samples/Images.cpp new file mode 100644 index 00000000..426ec876 --- /dev/null +++ b/source/samples/Images.cpp @@ -0,0 +1,223 @@ +#include "Images.h" + +//extern MicroBitImage rect_l("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"); + +static uint8_t pixel_full[25] = { + 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1 +}; +static uint8_t pixel_dot[25] = { + 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 +}; +static uint8_t pixel_small[25] = { + 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 +}; +static uint8_t pixel_large[25] = { + 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, +}; +static uint8_t pixel_arrow_left[25] = { + 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 +}; +static uint8_t pixel_arrow_right[25] = { + 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 +}; +static uint8_t pixel_arrow_leftright[25] = { + 0, 0, 1, 0, 0, + 0, 1, 0, 1, 0, + 1, 0, 0, 0, 1, + 0, 1, 0, 1, 0, + 0, 0, 1, 0, 0 +}; +static uint8_t pixel_double_row[25] = { + 0, 1, 1, 0, 0, + 0, 1, 1, 0, 0, + 0, 1, 1, 0, 0, + 0, 1, 1, 0, 0, + 0, 1, 1, 0, 0 +}; +static uint8_t pixel_tick[25] = { + 0, 0, 0, 0, 0, + 0, 0, 0, 0, 1, + 0, 0, 0, 1, 0, + 1, 0, 1, 0, 0, + 0, 1, 0, 0, 0 +}; +static uint8_t pixel_heart[25] = { + 0, 1, 0, 1, 0, + 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, + 0, 1, 1, 1, 0, + 0, 0, 1, 0, 0 +}; +static uint8_t pixel_smiley[25] = { + 0, 1, 0, 1, 0, + 0, 1, 0, 1, 0, + 0, 0, 0, 0, 0, + 1, 0, 0, 0, 1, + 0, 1, 1, 1, 0 +}; +static uint8_t pixel_sadly[25] = { + 0, 1, 0, 1, 0, + 0, 1, 0, 1, 0, + 0, 0, 0, 0, 0, + 0, 1, 1, 1, 0, + 1, 0, 0, 0, 1 +}; +static uint8_t pixel_rock[25] = { + 0, 0, 0, 0, 0, + 0, 1, 1, 1, 0, + 0, 1, 1, 1, 0, + 0, 1, 1, 1, 0, + 0, 0, 0, 0, 0 +}; +static uint8_t pixel_scissors[25] = { + 1, 0, 0, 0, 1, + 0, 1, 0, 1, 0, + 0, 0, 1, 0, 0, + 0, 1, 0, 1, 0, + 1, 0, 0, 0, 1 +}; +static uint8_t pixel_well[25] = { + 0, 1, 1, 1, 0, + 1, 0, 0, 0, 1, + 1, 0, 0, 0, 1, + 1, 0, 0, 0, 1, + 0, 1, 1, 1, 0 +}; +static uint8_t pixel_flash[25] = { + 0, 0, 1, 1, 0, + 0, 1, 1, 0, 0, + 1, 1, 1, 1, 1, + 0, 0, 1, 1, 0, + 0, 1, 1, 0, 0, +}; +static uint8_t pixel_wave[7 * 5] = { + 0, 0, 0, 0, 0, 0, 0, + 0, 1, 1, 0, 0, 0, 0, + 1, 0, 0, 1, 0, 0, 1, + 0, 0, 0, 0, 1, 1, 0, + 0, 0, 0, 0, 0, 0, 0 +}; +static uint8_t pixel_multiplier[25] = { + 0, 0, 0, 0, 0, + 0, 1, 0, 1, 0, + 0, 0, 1, 0, 0, + 0, 1, 0, 1, 0, + 0, 0, 0, 0, 0 +}; + +static uint8_t* changebrightness(uint8_t arr[],uint8_t brightness) { + + for(int i=0;i<25;i++) { + if(arr[i]==1) { + arr[i]=brightness; + } + } + return arr; +} + +MicroBitImage *images(Image_t index){ + static MicroBitImage returnImage; + switch(index){ + case ImageSmiley: { + returnImage = MicroBitImage(5, 5, pixel_smiley); + break; + } + case ImageSadly: { + returnImage = MicroBitImage(5, 5, pixel_sadly); + break; + } + case ImageHeart: { + returnImage = MicroBitImage(5, 5, pixel_heart); + break; + } + case ImageArrowLeft: { + returnImage = MicroBitImage(5, 5, pixel_arrow_left); + break; + } + case ImageArrowRight: { + returnImage = MicroBitImage(5, 5, pixel_arrow_right); + break; + } + case ImageArrowLeftRight: { + returnImage = MicroBitImage(5, 5, pixel_arrow_leftright); + break; + } + case ImageFull: { + returnImage = MicroBitImage(5, 5, pixel_full); + break; + } + case ImageDot: { + returnImage = MicroBitImage(5, 5, pixel_dot); + break; + } + case ImageSmallRect: { + returnImage = MicroBitImage (5, 5, changebrightness(pixel_small,255)); + break; + } + case ImageLargeRect: { + returnImage = MicroBitImage (5, 5, pixel_large); + break; + } + case ImageDoubleRow: { + returnImage = MicroBitImage(5, 5, pixel_double_row); + break; + } + case ImageTick: { + returnImage = MicroBitImage(5, 5, pixel_tick); + break; + } + case ImageRock: { + returnImage = MicroBitImage(5, 5, pixel_rock); + break; + } + case ImageScissors: { + returnImage = MicroBitImage(5, 5, pixel_scissors); + break; + } + case ImageWell: { + + returnImage = MicroBitImage(5, 5, pixel_well); + break; + } + case ImageFlash: { + returnImage = MicroBitImage(5, 5, pixel_flash); + break; + } + case ImageWave: { + returnImage = MicroBitImage(7, 5, pixel_wave); + break; + } + case ImageMultiplier: + returnImage = MicroBitImage(5, 5, pixel_multiplier); + break; + default: + returnImage = MicroBitImage(5, 5, pixel_sadly); + + break; + } + return &returnImage; +} \ No newline at end of file diff --git a/source/samples/Images.h b/source/samples/Images.h new file mode 100644 index 00000000..aef31611 --- /dev/null +++ b/source/samples/Images.h @@ -0,0 +1,30 @@ +#ifndef IMAGES_H +#define IMAGES_H + +#include "MicroBitImage.h" + +enum Image_t{ + ImageSmiley, + ImageSadly, + ImageHeart, + ImageArrowLeft, + ImageArrowRight, + ImageArrowLeftRight, + ImageFull, + ImageDot, + ImageSmallRect, + ImageLargeRect, + ImageDoubleRow, + ImageTick, + ImageRock, + ImageScissors, + ImageWell, + ImageFlash, + ImageWave, + ImageMultiplier, + ImageCOUNTER +}; + +MicroBitImage *images(Image_t index); + +#endif // IMAGES_H \ No newline at end of file diff --git a/source/samples/Localization.h b/source/samples/Localization.h new file mode 100644 index 00000000..88f40605 --- /dev/null +++ b/source/samples/Localization.h @@ -0,0 +1,11 @@ +#ifndef LOCALIZATION_H +#define LOCALIZATION_H + +#define LS_DEMO_HELLO "Hi!" +#define LS_DEMO_BUTTON_A "A" +#define LS_DEMO_BUTTON_B "B" +#define LS_DEMO_BUTTON_AB "A+B" +//#define LS_DEMO_SHAKE "SCHUETTELN" +#define LS_DEMO_GREAT "OK!" + +#endif // LOCALIZATION_H \ No newline at end of file diff --git a/source/samples/Menu.cpp b/source/samples/Menu.cpp new file mode 100644 index 00000000..95c81bd5 --- /dev/null +++ b/source/samples/Menu.cpp @@ -0,0 +1,66 @@ +#include "Menu.h" +#include "MicroBit.h" + +extern MicroBit uBit; + + +menustate_t menuWaitForChoice(menustate_t start) +{ + menustate_t state = start; + while (true) { + + // if (state == MenuStateInterpreter) { + // showNameHistogram(uBit.display); + // // static const uint8_t pixels[25] = { + // // 0, 0, 1, 1, 1, + // // 0, 0, 0, 1, 1, + // // 0, 0, 1, 0, 1, + // // 0, 1, 0, 0, 0, + // // 1, 0, 0, 0, 0 + // // }; + // // const MicroBitImage Image(5, 5, pixels); + // // uBit.display.print(Image); + // } else { + // uBit.display.print(ManagedString(state)); + // } + + uBit.display.print(ManagedString(state)); + + // event loop + while (true) { + // button A + if (uBit.buttonA.isPressed()) { + while (uBit.buttonA.isPressed()) { + uBit.sleep(10); + } + + if (state > MenuStateMin) { + state = menustate_t(state - 1); + break; + } + } + + // button B + if (uBit.buttonB.isPressed()) { + while (uBit.buttonB.isPressed()) { + uBit.sleep(10); + } + + if (state < MenuStateMax) { + state = menustate_t(state + 1); + break; + } + } + + // gesture + if (uBit.accelerometer.getGesture() == MICROBIT_ACCELEROMETER_EVT_SHAKE) { + while (uBit.accelerometer.getGesture() == MICROBIT_ACCELEROMETER_EVT_SHAKE) { + uBit.sleep(10); + } + return state; + } + + uBit.sleep(10); + } + } +} \ No newline at end of file diff --git a/source/samples/Menu.h b/source/samples/Menu.h new file mode 100644 index 00000000..cfa3b0e2 --- /dev/null +++ b/source/samples/Menu.h @@ -0,0 +1,16 @@ +#ifndef MENU_H +#define MENU_H + +typedef enum MenuState { + MenuStateOracle = 1, + MenuStateRockPaperScissors, + MenuStateMultiplication, + MenuStateVolumeMeter, + MenuStateInterpreter, + MenuStateMin = MenuStateOracle, + MenuStateMax = MenuStateInterpreter +} menustate_t; + +menustate_t menuWaitForChoice(menustate_t start); + +#endif // MENU_H \ No newline at end of file diff --git a/source/samples/RunMultiplication.cpp b/source/samples/RunMultiplication.cpp new file mode 100644 index 00000000..e94457a3 --- /dev/null +++ b/source/samples/RunMultiplication.cpp @@ -0,0 +1,92 @@ +#include "RunMultiplication.h" +#include "MicroBit.h" +#include "Utils.h" +#include "Images.h" + +extern MicroBit uBit; + +static int factor1 = 0; +static int factor2 = 0; +static bool ready = false; + +static void factor1Handler(MicroBitEvent) +{ + factor1 = uBit.random(10); + uBit.display.print(factor1); + + ready = false; +} + +static void factor2Handler(MicroBitEvent) +{ + uBit.display.print(*images(ImageMultiplier)); + uBit.sleep(300); + + factor2 = uBit.random(10); + uBit.display.print(factor2); + + ready = true; +} + +static void helpHandler(MicroBitEvent) +{ + if (!ready) { + return; + } + + int result = factor1 * factor2; + uBit.display.scroll(result); + uBit.sleep(300); + + uBit.display.print(*images(ImageArrowLeft)); +} + +void multiplication_run() { + + uBit.messageBus.listen( + MICROBIT_ID_BUTTON_AB, + MICROBIT_BUTTON_EVT_CLICK, + leaveHandler); + + uBit.messageBus.listen( + MICROBIT_ID_BUTTON_A, + MICROBIT_BUTTON_EVT_CLICK, + factor1Handler); + + uBit.messageBus.listen( + MICROBIT_ID_BUTTON_B, + MICROBIT_BUTTON_EVT_CLICK, + factor2Handler); + + uBit.messageBus.listen( + MICROBIT_ID_GESTURE, + MICROBIT_ACCELEROMETER_EVT_SHAKE, + helpHandler); + + uBit.display.print(*images(ImageArrowLeft)); + + leave = false; + while(!leave) { + uBit.sleep(100); + } + + uBit.messageBus.ignore( + MICROBIT_ID_BUTTON_AB, + MICROBIT_BUTTON_EVT_CLICK, + leaveHandler); + + uBit.messageBus.ignore( + MICROBIT_ID_BUTTON_A, + MICROBIT_BUTTON_EVT_CLICK, + factor1Handler); + + uBit.messageBus.ignore( + MICROBIT_ID_BUTTON_B, + MICROBIT_BUTTON_EVT_CLICK, + factor2Handler); + + uBit.messageBus.ignore( + MICROBIT_ID_GESTURE, + MICROBIT_ACCELEROMETER_EVT_SHAKE, + helpHandler); +} \ No newline at end of file diff --git a/source/samples/RunMultiplication.h b/source/samples/RunMultiplication.h new file mode 100644 index 00000000..9820f7d9 --- /dev/null +++ b/source/samples/RunMultiplication.h @@ -0,0 +1,6 @@ +#ifndef RUN_MULTIPLICATION_H +#define RUN_MULTIPLICATION_H + +void multiplication_run(); + +#endif // RUN_MULTIPLICATION_H \ No newline at end of file diff --git a/source/samples/RunOracle.cpp b/source/samples/RunOracle.cpp new file mode 100644 index 00000000..fb7797f0 --- /dev/null +++ b/source/samples/RunOracle.cpp @@ -0,0 +1,74 @@ +#include "RunOracle.h" +#include "MicroBit.h" +#include "Images.h" +#include "Utils.h" + +extern MicroBit uBit; + +void oracle_run() +{ + uBit.messageBus.listen( + MICROBIT_ID_BUTTON_AB, + MICROBIT_BUTTON_EVT_CLICK, + leaveHandler); + uBit.messageBus.listen( + MICROBIT_ID_BUTTON_A, + MICROBIT_BUTTON_EVT_CLICK, + eventHandler); + + leave = false; + while(!leave) { + + event = false; + + leave = false; + while(!leave) { + + uBit.display.print(*images(ImageArrowLeft)); + + for(uint32_t t=0, i=10; t<400 && !leave && !event; t+=i) { + uBit.sleep(i); + } + + if (leave || event) { + break; + } + + uBit.sleep(10); + uBit.display.clear(); + + for(uint32_t t=0, i=10; t<400 && !leave && !event; t+=i) { + uBit.sleep(i); + } + } + + if (leave) { + break; + } + + for (int i = 0; i < 5; i++) { + uBit.display.print(*images(ImageDot)); + uBit.sleep(200); + uBit.display.clear(); + uBit.sleep(50); + } + + int r = uBit.random(100); + uBit.display.print((r < 50) + ? *images(ImageSmiley) + : *images(ImageSadly) + ); + + uBit.sleep(3000); + uBit.display.clear(); + } + + uBit.messageBus.ignore( + MICROBIT_ID_BUTTON_AB, + MICROBIT_BUTTON_EVT_CLICK, + leaveHandler); + uBit.messageBus.ignore( + MICROBIT_ID_BUTTON_A, + MICROBIT_BUTTON_EVT_CLICK, + eventHandler); +} \ No newline at end of file diff --git a/source/samples/RunOracle.h b/source/samples/RunOracle.h new file mode 100644 index 00000000..92f7dbbf --- /dev/null +++ b/source/samples/RunOracle.h @@ -0,0 +1,6 @@ +#ifndef RUN_ORACLE_H +#define RUN_ORACLE_H + +void oracle_run(); + +#endif // RUN_ORACLE_H \ No newline at end of file diff --git a/source/samples/RunRockPaperScissors.cpp b/source/samples/RunRockPaperScissors.cpp new file mode 100644 index 00000000..6f865ced --- /dev/null +++ b/source/samples/RunRockPaperScissors.cpp @@ -0,0 +1,58 @@ +#include "RunRockPaperScissors.h" +#include "Images.h" +#include "MicroBit.h" +#include "Utils.h" + +extern MicroBit uBit; + +void rockpaperscissors_run() +{ + const uint16_t pause = 200; + + uBit.messageBus.listen( + MICROBIT_ID_BUTTON_AB, + MICROBIT_BUTTON_EVT_CLICK, + leaveHandler); + + uBit.display.print(*images(ImageRock), 0, 0, 0, pause * 2); + uBit.display.print(*images(ImageFull), 0, 0, 0, pause * 2); + uBit.display.print(*images(ImageScissors), 0, 0, 0, pause * 2); + uBit.display.print(*images(ImageWell), 0, 0, 0, pause * 2); + uBit.display.clear(); + uBit.sleep(pause); + + leave = false; + while (!leave) { + + blinkImageUntilEvent( + MICROBIT_ID_GESTURE, + MICROBIT_ACCELEROMETER_EVT_SHAKE, + *images(ImageDoubleRow), + -1, + 2, + 100); + + if (leave) { + break; + } + + int r = uBit.random(400); + if (r < 100) { + uBit.display.print(*images(ImageRock)); + } else if (r < 200) { + uBit.display.print(*images(ImageFull)); + } else if (r < 300) { + uBit.display.print(*images(ImageScissors)); + } else { + uBit.display.print(*images(ImageWell)); + } + + uBit.sleep(3000); + uBit.display.clear(); + } + + uBit.messageBus.ignore( + MICROBIT_ID_BUTTON_AB, + MICROBIT_BUTTON_EVT_CLICK, + leaveHandler); +} \ No newline at end of file diff --git a/source/samples/RunRockPaperScissors.h b/source/samples/RunRockPaperScissors.h new file mode 100644 index 00000000..a2d147c0 --- /dev/null +++ b/source/samples/RunRockPaperScissors.h @@ -0,0 +1,6 @@ +#ifndef RUN_ROCK_PAPER_SCISSORS_H +#define RUN_ROCK_PAPER_SCISSORS_H + +void rockpaperscissors_run(); + +#endif // RUN_ROCK_PAPER_SCISSORS_H \ No newline at end of file diff --git a/source/samples/RunVolumeMeter.cpp b/source/samples/RunVolumeMeter.cpp new file mode 100644 index 00000000..0022e97f --- /dev/null +++ b/source/samples/RunVolumeMeter.cpp @@ -0,0 +1,94 @@ +#include +#include "RunVolumeMeter.h" +#include "MicroBit.h" +#include "Utils.h" +// #include "neopixels.h" + +extern MicroBit uBit; + +const int threshold = 519; + +static int read() +{ + int mic = 0; + const int n = 30; + + uBit.io.runmic.setDigitalValue(1); + uBit.io.runmic.setHighDrive(true); + + for (uint8_t i = 0; i < n; i++) { + // assign new Pin of microphone? + const int v = uBit.io.microphone.getAnalogValue(); + if (v > mic) mic = v; + } + + if (mic < threshold) { + return 0; + } + + // mic 512...900 ~> 0...5 + const int gauge = static_cast((log2(mic - threshold + 1) * 5) / 8); + + // uBit.serial.printf("m:%d -> g:%d\n", mic, gauge); + + if (gauge < 0) { + return 0; + } + + if (gauge > 5) { + return 5; + } + + return gauge; +} + +void volumemeter_run() +{ + uBit.messageBus.listen( + MICROBIT_ID_BUTTON_AB, + MICROBIT_BUTTON_EVT_CLICK, + leaveHandler); + + int gauges[5] = {}; + + leave = false; + while (!leave) { + + // shift values + for (uint8_t i = 0; i < (5-1); i++) { + gauges[i] = gauges[i+1]; + } + + gauges[4] = read(); + + for (uint16_t x = 0; x < 5; x++) { + const int gauge = gauges[x]; + for (uint16_t y = 0; y < 5; y++) { + const uint8_t t = static_cast(gauge > y ? 255 : 0); + uBit.display.image.setPixelValue(x, 4-y, t); + } + } + + int sum = 0; + for (uint8_t i = 0; i < 5; i++) { + sum += gauges[i]; + } + + if (sum > 5*2.5) { + // neopixels + // uBit.rgb.setColour(0xFF, 0x00, 0x00, 0x00); + } else { + //neopixels + // uBit.rgb.setColour(0x00, 0xFF, 0x00, 0x00); + } + + uBit.sleep(100); + } + + // uBit.rgb.off(); + + uBit.messageBus.ignore( + MICROBIT_ID_BUTTON_AB, + MICROBIT_BUTTON_EVT_CLICK, + leaveHandler); +} \ No newline at end of file diff --git a/source/samples/RunVolumeMeter.h b/source/samples/RunVolumeMeter.h new file mode 100644 index 00000000..276e5fee --- /dev/null +++ b/source/samples/RunVolumeMeter.h @@ -0,0 +1,6 @@ +#ifndef RUN_VOLUME_METER_H +#define RUN_VOLUME_METER_H + +void volumemeter_run(); + +#endif // RUN_VOLUME_METER_H \ No newline at end of file diff --git a/source/samples/Storage.h b/source/samples/Storage.h new file mode 100644 index 00000000..675a578f --- /dev/null +++ b/source/samples/Storage.h @@ -0,0 +1,31 @@ +#ifndef STORAGE_H +#define STORAGE_H + +#define KEY_TEST "initial" +#define KEY_DEMO "demo" +#define KEY_INTERPRETER "interpreter" + +extern MicroBit uBit; + +inline bool hasStorageKey(const char* name) +{ + void* value = uBit.storage.get(name); + if (value != NULL) { + free(value); + return true; + } else { + return false; + } +} + +inline void setStorageKey(const char* name, uint8_t value = 1) +{ + uBit.storage.put(name, &value, sizeof(uint8_t)); +} + +inline void removeStorageKey(const char* name) +{ + uBit.storage.remove(name); +} + +#endif // STORAGE_H \ No newline at end of file diff --git a/source/samples/Utils.cpp b/source/samples/Utils.cpp new file mode 100644 index 00000000..a331d1f7 --- /dev/null +++ b/source/samples/Utils.cpp @@ -0,0 +1,67 @@ +#include "Utils.h" +#include "MicroBit.h" +#include "MicroBitImage.h" + +extern MicroBit uBit; + +bool event = false; +void eventHandler(MicroBitEvent) +{ + event = true; +} + +bool leave = false; +void leaveHandler(MicroBitEvent) +{ + leave = true; +} + +void blinkImage( + const MicroBitImage& image, + const int pos, + const uint16_t delay) +{ + + event = false; + + uBit.display.print(image, pos); + + for (uint32_t t = 0, i = 10; t < delay && !leave && !event; t += i) { + uBit.sleep(i); + } + + if (event) { + return; + } + + uBit.display.clear(); + + for (uint32_t t = 0, i = 10; t < delay && !leave && !event; t += i) { + uBit.sleep(i); + } +} + +void blinkImageUntilEvent( + const uint16_t source, + const uint16_t value, + const MicroBitImage& image, + const int pos1, + const int pos2, + const uint16_t delay) +{ + uBit.messageBus.listen( + source, + value, + eventHandler); + + event = false; + + for (uint8_t i = 0; !leave && !event; i++) { + blinkImage(image, (i % 2) == 0 ? pos1 : pos2, delay); + } + + uBit.messageBus.ignore( + source, + value, + eventHandler); +} \ No newline at end of file diff --git a/source/samples/Utils.h b/source/samples/Utils.h new file mode 100644 index 00000000..e18c2c7b --- /dev/null +++ b/source/samples/Utils.h @@ -0,0 +1,25 @@ +#ifndef UTILS_H +#define UTILS_H + +#include "MicroBit.h" + +extern bool event; +void leaveHandler(MicroBitEvent); + +extern bool leave; +void eventHandler(MicroBitEvent); + +void blinkImage( + const MicroBitImage& image, + const int pos = 0, + const uint16_t delay = 200); + +void blinkImageUntilEvent( + const uint16_t source, + const uint16_t value, + const MicroBitImage& image, + const int pos1 = 0, + const int pos2 = 0, + const uint16_t delay = 200); + +#endif // UTILS_H \ No newline at end of file From 36297123c373fd4bb1abd0ae7338b6b075e56ba6 Mon Sep 17 00:00:00 2001 From: Jasper Date: Wed, 19 Jul 2023 10:55:04 +0200 Subject: [PATCH 02/18] updating demo --- source/main.cpp | 310 +++++++++++++++++++++++- source/samples/Images.cpp | 22 +- source/samples/Images.h | 16 ++ source/samples/RunMultiplication.cpp | 6 +- source/samples/RunOracle.cpp | 8 +- source/samples/RunRockPaperScissors.cpp | 24 +- 6 files changed, 363 insertions(+), 23 deletions(-) diff --git a/source/main.cpp b/source/main.cpp index 2022bb7c..7689694e 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -1,14 +1,318 @@ #include "MicroBit.h" -#include "samples/Tests.h" +#include "Storage.h" +#include "Images.h" +// #include "RunTests.h" +// #include "RunDemo.h" +#include "Menu.h" +#include "RunOracle.h" +#include "RunRockPaperScissors.h" +#include "RunMultiplication.h" +#include "RunVolumeMeter.h" +#include "Utils.h" +#include "Tests.h" +// #include "neopixel.h" +// #include "Interpreter.h" +// #include "nrf.h" MicroBit uBit; +// static void showNameHistogram(MicroBitDisplay &display) +// { +// NRF_FICR_Type *ficr = NRF_FICR; +// uint32_t n = ficr->DEVICEID[1]; +// uint32_t ld = 1; +// uint32_t d = MICROBIT_DFU_HISTOGRAM_HEIGHT; +// uint32_t h; + +// display.clear(); +// for (uint32_t i = 0; i < MICROBIT_DFU_HISTOGRAM_WIDTH; i++) +// { +// h = (n % d) / ld; + +// n -= h; +// d *= MICROBIT_DFU_HISTOGRAM_HEIGHT; +// ld *= MICROBIT_DFU_HISTOGRAM_HEIGHT; + +// for (uint32_t j = 0; j < h + 1; j++) +// display.image.setPixelValue( +// static_cast(MICROBIT_DFU_HISTOGRAM_WIDTH - i - 1), +// static_cast(MICROBIT_DFU_HISTOGRAM_HEIGHT - j - 1), +// 255); +// } +// } + +static bool mute = false; +// Length of neopixel strip +const int strip_length = 3; + +// --------------------------- +static void playsound(int freq) +{ + if (mute || freq == 0) + { + uBit.io.speaker.setAnalogValue(0); + uBit.io.P0.setAnalogValue(0); + return; + } + uBit.io.speaker.setHighDrive(true); + uBit.io.speaker.setAnalogValue(512); + uBit.io.P0.setHighDrive(true); + uBit.io.P0.setAnalogValue(512); + int period = 1000000.0 / (float)freq; + + uBit.io.speaker.setAnalogPeriodUs(period); + uBit.io.P0.setAnalogPeriodUs(period); + return; +} + +static int readMic() +{ + int v; + uBit.audio.activateMic(); + uBit.sleep(100); // seems to be necessary + v = uBit.audio.levelSPL->getValue(); // what is SPL? + uBit.serial.printf("%d \n", v); + uBit.sleep(100); + return v; +} + +static inline void waitForever() +{ + while (true) + { + uBit.sleep(1000); + } +} + +static void beep() +{ + playsound(784); + uBit.sleep(125); + playsound(0); +} + +static void startSound() +{ + playsound(262); + uBit.sleep(125); + playsound(0); + uBit.sleep(63); + playsound(784); + uBit.sleep(500); + playsound(0); +} + +// static void writeRgbLeds(const uint8_t led0r, const uint8_t led0g, const uint8_t led0b, +// const uint8_t led1r, const uint8_t led1g, const uint8_t led1b, +// const uint8_t led2r, const uint8_t led2g, const uint8_t led2b) +// { +// // Create a buffer to hold pixel data of the appropriate size +// ManagedBuffer b(strip_length * 3); +// b.fill(0); +// // b[0 + 3 * index] = ledg; +// // b[1 + 3 * index] = ledr; +// // b[2 + 3 * index] = ledb; + +// b[0] = led0g; // turns 1st LED green +// b[1] = led0r; +// b[2] = led0b; +// b[3] = led1g; +// b[4] = led1r; // turns 2nd LED red +// b[5] = led1b; +// b[6] = led2g; +// b[7] = led2r; +// b[8] = led2b; // turns 3rd LED blue +// neopixel_send_buffer(uBit.io.RGB, b); +// } + +void neopixel_write() +{ + // Brightness of pixels + int brightness = 50; + + // Create a buffer to hold pixel data of the appropriate size + ManagedBuffer b(strip_length * 3); + + // Fill the buffer with some data. + // Neopixel typically has 3 bytes per pixel, for each primary colour + // in GRB format (Green, then Red, then Blue). + for (int i = 0; i < strip_length * 3; i++) + { + // Clear the buffer to zeroes. You don't need to do this, but it's often convenient. + b.fill(0); + b[i] = brightness; + neopixel_send_buffer(uBit.io.RGB, b); + // c = (c + 1) % 3; + uBit.sleep(100); + // Update the neopixel strip on pin P0. + } + for (int i = 0; i < strip_length; i++) + { + b.fill(0); + b[0 + i] = brightness; + b[3 + i] = brightness; + b[6 + i] = brightness; + neopixel_send_buffer(uBit.io.RGB, b); + // c = (c + 1) % 3; + uBit.sleep(300); + // Update the neopixel strip on pin P0. + blinkImage(herz, 0, 300); + } + + // Increment our colour counter so that we show a different colour in the next frame. + + // Wait for a second. +} + +static void disableRgbLeds() +{ + // Create a buffer to hold pixel data of the appropriate size + ManagedBuffer b(strip_length * 3); + b.fill(0); + neopixel_send_buffer(uBit.io.RGB, b); +} + +static void menuAnimateEnter() +{ + uBit.display.print(dot); + uBit.sleep(200); + uBit.display.print(rect_s); + uBit.sleep(200); + uBit.display.print(rect_l); + uBit.sleep(200); + uBit.display.clear(); +} + +static void menuAnimateLeave() +{ + uBit.display.print(rect_l); + uBit.sleep(200); + uBit.display.print(rect_s); + uBit.sleep(200); + uBit.display.print(dot); + uBit.sleep(200); + uBit.display.clear(); +} + +const uint16_t pause = 300; + + +// fire a accelerometer shake event with a 3G event +static void shakeEvent(MicroBitEvent) +{ + MicroBitEvent evt(MICROBIT_ID_GESTURE, MICROBIT_ACCELEROMETER_EVT_SHAKE); + uBit.messageBus.send(evt); +} + int main() { + + // uBit.accelerometer.updateSample(); uBit.init(); + // fire a accelerometer shake event with a 3G event + uBit.messageBus.listen(MICROBIT_ID_GESTURE, MICROBIT_ACCELEROMETER_EVT_3G, shakeEvent); + + uBit.display.setBrightness(100); + + uBit.serial.send("Calliope Demo v3.0\r\n"); + + //check if DEMO has already been played + // if (!hasStorageKey(KEY_DEMO)) + // { + // setStorageKey(KEY_DEMO); + + // begin of welcome DEMO program + startSound(); + // mems_mic_test(); + // start state + + uBit.display.scroll("Hi"); + + // press A + uBit.display.print("A"); + uBit.sleep(pause); + blinkImageUntilEvent(MICROBIT_ID_BUTTON_A, MICROBIT_BUTTON_EVT_CLICK, arrow_left); + uBit.display.print(check); + uBit.sleep(pause); + uBit.display.clear(); + + // press B + uBit.display.print("B"); + uBit.sleep(pause); + blinkImageUntilEvent(MICROBIT_ID_BUTTON_B, MICROBIT_BUTTON_EVT_CLICK, arrow_right); + uBit.display.print(check); + uBit.sleep(pause); + uBit.display.clear(); + + // press A+B + uBit.display.scroll("A+B"); + uBit.sleep(pause); + blinkImageUntilEvent(MICROBIT_ID_BUTTON_AB, MICROBIT_BUTTON_EVT_CLICK, arrow_left_right); + beep(); + uBit.display.print(check); + uBit.sleep(pause); + uBit.display.clear(); + + // shake + // uBit.display.scroll(LS_DEMO_SHAKE); + blinkImageUntilEvent(MICROBIT_ID_GESTURE, MICROBIT_ACCELEROMETER_EVT_SHAKE, doublerow, -1, + 2, 100); + uBit.display.print(check); + uBit.sleep(pause); + uBit.display.clear(); + + uBit.display.scroll("OK!"); + + // RGB LEDs + neopixel_write(); + disableRgbLeds(); + // } // end storage key DEMO + + menustate_t state = MenuStateOracle; - out_of_box_experience(); + while (true) + { + // for(int i = 0; i<100; i++) { + // uBit.sleep(10); + // threeLEDs(0,0,0,i,i,i,0,0,0); + // } + // const int v = uBit.io.microphone.getAnalogValue(); + // uBit.serial.send(); + // uBit.serial.printf("%d \n",readMic()); - microbit_panic( 999 ); + state = menuWaitForChoice(state); + switch (state) + { + // 1 + case MenuStateOracle: + menuAnimateEnter(); + oracle_run(); + menuAnimateLeave(); + break; + // 2 + case MenuStateRockPaperScissors: + menuAnimateEnter(); + rockpaperscissors_run(); + menuAnimateLeave(); + break; + // 3 + case MenuStateMultiplication: + menuAnimateEnter(); + multiplication_run(); + menuAnimateLeave(); + break; + // 4 + case MenuStateVolumeMeter: + menuAnimateEnter(); + volumemeter_run(); + menuAnimateLeave(); + break; + // 5 + case MenuStateInterpreter: + setStorageKey(KEY_INTERPRETER); + uBit.reset(); + break; + } + } } diff --git a/source/samples/Images.cpp b/source/samples/Images.cpp index 426ec876..790ed50f 100644 --- a/source/samples/Images.cpp +++ b/source/samples/Images.cpp @@ -1,6 +1,26 @@ #include "Images.h" -//extern MicroBitImage rect_l("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 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 sad_smiley("0,255,0,255,0\n0,255,0,255,0\n0,0,0,0,0\n0,255,255,255,0\n255,0,0,0,255\n"); +MicroBitImage rect_l( + "255,255,255,255,255\n255,0,0,0,255\n255,0,0,0,255\n255,0,0,0,255\n255,255,255,255,255\n"); +MicroBitImage rect_s("0,0,0,0,0,\n0,255,255,255,0\n0,255,0,255,0\n0,255,255,255,0\n0,0,0,0,0\n"); +MicroBitImage dot("0,0,0,0,0\n0,0,0,0,0\n0,0,255,0,0\n0,0,0,0,0\n0,0,0,0,0\n"); +MicroBitImage + arrow_left("0,0,255,0,0\n0,255,0,0,0\n255,255,255,255,255\n0,255,0,0,0\n0,0,255,0,0\n"); +MicroBitImage + arrow_right("0,0,255,0,0\n0,0,0,255,0\n255,255,255,255,255\n0,0,0,255,0\n0,0,255,0,0\n"); +MicroBitImage + arrow_left_right("0,0,255,0,0\n0,255,0,255,0\n255,0,0,0,255\n0,255,0,255,0\n0,0,255,0,0\n"); +MicroBitImage check("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"); +MicroBitImage + doublerow("0,255,255,0,0\n0,255,255,0,0\n0,255,255,0,0\n0,255,255,0,0\n0,255,255,0,0\n"); +MicroBitImage herz("0,255,0,255,0\n255,255,255,255,255\n255,255,255,255,255\n0,255,255,255,0\n0,0,255,0,0\n"); +MicroBitImage rock("0,0,0,0,0\n0,255,255,255,0\n0,255,255,255,0\n0,255,255,255,0\n0,0,0,0,0\n"); +MicroBitImage paper("255,255,255,255,255\n255,0,0,0,255\n255,0,0,0,255\n255,0,0,0,255\n255,255,255,255,255\n"); +MicroBitImage scissor("255,255,0,0,255\n255,255,0,255,0\n0,0,255,0,0\n0,0,0,255,255\n0,0,0,255,255\n"); +MicroBitImage multiply("0,0,0,0,0\n0,255,0,255,0\n0,0,255,0,0\n0,255,0,255,0\n0,0,0,0,0\n"); + static uint8_t pixel_full[25] = { 1, 1, 1, 1, 1, diff --git a/source/samples/Images.h b/source/samples/Images.h index aef31611..e2b10bb1 100644 --- a/source/samples/Images.h +++ b/source/samples/Images.h @@ -3,6 +3,22 @@ #include "MicroBitImage.h" +extern MicroBitImage smiley; +extern MicroBitImage sad_smiley; +extern MicroBitImage rect_l; +extern MicroBitImage rect_s; +extern MicroBitImage dot; +extern MicroBitImage arrow_left; +extern MicroBitImage arrow_right; +extern MicroBitImage arrow_left_right; +extern MicroBitImage check; +extern MicroBitImage doublerow; +extern MicroBitImage herz; +extern MicroBitImage rock; +extern MicroBitImage paper; +extern MicroBitImage scissor; +extern MicroBitImage multiply; + enum Image_t{ ImageSmiley, ImageSadly, diff --git a/source/samples/RunMultiplication.cpp b/source/samples/RunMultiplication.cpp index e94457a3..18a7b597 100644 --- a/source/samples/RunMultiplication.cpp +++ b/source/samples/RunMultiplication.cpp @@ -19,7 +19,7 @@ static void factor1Handler(MicroBitEvent) static void factor2Handler(MicroBitEvent) { - uBit.display.print(*images(ImageMultiplier)); + uBit.display.print(multiply); uBit.sleep(300); factor2 = uBit.random(10); @@ -38,7 +38,7 @@ static void helpHandler(MicroBitEvent) uBit.display.scroll(result); uBit.sleep(300); - uBit.display.print(*images(ImageArrowLeft)); + uBit.display.print(arrow_left); } void multiplication_run() { @@ -63,7 +63,7 @@ void multiplication_run() { MICROBIT_ACCELEROMETER_EVT_SHAKE, helpHandler); - uBit.display.print(*images(ImageArrowLeft)); + uBit.display.print(arrow_left); leave = false; while(!leave) { diff --git a/source/samples/RunOracle.cpp b/source/samples/RunOracle.cpp index fb7797f0..8e31f6e9 100644 --- a/source/samples/RunOracle.cpp +++ b/source/samples/RunOracle.cpp @@ -24,7 +24,7 @@ void oracle_run() leave = false; while(!leave) { - uBit.display.print(*images(ImageArrowLeft)); + uBit.display.print(arrow_left); for(uint32_t t=0, i=10; t<400 && !leave && !event; t+=i) { uBit.sleep(i); @@ -47,7 +47,7 @@ void oracle_run() } for (int i = 0; i < 5; i++) { - uBit.display.print(*images(ImageDot)); + uBit.display.print(dot); uBit.sleep(200); uBit.display.clear(); uBit.sleep(50); @@ -55,8 +55,8 @@ void oracle_run() int r = uBit.random(100); uBit.display.print((r < 50) - ? *images(ImageSmiley) - : *images(ImageSadly) + ? smiley + : sad_smiley ); uBit.sleep(3000); diff --git a/source/samples/RunRockPaperScissors.cpp b/source/samples/RunRockPaperScissors.cpp index 6f865ced..f6a93697 100644 --- a/source/samples/RunRockPaperScissors.cpp +++ b/source/samples/RunRockPaperScissors.cpp @@ -14,10 +14,10 @@ void rockpaperscissors_run() MICROBIT_BUTTON_EVT_CLICK, leaveHandler); - uBit.display.print(*images(ImageRock), 0, 0, 0, pause * 2); - uBit.display.print(*images(ImageFull), 0, 0, 0, pause * 2); - uBit.display.print(*images(ImageScissors), 0, 0, 0, pause * 2); - uBit.display.print(*images(ImageWell), 0, 0, 0, pause * 2); + uBit.display.print(rock, 0, 0, 0, pause * 2); + uBit.display.print(paper, 0, 0, 0, pause * 2); + uBit.display.print(scissor, 0, 0, 0, pause * 2); + // uBit.display.print(*images(ImageWell), 0, 0, 0, pause * 2); uBit.display.clear(); uBit.sleep(pause); @@ -27,7 +27,7 @@ void rockpaperscissors_run() blinkImageUntilEvent( MICROBIT_ID_GESTURE, MICROBIT_ACCELEROMETER_EVT_SHAKE, - *images(ImageDoubleRow), + doublerow, -1, 2, 100); @@ -36,15 +36,15 @@ void rockpaperscissors_run() break; } - int r = uBit.random(400); + int r = uBit.random(300); if (r < 100) { - uBit.display.print(*images(ImageRock)); + uBit.display.print(rock); } else if (r < 200) { - uBit.display.print(*images(ImageFull)); - } else if (r < 300) { - uBit.display.print(*images(ImageScissors)); - } else { - uBit.display.print(*images(ImageWell)); + uBit.display.print(paper); + } else { + uBit.display.print(scissor); + // } else { + // uBit.display.print(*images(ImageWell)); } uBit.sleep(3000); From d21deeabbe38d0b5b7dbd20a5510d0daa523a300 Mon Sep 17 00:00:00 2001 From: Jasper Date: Thu, 20 Jul 2023 13:58:53 +0200 Subject: [PATCH 03/18] Updating Demo - adding LED functions - volumeter running --- source/main.cpp | 242 ++++++++++-------------------- source/samples/Menu.cpp | 3 +- source/samples/MicrophoneTest.cpp | 2 +- source/samples/RunVolumeMeter.cpp | 116 ++++++++------ source/samples/Utils.cpp | 184 ++++++++++++++++++++--- source/samples/Utils.h | 14 ++ 6 files changed, 322 insertions(+), 239 deletions(-) diff --git a/source/main.cpp b/source/main.cpp index 7689694e..222f8b81 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -16,36 +16,37 @@ MicroBit uBit; -// static void showNameHistogram(MicroBitDisplay &display) -// { -// NRF_FICR_Type *ficr = NRF_FICR; -// uint32_t n = ficr->DEVICEID[1]; -// uint32_t ld = 1; -// uint32_t d = MICROBIT_DFU_HISTOGRAM_HEIGHT; -// uint32_t h; - -// display.clear(); -// for (uint32_t i = 0; i < MICROBIT_DFU_HISTOGRAM_WIDTH; i++) -// { -// h = (n % d) / ld; - -// n -= h; -// d *= MICROBIT_DFU_HISTOGRAM_HEIGHT; -// ld *= MICROBIT_DFU_HISTOGRAM_HEIGHT; - -// for (uint32_t j = 0; j < h + 1; j++) -// display.image.setPixelValue( -// static_cast(MICROBIT_DFU_HISTOGRAM_WIDTH - i - 1), -// static_cast(MICROBIT_DFU_HISTOGRAM_HEIGHT - j - 1), -// 255); -// } -// } - static bool mute = false; // Length of neopixel strip -const int strip_length = 3; +const uint16_t pause = 300; // --------------------------- + +static void showNameHistogram(MicroBitDisplay &display) +{ + NRF_FICR_Type *ficr = NRF_FICR; + uint32_t n = ficr->DEVICEID[1]; + uint32_t ld = 1; + uint32_t d = MICROBIT_DFU_HISTOGRAM_HEIGHT; + uint32_t h; + + display.clear(); + for (uint32_t i = 0; i < MICROBIT_DFU_HISTOGRAM_WIDTH; i++) + { + h = (n % d) / ld; + + n -= h; + d *= MICROBIT_DFU_HISTOGRAM_HEIGHT; + ld *= MICROBIT_DFU_HISTOGRAM_HEIGHT; + + for (uint32_t j = 0; j < h + 1; j++) + display.image.setPixelValue( + static_cast(MICROBIT_DFU_HISTOGRAM_WIDTH - i - 1), + static_cast(MICROBIT_DFU_HISTOGRAM_HEIGHT - j - 1), + 255); + } +} + static void playsound(int freq) { if (mute || freq == 0) @@ -65,17 +66,6 @@ static void playsound(int freq) return; } -static int readMic() -{ - int v; - uBit.audio.activateMic(); - uBit.sleep(100); // seems to be necessary - v = uBit.audio.levelSPL->getValue(); // what is SPL? - uBit.serial.printf("%d \n", v); - uBit.sleep(100); - return v; -} - static inline void waitForever() { while (true) @@ -102,78 +92,9 @@ static void startSound() playsound(0); } -// static void writeRgbLeds(const uint8_t led0r, const uint8_t led0g, const uint8_t led0b, -// const uint8_t led1r, const uint8_t led1g, const uint8_t led1b, -// const uint8_t led2r, const uint8_t led2g, const uint8_t led2b) -// { -// // Create a buffer to hold pixel data of the appropriate size -// ManagedBuffer b(strip_length * 3); -// b.fill(0); -// // b[0 + 3 * index] = ledg; -// // b[1 + 3 * index] = ledr; -// // b[2 + 3 * index] = ledb; - -// b[0] = led0g; // turns 1st LED green -// b[1] = led0r; -// b[2] = led0b; -// b[3] = led1g; -// b[4] = led1r; // turns 2nd LED red -// b[5] = led1b; -// b[6] = led2g; -// b[7] = led2r; -// b[8] = led2b; // turns 3rd LED blue -// neopixel_send_buffer(uBit.io.RGB, b); -// } - -void neopixel_write() -{ - // Brightness of pixels - int brightness = 50; - - // Create a buffer to hold pixel data of the appropriate size - ManagedBuffer b(strip_length * 3); - - // Fill the buffer with some data. - // Neopixel typically has 3 bytes per pixel, for each primary colour - // in GRB format (Green, then Red, then Blue). - for (int i = 0; i < strip_length * 3; i++) - { - // Clear the buffer to zeroes. You don't need to do this, but it's often convenient. - b.fill(0); - b[i] = brightness; - neopixel_send_buffer(uBit.io.RGB, b); - // c = (c + 1) % 3; - uBit.sleep(100); - // Update the neopixel strip on pin P0. - } - for (int i = 0; i < strip_length; i++) - { - b.fill(0); - b[0 + i] = brightness; - b[3 + i] = brightness; - b[6 + i] = brightness; - neopixel_send_buffer(uBit.io.RGB, b); - // c = (c + 1) % 3; - uBit.sleep(300); - // Update the neopixel strip on pin P0. - blinkImage(herz, 0, 300); - } - - // Increment our colour counter so that we show a different colour in the next frame. - - // Wait for a second. -} - -static void disableRgbLeds() -{ - // Create a buffer to hold pixel data of the appropriate size - ManagedBuffer b(strip_length * 3); - b.fill(0); - neopixel_send_buffer(uBit.io.RGB, b); -} - static void menuAnimateEnter() { + disableLEDs(); uBit.display.print(dot); uBit.sleep(200); uBit.display.print(rect_s); @@ -192,16 +113,7 @@ static void menuAnimateLeave() uBit.display.print(dot); uBit.sleep(200); uBit.display.clear(); -} - -const uint16_t pause = 300; - - -// fire a accelerometer shake event with a 3G event -static void shakeEvent(MicroBitEvent) -{ - MicroBitEvent evt(MICROBIT_ID_GESTURE, MICROBIT_ACCELEROMETER_EVT_SHAKE); - uBit.messageBus.send(evt); + disableLEDs(); } int main() @@ -209,63 +121,67 @@ int main() // uBit.accelerometer.updateSample(); uBit.init(); + // fire a accelerometer shake event with a 3G event uBit.messageBus.listen(MICROBIT_ID_GESTURE, MICROBIT_ACCELEROMETER_EVT_3G, shakeEvent); uBit.display.setBrightness(100); + disableLEDs(); uBit.serial.send("Calliope Demo v3.0\r\n"); - //check if DEMO has already been played + // check if DEMO has already been played // if (!hasStorageKey(KEY_DEMO)) // { // setStorageKey(KEY_DEMO); - // begin of welcome DEMO program - startSound(); - // mems_mic_test(); - // start state - - uBit.display.scroll("Hi"); - - // press A - uBit.display.print("A"); - uBit.sleep(pause); - blinkImageUntilEvent(MICROBIT_ID_BUTTON_A, MICROBIT_BUTTON_EVT_CLICK, arrow_left); - uBit.display.print(check); - uBit.sleep(pause); - uBit.display.clear(); - - // press B - uBit.display.print("B"); - uBit.sleep(pause); - blinkImageUntilEvent(MICROBIT_ID_BUTTON_B, MICROBIT_BUTTON_EVT_CLICK, arrow_right); - uBit.display.print(check); - uBit.sleep(pause); - uBit.display.clear(); - - // press A+B - uBit.display.scroll("A+B"); - uBit.sleep(pause); - blinkImageUntilEvent(MICROBIT_ID_BUTTON_AB, MICROBIT_BUTTON_EVT_CLICK, arrow_left_right); - beep(); - uBit.display.print(check); - uBit.sleep(pause); - uBit.display.clear(); - - // shake - // uBit.display.scroll(LS_DEMO_SHAKE); - blinkImageUntilEvent(MICROBIT_ID_GESTURE, MICROBIT_ACCELEROMETER_EVT_SHAKE, doublerow, -1, - 2, 100); - uBit.display.print(check); - uBit.sleep(pause); - uBit.display.clear(); - - uBit.display.scroll("OK!"); - - // RGB LEDs - neopixel_write(); - disableRgbLeds(); + // begin of welcome DEMO program + startSound(); + + uBit.display.scroll("Hi"); + + // press A + uBit.display.print("A"); + uBit.sleep(pause); + blinkImageUntilEvent(MICROBIT_ID_BUTTON_A, MICROBIT_BUTTON_EVT_CLICK, arrow_left); + uBit.display.print(check); + uBit.sleep(pause); + uBit.display.clear(); + + // press B + uBit.display.print("B"); + uBit.sleep(pause); + blinkImageUntilEvent(MICROBIT_ID_BUTTON_B, MICROBIT_BUTTON_EVT_CLICK, arrow_right); + uBit.display.print(check); + uBit.sleep(pause); + uBit.display.clear(); + + // press A+B + uBit.display.scroll("A+B"); + uBit.sleep(pause); + blinkImageUntilEvent(MICROBIT_ID_BUTTON_AB, MICROBIT_BUTTON_EVT_CLICK, arrow_left_right); + beep(); + uBit.display.print(check); + uBit.sleep(pause); + uBit.display.clear(); + + // shake + // uBit.display.scroll(LS_DEMO_SHAKE); + blinkImageUntilEvent(MICROBIT_ID_GESTURE, MICROBIT_ACCELEROMETER_EVT_SHAKE, doublerow, -1, 2, + 100); + uBit.display.print(check); + uBit.sleep(pause); + uBit.display.clear(); + + uBit.display.scroll("OK!"); + + // RGB LEDs + startLEDs(); + disableLEDs(); + uBit.display.clear(); + + uBit.display.print(smiley); + uBit.sleep(1000); // } // end storage key DEMO menustate_t state = MenuStateOracle; diff --git a/source/samples/Menu.cpp b/source/samples/Menu.cpp index 95c81bd5..d19f183c 100644 --- a/source/samples/Menu.cpp +++ b/source/samples/Menu.cpp @@ -1,9 +1,9 @@ #include "Menu.h" #include "MicroBit.h" +#include "Utils.h" extern MicroBit uBit; - menustate_t menuWaitForChoice(menustate_t start) { menustate_t state = start; @@ -25,6 +25,7 @@ menustate_t menuWaitForChoice(menustate_t start) // } uBit.display.print(ManagedString(state)); + LEDcounter(state); // event loop while (true) { diff --git a/source/samples/MicrophoneTest.cpp b/source/samples/MicrophoneTest.cpp index e97e32a9..8b2a755c 100644 --- a/source/samples/MicrophoneTest.cpp +++ b/source/samples/MicrophoneTest.cpp @@ -61,7 +61,7 @@ mems_mic_test() processor = new StreamNormalizer(mic->output, 0.05f, true, DATASTREAM_FORMAT_8BIT_SIGNED); if (streamer == NULL) - streamer = new SerialStreamer(processor->output, SERIAL_STREAM_MODE_BINARY); + streamer = new SerialStreamer(processor->output, SERIAL_STREAM_MODE_DECIMAL); uBit.io.runmic.setDigitalValue(1); uBit.io.runmic.setHighDrive(true); diff --git a/source/samples/RunVolumeMeter.cpp b/source/samples/RunVolumeMeter.cpp index 0022e97f..901ac853 100644 --- a/source/samples/RunVolumeMeter.cpp +++ b/source/samples/RunVolumeMeter.cpp @@ -2,93 +2,109 @@ #include "RunVolumeMeter.h" #include "MicroBit.h" #include "Utils.h" -// #include "neopixels.h" +#include "LevelDetector.h" +#include "LevelDetectorSPL.h" + +#define MICROPHONE_MIN 52.0f +#define MICROPHONE_MAX 120.0f extern MicroBit uBit; -const int threshold = 519; +long map(long x, long in_min, long in_max, long out_min, long out_max) +{ + return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min; +} -static int read() +int soundLevel() { - int mic = 0; - const int n = 30; + 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)); +} - uBit.io.runmic.setDigitalValue(1); - uBit.io.runmic.setHighDrive(true); +// static int read() +// { +// // int mic = 0; +// // const int n = 30; - for (uint8_t i = 0; i < n; i++) { - // assign new Pin of microphone? - const int v = uBit.io.microphone.getAnalogValue(); - if (v > mic) mic = v; - } +// uBit.audio.activateMic(); - if (mic < threshold) { - return 0; - } +// // for (uint8_t i = 0; i < n; i++) +// // { +// // mic = soundLevel(); +// // } - // mic 512...900 ~> 0...5 - const int gauge = static_cast((log2(mic - threshold + 1) * 5) / 8); +// // mapping values in range of LEDs +// const int gauge = map(soundLevel(), 0, 100, 0, 5); - // uBit.serial.printf("m:%d -> g:%d\n", mic, gauge); +// if (gauge < 0) +// { +// return 0; +// } - if (gauge < 0) { - return 0; - } +// if (gauge > 5) +// { +// return 5; +// } - if (gauge > 5) { - return 5; - } - - return gauge; -} +// return gauge; +// } void volumemeter_run() { - uBit.messageBus.listen( - MICROBIT_ID_BUTTON_AB, - MICROBIT_BUTTON_EVT_CLICK, - leaveHandler); + uBit.audio.activateMic(); + uBit.messageBus.listen(MICROBIT_ID_BUTTON_AB, MICROBIT_BUTTON_EVT_CLICK, leaveHandler); int gauges[5] = {}; leave = false; - while (!leave) { + while (!leave) + { // shift values - for (uint8_t i = 0; i < (5-1); i++) { - gauges[i] = gauges[i+1]; + for (uint8_t i = 0; i < (5 - 1); i++) + { + gauges[i] = gauges[i + 1]; } - gauges[4] = read(); + gauges[4] = map(soundLevel(), 0, 100, 0, 5); - for (uint16_t x = 0; x < 5; x++) { + for (uint16_t x = 0; x < 5; x++) + { const int gauge = gauges[x]; - for (uint16_t y = 0; y < 5; y++) { + for (uint16_t y = 0; y < 5; y++) + { const uint8_t t = static_cast(gauge > y ? 255 : 0); - uBit.display.image.setPixelValue(x, 4-y, t); + uBit.display.image.setPixelValue(x, 4 - y, t); } } int sum = 0; - for (uint8_t i = 0; i < 5; i++) { + for (uint8_t i = 0; i < 5; i++) + { sum += gauges[i]; } - if (sum > 5*2.5) { - // neopixels - // uBit.rgb.setColour(0xFF, 0x00, 0x00, 0x00); - } else { - //neopixels - // uBit.rgb.setColour(0x00, 0xFF, 0x00, 0x00); + if (sum > 5 * 3) + { + LEDTrafficLight(2); + } + else if (sum > 5 * 1.5) + { + LEDTrafficLight(1); + } + else + { + LEDTrafficLight(0); } uBit.sleep(100); } - // uBit.rgb.off(); + // uBit.rgb.off(); - uBit.messageBus.ignore( - MICROBIT_ID_BUTTON_AB, - MICROBIT_BUTTON_EVT_CLICK, - leaveHandler); + uBit.messageBus.ignore(MICROBIT_ID_BUTTON_AB, MICROBIT_BUTTON_EVT_CLICK, leaveHandler); } \ No newline at end of file diff --git a/source/samples/Utils.cpp b/source/samples/Utils.cpp index a331d1f7..2b305d82 100644 --- a/source/samples/Utils.cpp +++ b/source/samples/Utils.cpp @@ -1,6 +1,7 @@ #include "Utils.h" #include "MicroBit.h" #include "MicroBitImage.h" +#include "Images.h" extern MicroBit uBit; @@ -16,52 +17,187 @@ void leaveHandler(MicroBitEvent) leave = true; } -void blinkImage( - const MicroBitImage& image, - const int pos, - const uint16_t delay) +void blinkImage(const MicroBitImage &image, const int pos, const uint16_t delay) { event = false; uBit.display.print(image, pos); - for (uint32_t t = 0, i = 10; t < delay && !leave && !event; t += i) { + for (uint32_t t = 0, i = 10; t < delay && !leave && !event; t += i) + { uBit.sleep(i); } - if (event) { + if (event) + { return; } uBit.display.clear(); - for (uint32_t t = 0, i = 10; t < delay && !leave && !event; t += i) { + for (uint32_t t = 0, i = 10; t < delay && !leave && !event; t += i) + { uBit.sleep(i); } } -void blinkImageUntilEvent( - const uint16_t source, - const uint16_t value, - const MicroBitImage& image, - const int pos1, - const int pos2, - const uint16_t delay) +void blinkImageUntilEvent(const uint16_t source, const uint16_t value, const MicroBitImage &image, + const int pos1, const int pos2, const uint16_t delay) { - uBit.messageBus.listen( - source, - value, - eventHandler); + uBit.messageBus.listen(source, value, eventHandler); event = false; - for (uint8_t i = 0; !leave && !event; i++) { + for (uint8_t i = 0; !leave && !event; i++) + { blinkImage(image, (i % 2) == 0 ? pos1 : pos2, delay); } - uBit.messageBus.ignore( - source, - value, - eventHandler); -} \ No newline at end of file + uBit.messageBus.ignore(source, value, eventHandler); +} + +// fire a accelerometer shake event with a 3G event +void shakeEvent(MicroBitEvent) +{ + MicroBitEvent evt(MICROBIT_ID_GESTURE, MICROBIT_ACCELEROMETER_EVT_SHAKE); + uBit.messageBus.send(evt); +} + +// LED functions +int brightness = 32; +const int strip_length = 3; +uint16_t pause = 300; + +void LEDTrafficLight(int color) +{ + + // Create a buffer to hold pixel data of the appropriate size + ManagedBuffer b(strip_length * 3); + b.fill(0); + if (color == 0) + { + // turns 1st LED green + b[0] = brightness; + } + else if (color == 1) + { + // turns 2nd LED yellow + b[3] = brightness; + b[4] = brightness; + } + else if (color == 2) + { + // turns 3rd LED red + b[7] = brightness; + } + neopixel_send_buffer(uBit.io.RGB, b); +} + +void disableLEDs() +{ + // Create a buffer to hold pixel data of the appropriate size + ManagedBuffer b(strip_length * 3); + b.fill(0); + neopixel_send_buffer(uBit.io.RGB, b); +} + +void startLEDs() +{ + // Brightness of pixels + + // Create a buffer to hold pixel data of the appropriate size + ManagedBuffer b(strip_length * 3); + + // Fill the buffer with some data. + // Neopixel typically has 3 bytes per pixel, for each primary colour + // in GRB format (Green, then Red, then Blue). + for (int i = 0; i < strip_length * 3; i++) + { + // Clear the buffer to zeroes. You don't need to do this, but it's often convenient. + b.fill(0); + b[i] = brightness; + neopixel_send_buffer(uBit.io.RGB, b); + // c = (c + 1) % 3; + uBit.sleep(100); + // Update the neopixel strip on pin P0. + } + for (int i = 0; i < strip_length; i++) + { + disableLEDs(); + uBit.sleep(pause / 2); + b.fill(0); + b[0 + i] = brightness; + b[3 + i] = brightness; + b[6 + i] = brightness; + neopixel_send_buffer(uBit.io.RGB, b); + // c = (c + 1) % 3; + uBit.sleep(pause / 2); + // Update the neopixel strip on pin P0. + blinkImage(herz, 0, pause); + } + + // Wait for a second. +} + +void turnLEDon(int start_index, int end_index) +{ + brightness = 20; + ManagedBuffer b(strip_length * 3); + b.fill(0); + for (int i = start_index * 3; i < end_index * 3; i++) + b[i] = brightness; + neopixel_send_buffer(uBit.io.RGB, b); +} + +void LEDcounter(int number) +{ + ManagedBuffer b(strip_length * 3); + b.fill(0); + switch (number) + { + // 1 + case 1: + turnLEDon(0,1); + break; + // 2 + case 2: + turnLEDon(0,2); + break; + // 3 + case 3: + turnLEDon(0,3); + break; + // 4 + case 4: + turnLEDon(1,3); + break; + // 5 + case 5: + turnLEDon(2,3); + break; + } +} + + // static void writeRgbLeds(const uint8_t led0r, const uint8_t led0g, const uint8_t led0b, + // const uint8_t led1r, const uint8_t led1g, const uint8_t led1b, + // const uint8_t led2r, const uint8_t led2g, const uint8_t led2b) + // { + // // Create a buffer to hold pixel data of the appropriate size + // ManagedBuffer b(strip_length * 3); + // b.fill(0); + // // b[0 + 3 * index] = ledg; + // // b[1 + 3 * index] = ledr; + // // b[2 + 3 * index] = ledb; + + // b[0] = led0g; // turns 1st LED green + // b[1] = led0r; + // b[2] = led0b; + // b[3] = led1g; + // b[4] = led1r; // turns 2nd LED red + // b[5] = led1b; + // b[6] = led2g; + // b[7] = led2r; + // b[8] = led2b; // turns 3rd LED blue + // neopixel_send_buffer(uBit.io.RGB, b); + // } \ No newline at end of file diff --git a/source/samples/Utils.h b/source/samples/Utils.h index e18c2c7b..e149812b 100644 --- a/source/samples/Utils.h +++ b/source/samples/Utils.h @@ -22,4 +22,18 @@ void blinkImageUntilEvent( const int pos2 = 0, const uint16_t delay = 200); +void shakeEvent(MicroBitEvent); + +void LEDTrafficLight(int color); + +void disableLEDs(); + +void startLEDs(); + +void turnLEDon(int start_index, int end_index); + +void LEDcounter(int number); + + + #endif // UTILS_H \ No newline at end of file From 8995ae654b37a5cf013bea05ff1ed4978309db0e Mon Sep 17 00:00:00 2001 From: Jasper Date: Mon, 24 Jul 2023 16:05:46 +0200 Subject: [PATCH 04/18] adding program five --- source/main.cpp | 40 ++++++++------------------ source/samples/Images.cpp | 2 +- source/samples/Menu.cpp | 2 +- source/samples/ProgramFive.cpp | 51 ++++++++++++++++++++++++++++++++++ source/samples/ProgramFive.h | 6 ++++ 5 files changed, 71 insertions(+), 30 deletions(-) create mode 100644 source/samples/ProgramFive.cpp create mode 100644 source/samples/ProgramFive.h diff --git a/source/main.cpp b/source/main.cpp index 222f8b81..4051cd00 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -8,6 +8,7 @@ #include "RunRockPaperScissors.h" #include "RunMultiplication.h" #include "RunVolumeMeter.h" +#include "ProgramFive.h" #include "Utils.h" #include "Tests.h" // #include "neopixel.h" @@ -21,32 +22,6 @@ static bool mute = false; const uint16_t pause = 300; // --------------------------- - -static void showNameHistogram(MicroBitDisplay &display) -{ - NRF_FICR_Type *ficr = NRF_FICR; - uint32_t n = ficr->DEVICEID[1]; - uint32_t ld = 1; - uint32_t d = MICROBIT_DFU_HISTOGRAM_HEIGHT; - uint32_t h; - - display.clear(); - for (uint32_t i = 0; i < MICROBIT_DFU_HISTOGRAM_WIDTH; i++) - { - h = (n % d) / ld; - - n -= h; - d *= MICROBIT_DFU_HISTOGRAM_HEIGHT; - ld *= MICROBIT_DFU_HISTOGRAM_HEIGHT; - - for (uint32_t j = 0; j < h + 1; j++) - display.image.setPixelValue( - static_cast(MICROBIT_DFU_HISTOGRAM_WIDTH - i - 1), - static_cast(MICROBIT_DFU_HISTOGRAM_HEIGHT - j - 1), - 255); - } -} - static void playsound(int freq) { if (mute || freq == 0) @@ -122,6 +97,12 @@ int main() // uBit.accelerometer.updateSample(); uBit.init(); + // while (1) + // { + // uBit.serial.printf("%d \n", soundLevel()); + // uBit.sleep(100); + // } + // fire a accelerometer shake event with a 3G event uBit.messageBus.listen(MICROBIT_ID_GESTURE, MICROBIT_ACCELEROMETER_EVT_3G, shakeEvent); @@ -197,6 +178,7 @@ int main() // uBit.serial.printf("%d \n",readMic()); state = menuWaitForChoice(state); + switch (state) { // 1 @@ -225,10 +207,12 @@ int main() break; // 5 case MenuStateInterpreter: - setStorageKey(KEY_INTERPRETER); - uBit.reset(); + menuAnimateEnter(); + programfive_run(); + menuAnimateLeave(); break; } + LEDcounter(state); } } diff --git a/source/samples/Images.cpp b/source/samples/Images.cpp index 790ed50f..f2967af8 100644 --- a/source/samples/Images.cpp +++ b/source/samples/Images.cpp @@ -18,7 +18,7 @@ MicroBitImage MicroBitImage herz("0,255,0,255,0\n255,255,255,255,255\n255,255,255,255,255\n0,255,255,255,0\n0,0,255,0,0\n"); MicroBitImage rock("0,0,0,0,0\n0,255,255,255,0\n0,255,255,255,0\n0,255,255,255,0\n0,0,0,0,0\n"); MicroBitImage paper("255,255,255,255,255\n255,0,0,0,255\n255,0,0,0,255\n255,0,0,0,255\n255,255,255,255,255\n"); -MicroBitImage scissor("255,255,0,0,255\n255,255,0,255,0\n0,0,255,0,0\n0,0,0,255,255\n0,0,0,255,255\n"); +MicroBitImage scissor("255,255,0,0,255\n255,255,0,255,0\n0,0,255,0,0\n255,255,0,255,0\n255,255,0,0,255\n"); MicroBitImage multiply("0,0,0,0,0\n0,255,0,255,0\n0,0,255,0,0\n0,255,0,255,0\n0,0,0,0,0\n"); diff --git a/source/samples/Menu.cpp b/source/samples/Menu.cpp index d19f183c..8351a53f 100644 --- a/source/samples/Menu.cpp +++ b/source/samples/Menu.cpp @@ -23,10 +23,10 @@ menustate_t menuWaitForChoice(menustate_t start) // } else { // uBit.display.print(ManagedString(state)); // } - uBit.display.print(ManagedString(state)); LEDcounter(state); + // event loop while (true) { // button A diff --git a/source/samples/ProgramFive.cpp b/source/samples/ProgramFive.cpp new file mode 100644 index 00000000..bb8d2949 --- /dev/null +++ b/source/samples/ProgramFive.cpp @@ -0,0 +1,51 @@ + #include "ProgramFive.h" + #include "MicroBit.h" + #include "Utils.h" + + extern MicroBit uBit; + + // visual ID code constants + +static void showNameHistogram(MicroBitDisplay &display) +{ + uint16_t pixel_length = 5; + NRF_FICR_Type *ficr = NRF_FICR; + uint32_t n = ficr->DEVICEID[1]; + uint32_t ld = 1; + uint32_t d = pixel_length; + uint32_t h; + + display.clear(); + for (uint32_t i = 0; i < pixel_length; i++) + { + h = (n % d) / ld; + + n -= h; + d *= pixel_length; + ld *= pixel_length; + + for (uint32_t j = 0; j < h + 1; j++) + display.image.setPixelValue(static_cast(pixel_length - i - 1), + static_cast(pixel_length - j - 1), 255); + } +} + +void programfive_run() +{ + uBit.messageBus.listen(MICROBIT_ID_BUTTON_AB, MICROBIT_BUTTON_EVT_CLICK, leaveHandler); + + leave = false; + while (!leave) { + + if (leave) { + break; + } + + showNameHistogram(uBit.display); + + uBit.sleep(100); + uBit.display.clear(); + } + + uBit.messageBus.ignore(MICROBIT_ID_BUTTON_AB, MICROBIT_BUTTON_EVT_CLICK, leaveHandler); +} \ No newline at end of file diff --git a/source/samples/ProgramFive.h b/source/samples/ProgramFive.h new file mode 100644 index 00000000..d39f2b68 --- /dev/null +++ b/source/samples/ProgramFive.h @@ -0,0 +1,6 @@ +#ifndef RUN_PROGRAM_FIVE_H +#define RUN_PROGRAM_FIVE_H + +void programfive_run(); + +#endif // RRUN_PROGRAM_FIVE_H \ No newline at end of file From b02803fd3b7a78b78ffb9e7255568d1c5592780b Mon Sep 17 00:00:00 2001 From: Jasper Date: Tue, 25 Jul 2023 15:31:37 +0200 Subject: [PATCH 05/18] implementing test run --- source/main.cpp | 29 ++---------------- source/samples/RunTests.cpp | 59 +++++++++++++++++++++++++++++++++++++ source/samples/RunTests.h | 6 ++++ source/samples/Utils.cpp | 21 +++++++++++++ source/samples/Utils.h | 3 ++ 5 files changed, 92 insertions(+), 26 deletions(-) create mode 100644 source/samples/RunTests.cpp create mode 100644 source/samples/RunTests.h diff --git a/source/main.cpp b/source/main.cpp index 4051cd00..80076ce0 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -11,35 +11,17 @@ #include "ProgramFive.h" #include "Utils.h" #include "Tests.h" +#include "RunTests.h" // #include "neopixel.h" // #include "Interpreter.h" // #include "nrf.h" MicroBit uBit; -static bool mute = false; // Length of neopixel strip const uint16_t pause = 300; // --------------------------- -static void playsound(int freq) -{ - if (mute || freq == 0) - { - uBit.io.speaker.setAnalogValue(0); - uBit.io.P0.setAnalogValue(0); - return; - } - uBit.io.speaker.setHighDrive(true); - uBit.io.speaker.setAnalogValue(512); - uBit.io.P0.setHighDrive(true); - uBit.io.P0.setAnalogValue(512); - int period = 1000000.0 / (float)freq; - - uBit.io.speaker.setAnalogPeriodUs(period); - uBit.io.P0.setAnalogPeriodUs(period); - return; -} static inline void waitForever() { @@ -94,21 +76,16 @@ static void menuAnimateLeave() int main() { - // uBit.accelerometer.updateSample(); uBit.init(); - // while (1) - // { - // uBit.serial.printf("%d \n", soundLevel()); - // uBit.sleep(100); - // } - // fire a accelerometer shake event with a 3G event uBit.messageBus.listen(MICROBIT_ID_GESTURE, MICROBIT_ACCELEROMETER_EVT_3G, shakeEvent); uBit.display.setBrightness(100); disableLEDs(); + tests_run(); + uBit.serial.send("Calliope Demo v3.0\r\n"); // check if DEMO has already been played diff --git a/source/samples/RunTests.cpp b/source/samples/RunTests.cpp new file mode 100644 index 00000000..0af4c8b0 --- /dev/null +++ b/source/samples/RunTests.cpp @@ -0,0 +1,59 @@ +#include +#include "RunTests.h" +#include "MicroBit.h" +//#include "MicroBitSerial.h" +#include "Images.h" +#include "Utils.h" + +extern MicroBit uBit; + +static void showAllLEDs() +{ + uint16_t pixel_length = 25; + + uBit.display.clear(); + for (uint32_t i = 0; i < pixel_length; i++) + { + uBit.display.image.setPixelValue(i % 5, i / 5, 255); + } +} + +void tests_run() +{ + // uBit.messageBus.listen(MICROBIT_ID_BUTTON_A, MICROBIT_BUTTON_EVT_CLICK, onButtonA); + uBit.serial.send("begin test"); + uBit.sleep(500); + // initializes accelerometer. It's automatically initialized by creating an object. If + // requestUpdate could not save data in the buffer it will throw an 050 error + uBit.accelerometer.requestUpdate(); + uBit.serial.send("accelerometer found"); + + showAllLEDs(); + uBit.serial.send("show all LEDs on display\r\n"); + uBit.sleep(1000); + uBit.display.clear(); + turnLEDon(0, 3); + uBit.serial.send("show all neopixel LEDs\r\n"); + uBit.sleep(1000); + disableLEDs(); + blinkImageUntilEvent(MICROBIT_ID_BUTTON_A, MICROBIT_BUTTON_EVT_CLICK, arrow_left); + uBit.serial.send("button A pressed\r\n"); + playsound(300); + uBit.sleep(500); + uBit.serial.send("play sound\r\n"); + playsound(0); + + if (uBit.accelerometer.getZ() != 0) + { + uBit.serial.send("accelerometer z-axis not 0\r\n"); + uBit.display.print(check); + uBit.sleep(1000); + } + else + { + uBit.display.clear(); + } + uBit.serial.send("end test"); + uBit.display.clear(); + uBit.sleep(500); +} \ No newline at end of file diff --git a/source/samples/RunTests.h b/source/samples/RunTests.h new file mode 100644 index 00000000..9b41379b --- /dev/null +++ b/source/samples/RunTests.h @@ -0,0 +1,6 @@ +#ifndef RUN_TESTS_H +#define RUN_TESTS_H + +void tests_run(); + +#endif // RUN_TESTS_H \ No newline at end of file diff --git a/source/samples/Utils.cpp b/source/samples/Utils.cpp index 2b305d82..c5b5cefc 100644 --- a/source/samples/Utils.cpp +++ b/source/samples/Utils.cpp @@ -179,6 +179,27 @@ void LEDcounter(int number) } } +bool mute_mic = false; + +void playsound(int freq) +{ + if (mute_mic || freq == 0) + { + uBit.io.speaker.setAnalogValue(0); + uBit.io.P0.setAnalogValue(0); + return; + } + uBit.io.speaker.setHighDrive(true); + uBit.io.speaker.setAnalogValue(512); + uBit.io.P0.setHighDrive(true); + uBit.io.P0.setAnalogValue(512); + int period = 1000000.0 / (float)freq; + + uBit.io.speaker.setAnalogPeriodUs(period); + uBit.io.P0.setAnalogPeriodUs(period); + return; +} + // static void writeRgbLeds(const uint8_t led0r, const uint8_t led0g, const uint8_t led0b, // const uint8_t led1r, const uint8_t led1g, const uint8_t led1b, // const uint8_t led2r, const uint8_t led2g, const uint8_t led2b) diff --git a/source/samples/Utils.h b/source/samples/Utils.h index e149812b..6a471c89 100644 --- a/source/samples/Utils.h +++ b/source/samples/Utils.h @@ -34,6 +34,9 @@ void turnLEDon(int start_index, int end_index); void LEDcounter(int number); +extern bool mute_mic; +void playsound(int freq); + #endif // UTILS_H \ No newline at end of file From 5a7a28cdc0844c520a232cf861b43f48985943cf Mon Sep 17 00:00:00 2001 From: Jasper Date: Thu, 27 Jul 2023 13:02:30 +0200 Subject: [PATCH 06/18] optimizing LED functions --- source/main.cpp | 31 ++++- source/samples/Images.cpp | 5 +- source/samples/RunTests.cpp | 28 ++-- source/samples/RunVolumeMeter.cpp | 33 ----- source/samples/Utils.cpp | 214 ++++++++++++++++++++++++++---- source/samples/Utils.h | 20 +++ 6 files changed, 250 insertions(+), 81 deletions(-) diff --git a/source/main.cpp b/source/main.cpp index 80076ce0..24aaa5ed 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -73,6 +73,8 @@ static void menuAnimateLeave() disableLEDs(); } +int display_brightness = 100; + int main() { @@ -81,10 +83,12 @@ int main() // fire a accelerometer shake event with a 3G event uBit.messageBus.listen(MICROBIT_ID_GESTURE, MICROBIT_ACCELEROMETER_EVT_3G, shakeEvent); - uBit.display.setBrightness(100); + uBit.display.setBrightness(display_brightness); disableLEDs(); - tests_run(); + moveImageUntilEvent(MICROBIT_ID_GESTURE, MICROBIT_ACCELEROMETER_EVT_SHAKE, doublerow, 5, 1, 30); + //rainbow2(); + //tests_run(); uBit.serial.send("Calliope Demo v3.0\r\n"); @@ -124,9 +128,7 @@ int main() uBit.display.clear(); // shake - // uBit.display.scroll(LS_DEMO_SHAKE); - blinkImageUntilEvent(MICROBIT_ID_GESTURE, MICROBIT_ACCELEROMETER_EVT_SHAKE, doublerow, -1, 2, - 100); + moveImageUntilEvent(MICROBIT_ID_GESTURE, MICROBIT_ACCELEROMETER_EVT_SHAKE, doublerow, 3, 1, 50); uBit.display.print(check); uBit.sleep(pause); uBit.display.clear(); @@ -134,11 +136,26 @@ int main() uBit.display.scroll("OK!"); // RGB LEDs - startLEDs(); + // startLEDs(); disableLEDs(); uBit.display.clear(); + uBit.display.setBrightness(0); uBit.display.print(smiley); + + for (int i = 0; i < display_brightness; i++) + { + uBit.display.setBrightness(i); + + uBit.sleep(10); + } + + for (int i = display_brightness; i > 0; i--) + { + uBit.display.setBrightness(i); + uBit.sleep(10); + } + uBit.sleep(1000); // } // end storage key DEMO @@ -155,7 +172,7 @@ int main() // uBit.serial.printf("%d \n",readMic()); state = menuWaitForChoice(state); - + switch (state) { // 1 diff --git a/source/samples/Images.cpp b/source/samples/Images.cpp index f2967af8..529717ed 100644 --- a/source/samples/Images.cpp +++ b/source/samples/Images.cpp @@ -13,8 +13,9 @@ MicroBitImage MicroBitImage arrow_left_right("0,0,255,0,0\n0,255,0,255,0\n255,0,0,0,255\n0,255,0,255,0\n0,0,255,0,0\n"); MicroBitImage check("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"); -MicroBitImage - doublerow("0,255,255,0,0\n0,255,255,0,0\n0,255,255,0,0\n0,255,255,0,0\n0,255,255,0,0\n"); +MicroBitImage doublerow("0,255,255,0,0\n0,255,255,0,0\n0,255,255,0,0\n0,255,255,0,0\n0,255,255,0,0\n"); +//MicroBitImage doublerow("0,0,0,255,255\n0,0,0,255,255\n0,0,0,255,255\n0,0,0,255,255\n0,0,0,255,255\n"); +//MicroBitImage doublerow("255,255,0,0,0\n255,255,0,0,0\n255,255,0,0,0\n255,255,0,0,0\n255,255,0,0,0\n"); MicroBitImage herz("0,255,0,255,0\n255,255,255,255,255\n255,255,255,255,255\n0,255,255,255,0\n0,0,255,0,0\n"); MicroBitImage rock("0,0,0,0,0\n0,255,255,255,0\n0,255,255,255,0\n0,255,255,255,0\n0,0,0,0,0\n"); MicroBitImage paper("255,255,255,255,255\n255,0,0,0,255\n255,0,0,0,255\n255,0,0,0,255\n255,255,255,255,255\n"); diff --git a/source/samples/RunTests.cpp b/source/samples/RunTests.cpp index 0af4c8b0..2ba9c44e 100644 --- a/source/samples/RunTests.cpp +++ b/source/samples/RunTests.cpp @@ -30,18 +30,23 @@ void tests_run() showAllLEDs(); uBit.serial.send("show all LEDs on display\r\n"); - uBit.sleep(1000); - uBit.display.clear(); turnLEDon(0, 3); uBit.serial.send("show all neopixel LEDs\r\n"); - uBit.sleep(1000); - disableLEDs(); - blinkImageUntilEvent(MICROBIT_ID_BUTTON_A, MICROBIT_BUTTON_EVT_CLICK, arrow_left); - uBit.serial.send("button A pressed\r\n"); - playsound(300); - uBit.sleep(500); - uBit.serial.send("play sound\r\n"); - playsound(0); + + while (true) + { + // button A + if (uBit.buttonA.isPressed()) + { + + uBit.serial.send("button A pressed\r\n"); + playsound(300); + uBit.sleep(500); + uBit.serial.send("play sound\r\n"); + playsound(0); + break; + } + } if (uBit.accelerometer.getZ() != 0) { @@ -53,7 +58,8 @@ void tests_run() { uBit.display.clear(); } + uBit.sleep(3000); uBit.serial.send("end test"); uBit.display.clear(); - uBit.sleep(500); + disableLEDs(); } \ No newline at end of file diff --git a/source/samples/RunVolumeMeter.cpp b/source/samples/RunVolumeMeter.cpp index 901ac853..662cf318 100644 --- a/source/samples/RunVolumeMeter.cpp +++ b/source/samples/RunVolumeMeter.cpp @@ -10,11 +10,6 @@ extern MicroBit uBit; -long map(long x, long in_min, long in_max, long out_min, long out_max) -{ - return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min; -} - int soundLevel() { LevelDetectorSPL *level = uBit.audio.levelSPL; @@ -25,34 +20,6 @@ int soundLevel() return min(0xff, scaled * 0xff / (MICROPHONE_MAX - MICROPHONE_MIN)); } -// static int read() -// { -// // int mic = 0; -// // const int n = 30; - -// uBit.audio.activateMic(); - -// // for (uint8_t i = 0; i < n; i++) -// // { -// // mic = soundLevel(); -// // } - -// // mapping values in range of LEDs -// const int gauge = map(soundLevel(), 0, 100, 0, 5); - -// if (gauge < 0) -// { -// return 0; -// } - -// if (gauge > 5) -// { -// return 5; -// } - -// return gauge; -// } - void volumemeter_run() { uBit.audio.activateMic(); diff --git a/source/samples/Utils.cpp b/source/samples/Utils.cpp index c5b5cefc..cf1785c0 100644 --- a/source/samples/Utils.cpp +++ b/source/samples/Utils.cpp @@ -5,6 +5,11 @@ extern MicroBit uBit; +long map(long x, long in_min, long in_max, long out_min, long out_max) +{ + return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min; +} + bool event = false; void eventHandler(MicroBitEvent) { @@ -57,6 +62,63 @@ void blinkImageUntilEvent(const uint16_t source, const uint16_t value, const Mic uBit.messageBus.ignore(source, value, eventHandler); } +// void moveImageUntilEvent(const uint16_t source, const uint16_t value, const MicroBitImage &image, +// const uint16_t delay) +// { +// int direction = 1; +// int offset = -3; +// uBit.messageBus.listen(source, value, eventHandler); + +// event = false; + +// for (uint8_t i = 0; !leave && !event; i++) +// { +// for (int j = 0; j < 20; j++) +// { + +// offset = offset + direction; +// uBit.display.print(image, offset, 0, 0, delay); +// if (offset >= 7) +// { +// direction = -1; +// } +// } +// direction = 1; +// } +// uBit.messageBus.ignore(source, value, eventHandler); +// } + +void moveImageUntilEvent( +const uint16_t source, +const uint16_t value, +const MicroBitImage &image, +// const uint16_t cycles, +const uint16_t offset, +const int asymetric, +const uint16_t delay) +{ + int direction = 1; + int x; + + uBit.messageBus.listen(source, value, eventHandler); + + event = false; + + for (uint8_t i = 0; !leave && !event; i++) + { + uBit.serial.send(x + "\r\n"); + x += direction; + if (x >= offset + asymetric) + { + direction = -1; + } else if (x <= -offset ) { + direction = 1; + } + uBit.display.print(image, x, 0, 0, delay); + } + uBit.messageBus.ignore(source, value, eventHandler); +} + // fire a accelerometer shake event with a 3G event void shakeEvent(MicroBitEvent) { @@ -158,27 +220,123 @@ void LEDcounter(int number) { // 1 case 1: - turnLEDon(0,1); + turnLEDon(0, 1); break; // 2 case 2: - turnLEDon(0,2); + turnLEDon(0, 2); break; // 3 case 3: - turnLEDon(0,3); + turnLEDon(0, 3); break; // 4 case 4: - turnLEDon(1,3); + turnLEDon(1, 3); break; - // 5 + // 5 case 5: - turnLEDon(2,3); + turnLEDon(2, 3); break; } } +void rainbow() +{ + uint16_t i, j; + int WheelPos; + ManagedBuffer b(3 * 3); + + for (j = 0; j < 250; j++) + { + for (i = 0; i < 3; i++) + { + WheelPos = i + j; + WheelPos = 255 - WheelPos; + if (WheelPos < 85) + { + b[0 + i * 3] = (255 - WheelPos * 3) / 10; + b[1 + i * 3] = (0) / 10; + b[2 + i * 3] = (WheelPos * 3) / 10; + } + if (WheelPos < 170) + { + WheelPos -= 85; + b[0 + i * 3] = (0) / 10; + b[1 + i * 3] = (WheelPos * 3) / 10; + b[2 + i * 3] = (255 - WheelPos * 3) / 10; + } + if (WheelPos > 170 && WheelPos < 240) + { + WheelPos -= 170; + b[0 + i * 3] = (WheelPos * 3) / 10; + b[1 + i * 3] = (255 - WheelPos * 3) / 10; + b[2 + i * 3] = (0) / 10; + } + } + neopixel_send_buffer(uBit.io.RGB, b); + uBit.sleep(10); + } +} + +void rainbow2() +{ + // Cycle a single RGB led using the sine fade technique + // For common cathode led + + ManagedBuffer b(strip_length * 3); + int delay = 1; // ms + + const uint8_t sins[360] = { + 0, 0, 0, 0, 0, 1, 1, 2, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, + 13, 15, 17, 18, 20, 22, 24, 26, 28, 30, 32, 35, 37, 39, 42, 44, 47, 49, + 52, 55, 58, 60, 63, 66, 69, 72, 75, 78, 81, 85, 88, 91, 94, 97, 101, 104, + 107, 111, 114, 117, 121, 124, 127, 131, 134, 137, 141, 144, 147, 150, 154, 157, 160, 163, + 167, 170, 173, 176, 179, 182, 185, 188, 191, 194, 197, 200, 202, 205, 208, 210, 213, 215, + 217, 220, 222, 224, 226, 229, 231, 232, 234, 236, 238, 239, 241, 242, 244, 245, 246, 248, + 249, 250, 251, 251, 252, 253, 253, 254, 254, 255, 255, 255, 255, 255, 255, 255, 254, 254, + 253, 253, 252, 251, 251, 250, 249, 248, 246, 245, 244, 242, 241, 239, 238, 236, 234, 232, + 231, 229, 226, 224, 222, 220, 217, 215, 213, 210, 208, 205, 202, 200, 197, 194, 191, 188, + 185, 182, 179, 176, 173, 170, 167, 163, 160, 157, 154, 150, 147, 144, 141, 137, 134, 131, + 127, 124, 121, 117, 114, 111, 107, 104, 101, 97, 94, 91, 88, 85, 81, 78, 75, 72, + 69, 66, 63, 60, 58, 55, 52, 49, 47, 44, 42, 39, 37, 35, 32, 30, 28, 26, + 24, 22, 20, 18, 17, 15, 13, 12, 11, 9, 8, 7, 6, 5, 4, 3, 2, 2, + 1, 1, 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, 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, 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, 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, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; + + /* fade leds in */ + // for (int j = 0; j < 100; j++) + // { + // for (size_t i = 0; i < 3; i++) + // { + // // + // b[0 + i * 3] = map(j, 0, 100, 0, map(sins[120 % 360], 0, 255, 0, brightness)); + // b[1 + i * 3] = map(j, 0, 100, 0, map(sins[(120 + i* 120) % 360], 0, 255, 0, + // brightness)); b[2 + i * 3] = map(j, 0, 100, 0, map(sins[(240 + i * 120) % 360], 0, + // 255, 0, brightness)); neopixel_send_buffer(uBit.io.RGB, b); uBit.sleep(1); + // } + // } + + for (int k = 0; k < 360; k++) + { + + for (int i = 0; i < strip_length; i++) + { + // green red blue + b[0 + i * 3] = map(sins[(k + i * 120) % 360], 0, 255, 0, brightness); + b[1 + i * 3] = map(sins[(k + 120 + i * 120) % 360], 0, 255, 0, brightness); + b[2 + i * 3] = map(sins[(k + 240 + i * 120) % 360], 0, 255, 0, brightness); + neopixel_send_buffer(uBit.io.RGB, b); + uBit.sleep(delay); + } + } +} + bool mute_mic = false; void playsound(int freq) @@ -200,25 +358,25 @@ void playsound(int freq) return; } - // static void writeRgbLeds(const uint8_t led0r, const uint8_t led0g, const uint8_t led0b, - // const uint8_t led1r, const uint8_t led1g, const uint8_t led1b, - // const uint8_t led2r, const uint8_t led2g, const uint8_t led2b) - // { - // // Create a buffer to hold pixel data of the appropriate size - // ManagedBuffer b(strip_length * 3); - // b.fill(0); - // // b[0 + 3 * index] = ledg; - // // b[1 + 3 * index] = ledr; - // // b[2 + 3 * index] = ledb; - - // b[0] = led0g; // turns 1st LED green - // b[1] = led0r; - // b[2] = led0b; - // b[3] = led1g; - // b[4] = led1r; // turns 2nd LED red - // b[5] = led1b; - // b[6] = led2g; - // b[7] = led2r; - // b[8] = led2b; // turns 3rd LED blue - // neopixel_send_buffer(uBit.io.RGB, b); - // } \ No newline at end of file +// static void writeRgbLeds(const uint8_t led0r, const uint8_t led0g, const uint8_t led0b, +// const uint8_t led1r, const uint8_t led1g, const uint8_t led1b, +// const uint8_t led2r, const uint8_t led2g, const uint8_t led2b) +// { +// // Create a buffer to hold pixel data of the appropriate size +// ManagedBuffer b(strip_length * 3); +// b.fill(0); +// // b[0 + 3 * index] = ledg; +// // b[1 + 3 * index] = ledr; +// // b[2 + 3 * index] = ledb; + +// b[0] = led0g; // turns 1st LED green +// b[1] = led0r; +// b[2] = led0b; +// b[3] = led1g; +// b[4] = led1r; // turns 2nd LED red +// b[5] = led1b; +// b[6] = led2g; +// b[7] = led2r; +// b[8] = led2b; // turns 3rd LED blue +// neopixel_send_buffer(uBit.io.RGB, b); +// } \ No newline at end of file diff --git a/source/samples/Utils.h b/source/samples/Utils.h index 6a471c89..a5e5cc8f 100644 --- a/source/samples/Utils.h +++ b/source/samples/Utils.h @@ -3,6 +3,8 @@ #include "MicroBit.h" +long map(long x, long in_min, long in_max, long out_min, long out_max); + extern bool event; void leaveHandler(MicroBitEvent); @@ -22,6 +24,20 @@ void blinkImageUntilEvent( const int pos2 = 0, const uint16_t delay = 200); +// void moveImageUntilEvent( +// const uint16_t source, +// const uint16_t value, +// const MicroBitImage &image, +// const uint16_t delay = 30); + +void moveImageUntilEvent( + const uint16_t source, + const uint16_t value, + const MicroBitImage &image, + const uint16_t offset = 3, + const int asymetric = 0, + const uint16_t delay = 30); + void shakeEvent(MicroBitEvent); void LEDTrafficLight(int color); @@ -34,6 +50,10 @@ void turnLEDon(int start_index, int end_index); void LEDcounter(int number); +void rainbow(); + +void rainbow2(); + extern bool mute_mic; void playsound(int freq); From 0709d10b5358c5c35b79b2e53416e5f2937f7270 Mon Sep 17 00:00:00 2001 From: Jasper Date: Wed, 30 Aug 2023 14:57:26 +0200 Subject: [PATCH 07/18] merging rainbow and smiley fade function to look non-blocking adding shake animation to roch paper scissor --- source/main.cpp | 30 +++------ source/samples/RunRockPaperScissors.cpp | 8 +-- source/samples/Utils.cpp | 82 ++++++++++++++----------- 3 files changed, 55 insertions(+), 65 deletions(-) diff --git a/source/main.cpp b/source/main.cpp index 24aaa5ed..5dbf144f 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -86,8 +86,6 @@ int main() uBit.display.setBrightness(display_brightness); disableLEDs(); - moveImageUntilEvent(MICROBIT_ID_GESTURE, MICROBIT_ACCELEROMETER_EVT_SHAKE, doublerow, 5, 1, 30); - //rainbow2(); //tests_run(); uBit.serial.send("Calliope Demo v3.0\r\n"); @@ -98,10 +96,13 @@ int main() // setStorageKey(KEY_DEMO); // begin of welcome DEMO program + startSound(); uBit.display.scroll("Hi"); + + // press A uBit.display.print("A"); uBit.sleep(pause); @@ -128,35 +129,18 @@ int main() uBit.display.clear(); // shake - moveImageUntilEvent(MICROBIT_ID_GESTURE, MICROBIT_ACCELEROMETER_EVT_SHAKE, doublerow, 3, 1, 50); + moveImageUntilEvent(MICROBIT_ID_GESTURE, MICROBIT_ACCELEROMETER_EVT_SHAKE, doublerow, 3, 1, 40); uBit.display.print(check); uBit.sleep(pause); uBit.display.clear(); uBit.display.scroll("OK!"); - // RGB LEDs - // startLEDs(); - disableLEDs(); - uBit.display.clear(); - - uBit.display.setBrightness(0); - uBit.display.print(smiley); - - for (int i = 0; i < display_brightness; i++) - { - uBit.display.setBrightness(i); - - uBit.sleep(10); - } - - for (int i = display_brightness; i > 0; i--) - { - uBit.display.setBrightness(i); - uBit.sleep(10); - } + // RGB LEDs and Smiley + rainbow2(); uBit.sleep(1000); + // } // end storage key DEMO menustate_t state = MenuStateOracle; diff --git a/source/samples/RunRockPaperScissors.cpp b/source/samples/RunRockPaperScissors.cpp index f6a93697..347aa036 100644 --- a/source/samples/RunRockPaperScissors.cpp +++ b/source/samples/RunRockPaperScissors.cpp @@ -24,13 +24,7 @@ void rockpaperscissors_run() leave = false; while (!leave) { - blinkImageUntilEvent( - MICROBIT_ID_GESTURE, - MICROBIT_ACCELEROMETER_EVT_SHAKE, - doublerow, - -1, - 2, - 100); + moveImageUntilEvent(MICROBIT_ID_GESTURE, MICROBIT_ACCELEROMETER_EVT_SHAKE, doublerow, 3, 1, 40); if (leave) { break; diff --git a/source/samples/Utils.cpp b/source/samples/Utils.cpp index cf1785c0..9071c9da 100644 --- a/source/samples/Utils.cpp +++ b/source/samples/Utils.cpp @@ -88,14 +88,9 @@ void blinkImageUntilEvent(const uint16_t source, const uint16_t value, const Mic // uBit.messageBus.ignore(source, value, eventHandler); // } -void moveImageUntilEvent( -const uint16_t source, -const uint16_t value, -const MicroBitImage &image, -// const uint16_t cycles, -const uint16_t offset, -const int asymetric, -const uint16_t delay) +void moveImageUntilEvent(const uint16_t source, const uint16_t value, const MicroBitImage &image, + // const uint16_t cycles, + const uint16_t offset, const int asymetric, const uint16_t delay) { int direction = 1; int x; @@ -106,14 +101,15 @@ const uint16_t delay) for (uint8_t i = 0; !leave && !event; i++) { - uBit.serial.send(x + "\r\n"); - x += direction; + x += direction; if (x >= offset + asymetric) - { - direction = -1; - } else if (x <= -offset ) { - direction = 1; - } + { + direction = -1; + } + else if (x <= -offset) + { + direction = 1; + } uBit.display.print(image, x, 0, 0, delay); } uBit.messageBus.ignore(source, value, eventHandler); @@ -286,6 +282,10 @@ void rainbow2() ManagedBuffer b(strip_length * 3); int delay = 1; // ms + int start_brightness = 0; + int cycle_length = 360; + int max_display_brightness = 100; + int display_brightness = 0; const uint8_t sins[360] = { 0, 0, 0, 0, 0, 1, 1, 2, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, @@ -309,32 +309,44 @@ void rainbow2() 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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; - /* fade leds in */ - // for (int j = 0; j < 100; j++) - // { - // for (size_t i = 0; i < 3; i++) - // { - // // - // b[0 + i * 3] = map(j, 0, 100, 0, map(sins[120 % 360], 0, 255, 0, brightness)); - // b[1 + i * 3] = map(j, 0, 100, 0, map(sins[(120 + i* 120) % 360], 0, 255, 0, - // brightness)); b[2 + i * 3] = map(j, 0, 100, 0, map(sins[(240 + i * 120) % 360], 0, - // 255, 0, brightness)); neopixel_send_buffer(uBit.io.RGB, b); uBit.sleep(1); - // } - // } - - for (int k = 0; k < 360; k++) + uBit.display.setBrightness(0); + uBit.display.print(smiley); + + for (int k = 0; k < cycle_length ; k++) { + if (k <= max_display_brightness) + { + display_brightness++; + } + if (k > cycle_length - (max_display_brightness + 1)) + { + display_brightness--; + } + uBit.display.setBrightness(display_brightness); + + if (k <= brightness) + { + start_brightness++; + + } + if (k > cycle_length - (brightness + 1)) + { + start_brightness--; + } for (int i = 0; i < strip_length; i++) { - // green red blue - b[0 + i * 3] = map(sins[(k + i * 120) % 360], 0, 255, 0, brightness); - b[1 + i * 3] = map(sins[(k + 120 + i * 120) % 360], 0, 255, 0, brightness); - b[2 + i * 3] = map(sins[(k + 240 + i * 120) % 360], 0, 255, 0, brightness); - neopixel_send_buffer(uBit.io.RGB, b); - uBit.sleep(delay); + b[0 + i * 3] = map(sins[(k + i * 120) % 360], 0, 255, 0, start_brightness); + b[1 + i * 3] = map(sins[(k + 120 + i * 120) % 360], 0, 255, 0, start_brightness); + b[2 + i * 3] = map(sins[(k + 240 + i * 120) % 360], 0, 255, 0, start_brightness); } + uBit.serial.send(start_brightness + "\r\n"); + neopixel_send_buffer(uBit.io.RGB, b); + uBit.sleep(delay); } + disableLEDs(); + uBit.display.clear(); + uBit.display.setBrightness(max_display_brightness); } bool mute_mic = false; From c490c9afbe4ec224aa9805585285ea88e826af1b Mon Sep 17 00:00:00 2001 From: Jasper Date: Wed, 30 Aug 2023 15:35:48 +0200 Subject: [PATCH 08/18] leds on when back to menu --- source/main.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/source/main.cpp b/source/main.cpp index 5dbf144f..acae4b00 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -164,33 +164,38 @@ int main() menuAnimateEnter(); oracle_run(); menuAnimateLeave(); + LEDcounter(state); break; // 2 case MenuStateRockPaperScissors: menuAnimateEnter(); rockpaperscissors_run(); menuAnimateLeave(); + LEDcounter(state); break; // 3 case MenuStateMultiplication: menuAnimateEnter(); multiplication_run(); menuAnimateLeave(); + LEDcounter(state); break; // 4 case MenuStateVolumeMeter: menuAnimateEnter(); volumemeter_run(); menuAnimateLeave(); + LEDcounter(state); break; // 5 case MenuStateInterpreter: menuAnimateEnter(); programfive_run(); menuAnimateLeave(); + LEDcounter(state); break; } - LEDcounter(state); + //LEDcounter(state); } } From ae6101fc87442e73ae09b9ffb9ee2ae07e859fa4 Mon Sep 17 00:00:00 2001 From: fabianhugo Date: Tue, 12 Sep 2023 14:54:51 +0200 Subject: [PATCH 09/18] update RGB animation to hsv cycle --- source/main.cpp | 2 +- source/samples/Utils.cpp | 137 ++++++++++++++++++++++++++++++--------- 2 files changed, 108 insertions(+), 31 deletions(-) diff --git a/source/main.cpp b/source/main.cpp index acae4b00..ef3cb819 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -137,7 +137,7 @@ int main() uBit.display.scroll("OK!"); // RGB LEDs and Smiley - rainbow2(); + rainbow(); uBit.sleep(1000); diff --git a/source/samples/Utils.cpp b/source/samples/Utils.cpp index 9071c9da..e3378325 100644 --- a/source/samples/Utils.cpp +++ b/source/samples/Utils.cpp @@ -237,43 +237,120 @@ void LEDcounter(int number) } } -void rainbow() +#define MAX(x, y) (((x) > (y)) ? (x) : (y)) +#define MIN(x, y) (((x) < (y)) ? (x) : (y)) +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 hsv rgb2hsv(rgb in); +static rgb hsv2rgb(hsv in); + +rgb hsv2rgb(hsv in) { - uint16_t i, j; - int WheelPos; + 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); - for (j = 0; j < 250; j++) - { - for (i = 0; i < 3; i++) - { - WheelPos = i + j; - WheelPos = 255 - WheelPos; - if (WheelPos < 85) - { - b[0 + i * 3] = (255 - WheelPos * 3) / 10; - b[1 + i * 3] = (0) / 10; - b[2 + i * 3] = (WheelPos * 3) / 10; - } - if (WheelPos < 170) - { - WheelPos -= 85; - b[0 + i * 3] = (0) / 10; - b[1 + i * 3] = (WheelPos * 3) / 10; - b[2 + i * 3] = (255 - WheelPos * 3) / 10; - } - if (WheelPos > 170 && WheelPos < 240) - { - WheelPos -= 170; - b[0 + i * 3] = (WheelPos * 3) / 10; - b[1 + i * 3] = (255 - WheelPos * 3) / 10; - b[2 + i * 3] = (0) / 10; - } } 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 + } void rainbow2() { From ec09b6991f84fc0175226c79e651e1ed25737cd1 Mon Sep 17 00:00:00 2001 From: fabianhugo Date: Tue, 12 Sep 2023 16:27:00 +0200 Subject: [PATCH 10/18] adds persistent counter, that keeps track which program part should run after reboot --- source/main.cpp | 115 +++++++++++++++++++----------------- source/samples/RunTests.cpp | 3 +- source/samples/Utils.cpp | 3 - 3 files changed, 62 insertions(+), 59 deletions(-) diff --git a/source/main.cpp b/source/main.cpp index ef3cb819..d01f54e4 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -86,63 +86,70 @@ int main() uBit.display.setBrightness(display_brightness); disableLEDs(); - //tests_run(); - + int stored; uBit.serial.send("Calliope Demo v3.0\r\n"); + KeyValuePair* firstTime = uBit.storage.get("counter"); + // check if Test has already been done + if (firstTime == NULL) + { + tests_run(); // Perform test + stored = 1; + uBit.storage.put("counter", (uint8_t *)&stored, sizeof(int)); + uBit.sleep(1000); + } + else{ + memcpy(&stored, firstTime->value, sizeof(int)); - // check if DEMO has already been played - // if (!hasStorageKey(KEY_DEMO)) - // { - // setStorageKey(KEY_DEMO); - - // begin of welcome DEMO program - - startSound(); - - uBit.display.scroll("Hi"); - - - - // press A - uBit.display.print("A"); - uBit.sleep(pause); - blinkImageUntilEvent(MICROBIT_ID_BUTTON_A, MICROBIT_BUTTON_EVT_CLICK, arrow_left); - uBit.display.print(check); - uBit.sleep(pause); - uBit.display.clear(); - - // press B - uBit.display.print("B"); - uBit.sleep(pause); - blinkImageUntilEvent(MICROBIT_ID_BUTTON_B, MICROBIT_BUTTON_EVT_CLICK, arrow_right); - uBit.display.print(check); - uBit.sleep(pause); - uBit.display.clear(); - - // press A+B - uBit.display.scroll("A+B"); - uBit.sleep(pause); - blinkImageUntilEvent(MICROBIT_ID_BUTTON_AB, MICROBIT_BUTTON_EVT_CLICK, arrow_left_right); - beep(); - uBit.display.print(check); - uBit.sleep(pause); - uBit.display.clear(); - - // shake - moveImageUntilEvent(MICROBIT_ID_GESTURE, MICROBIT_ACCELEROMETER_EVT_SHAKE, doublerow, 3, 1, 40); - uBit.display.print(check); - uBit.sleep(pause); - uBit.display.clear(); - - uBit.display.scroll("OK!"); - - // RGB LEDs and Smiley - rainbow(); - - uBit.sleep(1000); - - // } // end storage key DEMO + } + if (stored == 1){ + + // begin of welcome DEMO program + startSound(); + + uBit.display.scroll("Hi"); + + // press A + uBit.display.print("A"); + uBit.sleep(pause); + blinkImageUntilEvent(MICROBIT_ID_BUTTON_A, MICROBIT_BUTTON_EVT_CLICK, arrow_left); + uBit.display.print(check); + uBit.sleep(pause); + uBit.display.clear(); + + // press B + uBit.display.print("B"); + uBit.sleep(pause); + blinkImageUntilEvent(MICROBIT_ID_BUTTON_B, MICROBIT_BUTTON_EVT_CLICK, arrow_right); + uBit.display.print(check); + uBit.sleep(pause); + uBit.display.clear(); + + // press A+B + uBit.display.scroll("A+B"); + uBit.sleep(pause); + blinkImageUntilEvent(MICROBIT_ID_BUTTON_AB, MICROBIT_BUTTON_EVT_CLICK, arrow_left_right); + beep(); + uBit.display.print(check); + uBit.sleep(pause); + uBit.display.clear(); + + // shake + moveImageUntilEvent(MICROBIT_ID_GESTURE, MICROBIT_ACCELEROMETER_EVT_SHAKE, doublerow, 3, 1, 40); + uBit.display.print(check); + uBit.sleep(pause); + uBit.display.clear(); + + uBit.display.scroll("OK!"); + + // RGB LEDs and Smiley + rainbow(); + + stored = 2; + uBit.sleep(1000); + uBit.storage.put("counter", (uint8_t *)&stored, sizeof(int)); + } // End of Welcome Demo Programm + // Beginn of program menu menustate_t state = MenuStateOracle; while (true) diff --git a/source/samples/RunTests.cpp b/source/samples/RunTests.cpp index 2ba9c44e..f07d8dea 100644 --- a/source/samples/RunTests.cpp +++ b/source/samples/RunTests.cpp @@ -58,8 +58,7 @@ void tests_run() { uBit.display.clear(); } - uBit.sleep(3000); - uBit.serial.send("end test"); + uBit.serial.send("end test\r\n"); uBit.display.clear(); disableLEDs(); } \ No newline at end of file diff --git a/source/samples/Utils.cpp b/source/samples/Utils.cpp index e3378325..fddb4937 100644 --- a/source/samples/Utils.cpp +++ b/source/samples/Utils.cpp @@ -237,8 +237,6 @@ void LEDcounter(int number) } } -#define MAX(x, y) (((x) > (y)) ? (x) : (y)) -#define MIN(x, y) (((x) < (y)) ? (x) : (y)) typedef struct { double r; // a fraction between 0 and 1 double g; // a fraction between 0 and 1 @@ -251,7 +249,6 @@ typedef struct { double v; // a fraction between 0 and 1 } hsv; -static hsv rgb2hsv(rgb in); static rgb hsv2rgb(hsv in); rgb hsv2rgb(hsv in) From 202b0e55d9be7fb334d9a5f2ea995229ad3d6fc8 Mon Sep 17 00:00:00 2001 From: fabianhugo Date: Thu, 28 Sep 2023 16:47:57 +0200 Subject: [PATCH 11/18] optimizes test routine, as soon as the check symbol is on the display, the device can be disconnected --- source/main.cpp | 7 ++++--- source/samples/RunTests.cpp | 2 -- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/source/main.cpp b/source/main.cpp index d01f54e4..73db35e6 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -93,9 +93,10 @@ int main() if (firstTime == NULL) { tests_run(); // Perform test - stored = 1; - uBit.storage.put("counter", (uint8_t *)&stored, sizeof(int)); - uBit.sleep(1000); + stored = 1; // Set persistent counter so next boot will start in user program + uBit.storage.put("counter", (uint8_t *)&stored, sizeof(int)); + uBit.sleep(2000); + uBit.display.clear(); } else{ memcpy(&stored, firstTime->value, sizeof(int)); diff --git a/source/samples/RunTests.cpp b/source/samples/RunTests.cpp index f07d8dea..bbd32222 100644 --- a/source/samples/RunTests.cpp +++ b/source/samples/RunTests.cpp @@ -52,13 +52,11 @@ void tests_run() { uBit.serial.send("accelerometer z-axis not 0\r\n"); uBit.display.print(check); - uBit.sleep(1000); } else { uBit.display.clear(); } uBit.serial.send("end test\r\n"); - uBit.display.clear(); disableLEDs(); } \ No newline at end of file From 42fa777c062f21867837690b4f5c323383d41397 Mon Sep 17 00:00:00 2001 From: Jasper Precht <72135430+jasperp92@users.noreply.github.com> Date: Wed, 11 Oct 2023 17:32:56 +0200 Subject: [PATCH 12/18] taking out program 5, fixing counter --- source/main.cpp | 21 ++++++++++----------- source/samples/Menu.cpp | 1 - source/samples/Menu.h | 4 ++-- source/samples/Utils.cpp | 13 +++++++------ 4 files changed, 19 insertions(+), 20 deletions(-) diff --git a/source/main.cpp b/source/main.cpp index 73db35e6..341c7865 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -172,38 +172,37 @@ int main() menuAnimateEnter(); oracle_run(); menuAnimateLeave(); - LEDcounter(state); + LEDcounter(1); break; // 2 case MenuStateRockPaperScissors: menuAnimateEnter(); rockpaperscissors_run(); menuAnimateLeave(); - LEDcounter(state); + LEDcounter(2); break; // 3 case MenuStateMultiplication: menuAnimateEnter(); multiplication_run(); menuAnimateLeave(); - LEDcounter(state); + LEDcounter(3); break; // 4 case MenuStateVolumeMeter: menuAnimateEnter(); volumemeter_run(); menuAnimateLeave(); - LEDcounter(state); + LEDcounter(4); break; // 5 - case MenuStateInterpreter: - menuAnimateEnter(); - programfive_run(); - menuAnimateLeave(); - LEDcounter(state); - break; + // case MenuStateInterpreter: + // menuAnimateEnter(); + // programfive_run(); + // menuAnimateLeave(); + // LEDcounter(state); + // break; } - //LEDcounter(state); } } diff --git a/source/samples/Menu.cpp b/source/samples/Menu.cpp index 8351a53f..3231f6f2 100644 --- a/source/samples/Menu.cpp +++ b/source/samples/Menu.cpp @@ -26,7 +26,6 @@ menustate_t menuWaitForChoice(menustate_t start) uBit.display.print(ManagedString(state)); LEDcounter(state); - // event loop while (true) { // button A diff --git a/source/samples/Menu.h b/source/samples/Menu.h index cfa3b0e2..bd2befb8 100644 --- a/source/samples/Menu.h +++ b/source/samples/Menu.h @@ -6,9 +6,9 @@ typedef enum MenuState { MenuStateRockPaperScissors, MenuStateMultiplication, MenuStateVolumeMeter, - MenuStateInterpreter, + // MenuStateInterpreter, MenuStateMin = MenuStateOracle, - MenuStateMax = MenuStateInterpreter + MenuStateMax = MenuStateVolumeMeter } menustate_t; menustate_t menuWaitForChoice(menustate_t start); diff --git a/source/samples/Utils.cpp b/source/samples/Utils.cpp index fddb4937..2a06b2a1 100644 --- a/source/samples/Utils.cpp +++ b/source/samples/Utils.cpp @@ -200,7 +200,7 @@ void startLEDs() void turnLEDon(int start_index, int end_index) { - brightness = 20; + //brightness = 20; ManagedBuffer b(strip_length * 3); b.fill(0); for (int i = start_index * 3; i < end_index * 3; i++) @@ -224,17 +224,18 @@ void LEDcounter(int number) break; // 3 case 3: - turnLEDon(0, 3); + turnLEDon(1, 3); break; // 4 case 4: - turnLEDon(1, 3); - break; - // 5 - case 5: turnLEDon(2, 3); break; + // 5 + // case 5: + // turnLEDon(2, 3); + // break; } + uBit.sleep(10); } typedef struct { From 7188973b7ac73cc007cbe533b2553f7d427467af Mon Sep 17 00:00:00 2001 From: Jasper Precht <72135430+jasperp92@users.noreply.github.com> Date: Wed, 11 Oct 2023 17:54:07 +0200 Subject: [PATCH 13/18] reduce brightness, remove old rainbow function --- source/samples/Utils.cpp | 76 +--------------------------------------- 1 file changed, 1 insertion(+), 75 deletions(-) diff --git a/source/samples/Utils.cpp b/source/samples/Utils.cpp index 2a06b2a1..22bdd1b4 100644 --- a/source/samples/Utils.cpp +++ b/source/samples/Utils.cpp @@ -123,7 +123,7 @@ void shakeEvent(MicroBitEvent) } // LED functions -int brightness = 32; +int brightness = 20; const int strip_length = 3; uint16_t pause = 300; @@ -350,80 +350,6 @@ void rainbow(void){ uBit.display.setBrightness(100); // restore brightness } -void rainbow2() -{ - // Cycle a single RGB led using the sine fade technique - // For common cathode led - - ManagedBuffer b(strip_length * 3); - int delay = 1; // ms - int start_brightness = 0; - int cycle_length = 360; - int max_display_brightness = 100; - int display_brightness = 0; - - const uint8_t sins[360] = { - 0, 0, 0, 0, 0, 1, 1, 2, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, - 13, 15, 17, 18, 20, 22, 24, 26, 28, 30, 32, 35, 37, 39, 42, 44, 47, 49, - 52, 55, 58, 60, 63, 66, 69, 72, 75, 78, 81, 85, 88, 91, 94, 97, 101, 104, - 107, 111, 114, 117, 121, 124, 127, 131, 134, 137, 141, 144, 147, 150, 154, 157, 160, 163, - 167, 170, 173, 176, 179, 182, 185, 188, 191, 194, 197, 200, 202, 205, 208, 210, 213, 215, - 217, 220, 222, 224, 226, 229, 231, 232, 234, 236, 238, 239, 241, 242, 244, 245, 246, 248, - 249, 250, 251, 251, 252, 253, 253, 254, 254, 255, 255, 255, 255, 255, 255, 255, 254, 254, - 253, 253, 252, 251, 251, 250, 249, 248, 246, 245, 244, 242, 241, 239, 238, 236, 234, 232, - 231, 229, 226, 224, 222, 220, 217, 215, 213, 210, 208, 205, 202, 200, 197, 194, 191, 188, - 185, 182, 179, 176, 173, 170, 167, 163, 160, 157, 154, 150, 147, 144, 141, 137, 134, 131, - 127, 124, 121, 117, 114, 111, 107, 104, 101, 97, 94, 91, 88, 85, 81, 78, 75, 72, - 69, 66, 63, 60, 58, 55, 52, 49, 47, 44, 42, 39, 37, 35, 32, 30, 28, 26, - 24, 22, 20, 18, 17, 15, 13, 12, 11, 9, 8, 7, 6, 5, 4, 3, 2, 2, - 1, 1, 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, 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, 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, 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, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; - - uBit.display.setBrightness(0); - uBit.display.print(smiley); - - for (int k = 0; k < cycle_length ; k++) - { - if (k <= max_display_brightness) - { - display_brightness++; - } - if (k > cycle_length - (max_display_brightness + 1)) - { - display_brightness--; - } - uBit.display.setBrightness(display_brightness); - - if (k <= brightness) - { - start_brightness++; - - } - if (k > cycle_length - (brightness + 1)) - { - start_brightness--; - } - - for (int i = 0; i < strip_length; i++) - { - b[0 + i * 3] = map(sins[(k + i * 120) % 360], 0, 255, 0, start_brightness); - b[1 + i * 3] = map(sins[(k + 120 + i * 120) % 360], 0, 255, 0, start_brightness); - b[2 + i * 3] = map(sins[(k + 240 + i * 120) % 360], 0, 255, 0, start_brightness); - } - uBit.serial.send(start_brightness + "\r\n"); - neopixel_send_buffer(uBit.io.RGB, b); - uBit.sleep(delay); - } - disableLEDs(); - uBit.display.clear(); - uBit.display.setBrightness(max_display_brightness); -} - bool mute_mic = false; void playsound(int freq) From 3a45c6c529e787e1d89c2c55bba61741373365c8 Mon Sep 17 00:00:00 2001 From: fabianhugo Date: Mon, 13 Nov 2023 14:50:11 +0100 Subject: [PATCH 14/18] revert to default codal.json settings --- codal.ble.json | 5 ++--- codal.json | 10 ++-------- 2 files changed, 4 insertions(+), 11 deletions(-) diff --git a/codal.ble.json b/codal.ble.json index be4cd4f5..fc7a1332 100644 --- a/codal.ble.json +++ b/codal.ble.json @@ -2,7 +2,7 @@ "target": { "name": "codal-microbit-v2", "url": "https://github.com/calliope-edu/codal-microbit-v2", - "branch": "calliope", + "branch": "v0.2.57-calliope-1.5", "type": "git" }, "config":{ @@ -13,7 +13,6 @@ "MICROBIT_BLE_DEVICE_INFORMATION_SERVICE": 1, "MICROBIT_BLE_EVENT_SERVICE" : 1, "MICROBIT_BLE_PARTIAL_FLASHING" : 1, - "MICROBIT_BLE_OPEN" : 1, - "MICROBIT_BLE_SECURITY_LEVEL": null + "MICROBIT_BLE_SECURITY_LEVEL": "SECURITY_MODE_ENCRYPTION_NO_MITM" } } diff --git a/codal.json b/codal.json index e1ab3651..302c50e1 100644 --- a/codal.json +++ b/codal.json @@ -6,13 +6,7 @@ "type": "git" }, "config":{ - "DEVICE_BLE": 1, - "MICROBIT_BLE_ENABLED" : 1, - "MICROBIT_BLE_PAIRING_MODE": 1, - "MICROBIT_BLE_DFU_SERVICE": 1, - "MICROBIT_BLE_DEVICE_INFORMATION_SERVICE": 1, - "MICROBIT_BLE_EVENT_SERVICE" : 1, - "MICROBIT_BLE_PARTIAL_FLASHING" : 1, - "MICROBIT_BLE_SECURITY_LEVEL" : "SECURITY_MODE_ENCRYPTION_NO_MITM" + "MICROBIT_BLE_ENABLED" : 0, + "MICROBIT_BLE_PAIRING_MODE": 0 } } From 6e4178e1f432e6446bd35fad0b0ab2fedc6eaebb Mon Sep 17 00:00:00 2001 From: fabianhugo Date: Tue, 14 Nov 2023 13:17:57 +0100 Subject: [PATCH 15/18] implement test routine and reset to test combination tiny update --- source/main.cpp | 6 ++++-- source/samples/GPIOTest.cpp | 17 +++++++++++++---- source/samples/GPIOTest.h | 7 +++++++ source/samples/Menu.cpp | 7 ++++++- source/samples/RunTests.cpp | 6 ++++++ 5 files changed, 36 insertions(+), 7 deletions(-) create mode 100644 source/samples/GPIOTest.h diff --git a/source/main.cpp b/source/main.cpp index 341c7865..840bb431 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -86,9 +86,11 @@ int main() uBit.display.setBrightness(display_brightness); disableLEDs(); - int stored; uBit.serial.send("Calliope Demo v3.0\r\n"); - KeyValuePair* firstTime = uBit.storage.get("counter"); + + int stored; + // stored = 1; uBit.storage.put("counter", (uint8_t *)&stored, sizeof(int)); // Skip Test Sequence + KeyValuePair* firstTime = uBit.storage.get("counter"); // check if Test has already been done if (firstTime == NULL) { diff --git a/source/samples/GPIOTest.cpp b/source/samples/GPIOTest.cpp index 4ab1b472..7e6ba3e1 100644 --- a/source/samples/GPIOTest.cpp +++ b/source/samples/GPIOTest.cpp @@ -21,9 +21,14 @@ Pin *edgeConnector[] = { &uBit.io.P14, &uBit.io.P15, &uBit.io.A1RX, - &uBit.io.A1TX, + &uBit.io.A1TX, + &uBit.io.P18, &uBit.io.A0SCL, - &uBit.io.A0SDA + &uBit.io.A0SDA, + &uBit.io.M_A_IN1, + &uBit.io.M_A_IN2, + &uBit.io.M_B_IN1, + &uBit.io.M_B_IN2 }; //Pin *analogPins[] = {&uBit.io.P1, &uBit.io.P2}; @@ -48,10 +53,14 @@ edge_connector_test() for (Pin *p : edgeConnector) { p->setDigitalValue(1); - uBit.sleep(500); + } + uBit.sleep(300); + for (Pin *p : edgeConnector) + { p->setDigitalValue(0); - uBit.sleep(500); } + uBit.sleep(300); + } } diff --git a/source/samples/GPIOTest.h b/source/samples/GPIOTest.h new file mode 100644 index 00000000..da12137b --- /dev/null +++ b/source/samples/GPIOTest.h @@ -0,0 +1,7 @@ +#include "MicroBit.h" +#include "Tests.h" +#include "NRF52PWM.h" +#include "MemorySource.h" + + +void edge_connector_test(); \ No newline at end of file diff --git a/source/samples/Menu.cpp b/source/samples/Menu.cpp index 3231f6f2..69bc885b 100644 --- a/source/samples/Menu.cpp +++ b/source/samples/Menu.cpp @@ -59,7 +59,12 @@ menustate_t menuWaitForChoice(menustate_t start) } return state; } - + // reset to test mode by touching all 4 edges + if ((uBit.io.P0.isTouched(TouchMode::Capacitative)==1) && (uBit.io.P1.isTouched(TouchMode::Capacitative)==1) && (uBit.io.P2.isTouched(TouchMode::Capacitative)==1)&& (uBit.io.P3.isTouched(TouchMode::Capacitative)==1)){ + uBit.storage.remove("counter"); + uBit.display.print("TestMode"); + uBit.reset(); + } uBit.sleep(10); } } diff --git a/source/samples/RunTests.cpp b/source/samples/RunTests.cpp index bbd32222..956d895c 100644 --- a/source/samples/RunTests.cpp +++ b/source/samples/RunTests.cpp @@ -4,6 +4,7 @@ //#include "MicroBitSerial.h" #include "Images.h" #include "Utils.h" +#include "GPIOTest.h" extern MicroBit uBit; @@ -46,6 +47,11 @@ void tests_run() playsound(0); break; } + if (uBit.buttonB.isPressed()) + { + uBit.serial.send("button B pressed\r\n"); + edge_connector_test(); + } } if (uBit.accelerometer.getZ() != 0) From 4cea26107788aa921d9811cefd2f6e2d1cbcbb07 Mon Sep 17 00:00:00 2001 From: fabianhugo Date: Wed, 15 Nov 2023 16:41:33 +0100 Subject: [PATCH 16/18] set reset to test to resitive touch mode --- source/samples/Menu.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/samples/Menu.cpp b/source/samples/Menu.cpp index 69bc885b..13b79335 100644 --- a/source/samples/Menu.cpp +++ b/source/samples/Menu.cpp @@ -60,7 +60,7 @@ menustate_t menuWaitForChoice(menustate_t start) return state; } // reset to test mode by touching all 4 edges - if ((uBit.io.P0.isTouched(TouchMode::Capacitative)==1) && (uBit.io.P1.isTouched(TouchMode::Capacitative)==1) && (uBit.io.P2.isTouched(TouchMode::Capacitative)==1)&& (uBit.io.P3.isTouched(TouchMode::Capacitative)==1)){ + if (uBit.io.P0.isTouched() && uBit.io.P1.isTouched() && uBit.io.P2.isTouched() && uBit.io.P3.isTouched()){ uBit.storage.remove("counter"); uBit.display.print("TestMode"); uBit.reset(); From 1c621d17072e22b62e0212bdc1b02a2a6b4bb7e0 Mon Sep 17 00:00:00 2001 From: fabianhugo Date: Wed, 15 Nov 2023 17:16:45 +0100 Subject: [PATCH 17/18] make reset to test harder --- source/samples/Menu.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/samples/Menu.cpp b/source/samples/Menu.cpp index 13b79335..51e2a6ce 100644 --- a/source/samples/Menu.cpp +++ b/source/samples/Menu.cpp @@ -60,7 +60,7 @@ menustate_t menuWaitForChoice(menustate_t start) return state; } // reset to test mode by touching all 4 edges - if (uBit.io.P0.isTouched() && uBit.io.P1.isTouched() && uBit.io.P2.isTouched() && uBit.io.P3.isTouched()){ + if ((uBit.accelerometer.getZ() > 900) && uBit.io.P0.isTouched() && uBit.io.P1.isTouched() && uBit.io.P2.isTouched() && uBit.io.P3.isTouched()){ uBit.storage.remove("counter"); uBit.display.print("TestMode"); uBit.reset(); From 0d6afa3f58c0f83c309af36eb2d2865d8a50fd75 Mon Sep 17 00:00:00 2001 From: fabianhugo Date: Mon, 27 Nov 2023 16:31:10 +0100 Subject: [PATCH 18/18] adds possibility to skip production test --- .github/workflows/build.yml | 30 +++++++++++++++++++++--------- source/main.cpp | 8 +++++--- source/samples/Menu.cpp | 10 ++++++---- 3 files changed, 32 insertions(+), 16 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 53b94f1e..eef8216f 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -1,4 +1,4 @@ -name: Build Natively +name: Build Natively on Ubuntu on: push: @@ -10,8 +10,8 @@ jobs: build-py-script: strategy: matrix: - os: [ubuntu-20.04, macos-11, windows-2019] - gcc: ['7-2017-q4', 'latest'] + os: [ubuntu-20.04] #, macos-11, windows-2019] + gcc: ['latest'] # '7-2017-q4' cmake: ['3.6.0', ''] # Empty string installs the latest CMake release fail-fast: false runs-on: ${{ matrix.os }} @@ -43,20 +43,32 @@ jobs: - name: Upload hex file uses: actions/upload-artifact@v1 with: - name: build-py-${{ matrix.os }} + name: build-py-NO_BLE-${{ matrix.os }} path: MICROBIT.hex - - name: Prepare BLE example + - name: Prepare production example (BLE) run: | rm codal.json mv codal.ble.json codal.json - python -c "import pathlib; \ - f=pathlib.Path('source/main.cpp'); \ - f.write_text(f.read_text().replace('out_of_box_experience()', 'ble_test()'))" + # python -c "import pathlib; \ + # f=pathlib.Path('source/main.cpp'); \ + # f.write_text(f.read_text().replace('out_of_box_experience()', 'ble_test()'))" cat codal.json - name: Build BLE project using build.py run: python build.py --clean - name: Upload BLE hex file uses: actions/upload-artifact@v1 with: - name: build-py-BLE-${{ matrix.os }} + name: build-py-production-${{ matrix.os }} + path: MICROBIT.hex + - name: Prepare user example (BLE) (skip test) + run: | + python -c "import pathlib; \ + f=pathlib.Path('source/main.cpp'); \ + f.write_text(f.read_text().replace('int productiontest=1;', 'int productiontest=0;'))" + - name: Build BLE project using build.py + run: python build.py --clean + - name: Upload BLE hex file + uses: actions/upload-artifact@v1 + with: + name: build-py-user-${{ matrix.os }} path: MICROBIT.hex diff --git a/source/main.cpp b/source/main.cpp index 840bb431..ed549892 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -89,15 +89,17 @@ int main() uBit.serial.send("Calliope Demo v3.0\r\n"); int stored; - // stored = 1; uBit.storage.put("counter", (uint8_t *)&stored, sizeof(int)); // Skip Test Sequence + int productiontest=1; KeyValuePair* firstTime = uBit.storage.get("counter"); // check if Test has already been done if (firstTime == NULL) { - tests_run(); // Perform test + if (productiontest==1){ + tests_run();} // Perform test stored = 1; // Set persistent counter so next boot will start in user program uBit.storage.put("counter", (uint8_t *)&stored, sizeof(int)); - uBit.sleep(2000); + if (productiontest==1){ + uBit.sleep(2000);} uBit.display.clear(); } else{ diff --git a/source/samples/Menu.cpp b/source/samples/Menu.cpp index 51e2a6ce..5ed50de1 100644 --- a/source/samples/Menu.cpp +++ b/source/samples/Menu.cpp @@ -1,6 +1,7 @@ #include "Menu.h" #include "MicroBit.h" #include "Utils.h" +#include "RunTests.h" extern MicroBit uBit; @@ -59,11 +60,12 @@ menustate_t menuWaitForChoice(menustate_t start) } return state; } - // reset to test mode by touching all 4 edges + // reset to test mode by touching all 4 edges and turning the mini upside down if ((uBit.accelerometer.getZ() > 900) && uBit.io.P0.isTouched() && uBit.io.P1.isTouched() && uBit.io.P2.isTouched() && uBit.io.P3.isTouched()){ - uBit.storage.remove("counter"); - uBit.display.print("TestMode"); - uBit.reset(); + uBit.storage.remove("counter"); + uBit.display.scroll("tester"); + tests_run(); + uBit.reset(); } uBit.sleep(10); }