Skip to content

Codegen on .ino file #486

@MirkoCalvi

Description

@MirkoCalvi

Feature Description

Create a build step that returns a minimal .ino file that imports Zant static library and calls its methods. It must also set the correct input shape and output size.
If XIP is enabled, it must also ensure that the peripherals are correctly declared.

I suggest to add a -Dgen_ino option in zig build lib-gen step that codegenerates the file and put it in the same folder of the other generated .zig files .

Here it is an example of the generated .ino file:

#include <Arduino.h>
#include <lib_zant.h> // int predict(float*, uint32_t*, uint32_t, float**)

// ---- Predict parameters ----
#ifndef ZANT_OUTPUT_LEN
#define ZANT_OUTPUT_LEN 1*2*17*16 // <<<<<<<<<<<<<<<< ensure it is correct !!
#endif
static const uint32_t OUT_LEN = ZANT_OUTPUT_LEN;
static const uint32_t IN_N = 1; // <<<<<<<<<<<<<<<< ensure it is correct !!
static const uint32_t IN_C = 1; // <<<<<<<<<<<<<<<< ensure it is correct !!
static const uint32_t IN_H = 17; // <<<<<<<<<<<<<<<< ensure it is correct !!
static const uint32_t IN_W = 16; // <<<<<<<<<<<<<<<< ensure it is correct !!
static const uint32_t IN_SIZE = IN_N * IN_C * IN_H * IN_W;
static u_int8_t inputData[IN_SIZE];
static uint32_t inputShape[4] = {IN_N, IN_C, IN_H, IN_W};

static void printOutput(const u_int8_t *out, uint32_t len)
{
    if (!out || len <= 0)
    {
        Serial.println("Output nullo");
        return;
    }
    Serial.println("=== Output ===");
    for (int i = 0; i < len; ++i)
    {
        Serial.print("out[");
        Serial.print(i);
        Serial.print("] = ");
        Serial.println(out[i], 6);
    }
    Serial.println("==============");
}

void setup()
{
    Serial.begin(115200);
    uint32_t t0 = millis();
    while (!Serial && (millis() - t0) < 4000)
        delay(10);
    Serial.println("\n== Nicla Vision ==");

    // Prepare NCHW input 
    for (uint32_t c = 0; c < IN_C; ++c)
        for (uint32_t h = 0; h < IN_H; ++h)
            for (uint32_t w = 0; w < IN_W; ++w)
            {
                uint32_t idx = c * (IN_H * IN_W) + h * IN_W + w;
                inputData[idx] = 1;
            }
}

void loop() { 
    
    u_int8_t *out = nullptr;
    Serial.println("[Predict] Calling predict()...");
    int rc = -3 ;
    unsigned long average_sum = 0;

    for(uint32_t i = 0; i<10; i++) {
        unsigned long t_us0 = micros();
        rc = predict(inputData, inputShape, 4, &out);
        unsigned long t_us1 = micros();
        average_sum = average_sum + t_us1 - t_us0;
        if(rc!=0) break;
    }

    if (rc == 0 && out)
    {
        printOutput(out, OUT_LEN);
    }
    else
    {
        Serial.println("[Predict] FAIL");
    }

    Serial.print("[Predict] rc=");
    Serial.println(rc);
    Serial.print("[Predict] us=");
    Serial.println((unsigned long)(average_sum/10));
    
    delay(500); 
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions