Arijit Das
Published © MIT

Codecraft: Graphical Programming for Embedded ML

Seeed Studio's Codecraft now gets powered by Edge Impulse leading to the development of next-generation of developing embedded ML projects!

BeginnerProtip1 hour1,023
Codecraft: Graphical Programming for Embedded ML

Things used in this project

Hardware components

Wio Terminal
Seeed Studio Wio Terminal
×1

Software apps and online services

Seeed Studio Codecraft
Edge Impulse Studio
Edge Impulse Studio

Story

Read more

Schematics

Final Outcome

Code

Final Code

C/C++
#include"TFT_eSPI.h"
#include <project_46617_inferencing.h>
#include"LIS3DHTR.h"

TFT_eSPI tft;
LIS3DHTR<TwoWire> lis;
#define CONVERT_G_TO_MS2    9.80665f
ei_impulse_result_classification_t currentClassification[EI_CLASSIFIER_LABEL_COUNT];
const char* maxConfidenceLabel;
void ei_printf(const char *format, ...) {
    static char print_buf[1024] = { 0 };

    va_list args;
    va_start(args, format);
    int r = vsnprintf(print_buf, sizeof(print_buf), format, args);
    va_end(args);

    if (r > 0) {
        Serial.write(print_buf);
    }
}

void runClassifier()
{
    float buffer[EI_CLASSIFIER_DSP_INPUT_FRAME_SIZE] = { 0 };
    for (size_t ix = 0; ix < EI_CLASSIFIER_DSP_INPUT_FRAME_SIZE; ix += 3) {
        // Determine the next tick (and then sleep later)
        uint64_t next_tick = micros() + (EI_CLASSIFIER_INTERVAL_MS * 1000);
        lis.getAcceleration(&buffer[ix], &buffer[ix + 1], &buffer[ix + 2]);
        buffer[ix + 0] *= CONVERT_G_TO_MS2;
        buffer[ix + 1] *= CONVERT_G_TO_MS2;
        buffer[ix + 2] *= CONVERT_G_TO_MS2;
        delayMicroseconds(next_tick - micros());
    }
    signal_t signal;
    int err = numpy:: signal_from_buffer(buffer, EI_CLASSIFIER_DSP_INPUT_FRAME_SIZE, &signal);
    ei_impulse_result_t result = { 0 };

    err = run_classifier(&signal, &result, false);
    float maxValue = 0;
    for (size_t ix = 0; ix < EI_CLASSIFIER_LABEL_COUNT; ix++) {
        ei_impulse_result_classification_t classification_t = result.classification[ix];
        ei_printf("    %s: %.5f\n", classification_t.label, classification_t.value);
        float value = classification_t.value;
        if (value > maxValue) {
            maxValue = value;
            maxConfidenceLabel = classification_t.label;
        }
        currentClassification[ix] = classification_t;
    }
}

void setup(){
    tft.begin();
    lis.begin(Wire1);
    lis.setOutputDataRate(LIS3DHTR_DATARATE_100HZ);
    lis.setFullScaleRange(LIS3DHTR_RANGE_4G);

  tft.setRotation(3);
  tft.fillScreen(0xFD00);
  tft.setTextSize(4);
  tft.setTextColor(0x0);

}



void loop(){

  runClassifier();
  tft.drawRect(10,10,100,50,0x4FF);
  tft.fillRect(10,10,100,50,0x4FF);
  tft.drawString((String)maxConfidenceLabel, 10, 10);
  Serial.println(maxConfidenceLabel);

}

Credits

Arijit Das

Arijit Das

10 projects • 60 followers
16 y/o • Community Expert @Edge Impulse • Co-Organizer @tinyML India • Speaker @GitHub, @Docker, @Linaro
Thanks to Seeed Studio.

Comments