Skip to content

Commit

Permalink
feat(samples): Add a sample of the STeaMi's main features
Browse files Browse the repository at this point in the history
  • Loading branch information
jnthbdn authored and nedseb committed Oct 8, 2024
1 parent 406aa2d commit 169a9d5
Show file tree
Hide file tree
Showing 11 changed files with 958 additions and 0 deletions.
63 changes: 63 additions & 0 deletions samples/OOB/accelero.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#pragma once

#include "ism330dl.h"
#include "utils.h"

extern codal::STM32Pin* btnMenu;
extern codal::ISM330DL* ism;
extern codal::SSD1327_SPI* ssd;

class Ball {
public:
int x;
int y;
float ax;
float ay;
float speed;
int radius;

Ball(int x, int y, int radius) : x{x}, y{y}, ax{0}, ay{0}, speed{3}, radius{radius} {}

float low_limit() { return 18 + radius; }
float high_limit() { return 110 - radius; }

void update(ISM_Data& accel)
{
ax = clampf(ax + (accel.y), -5, 5);
ay = clampf(ay - (accel.x), -5, 5);

x += ax * speed;
y += ay * speed;

if (x < low_limit() || x > high_limit()) {
ax *= -0.8;
x = clampf(x, low_limit(), high_limit());
}

if (y < low_limit() || y > high_limit()) {
ay *= -0.8;
y = clampf(y, low_limit(), high_limit());
}
}

void draw() { ssd->drawCircle(x, y, radius, false, 0xFF); }
};

void accelero_prog()
{
Ball ball(64, 64, 8);
while (true) {
if (click_button(btnMenu)) {
break;
}

auto accel = ism->readAccelerometerData();

ball.update(accel);

ssd->fill(0x00);
ssd->drawRectangle(18, 18, 110, 110, false, 0xFF);
ball.draw();
ssd->show();
}
}
92 changes: 92 additions & 0 deletions samples/OOB/buzzer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#pragma once

#include <cstdint>

#include "utils.h"

extern codal::STM32Pin* btnMenu;
extern codal::STM32Pin* buzzer;
extern codal::SSD1327_SPI* ssd;
extern codal::MCP23009E* mcp;

void buzzer_prog()
{
uint8_t* cursor = new uint8_t(2);
int* freq = new int(440);

mcp->interruptOnFalling(MCP_GP_BOTTOM, [=]() {
int p = pow(10, *cursor);
int digit = int(*freq / p) % 10;
if (digit == 0) {
return;
}

*freq -= p;
buzzer->setAnalogPeriodUs(1000000 / *freq);
buzzer->setAnalogValue(255);
});

mcp->interruptOnFalling(MCP_GP_UP, [=]() {
int p = pow(10, *cursor);
int digit = int(*freq / p) % 10;
if (digit == 9) {
return;
}

*freq += p;
buzzer->setAnalogPeriodUs(1000000 / *freq);
buzzer->setAnalogValue(255);
});

mcp->interruptOnFalling(MCP_GP_LEFT, [=]() {
if (*cursor < 5) {
(*cursor)++;
}
});

mcp->interruptOnFalling(MCP_GP_RIGHT, [=]() {
if (*cursor > 0) {
(*cursor)--;
}
});

buzzer->setAnalogPeriodUs(1000000 / *freq);
buzzer->setAnalogValue(255);

while (1) {
if (click_button(btnMenu)) {
break;
}

auto freqStr = std::to_string(*freq);
string cursorStr = " ";

while (freqStr.size() < 5) {
freqStr = "0" + freqStr;
}

for (int8_t i = 4; i >= 0; --i) {
if (i == *cursor) {
cursorStr.push_back('^');
}
else {
cursorStr.push_back(' ');
}
}

ssd->fill(0x00);
ssd->drawText("Freq: " + freqStr + "Hz", 15, 40, 0xFF);
ssd->drawText(cursorStr, 15, 48, 0xFF);
ssd->drawText("< / >: change digit", 5, 66, 0xFF);
ssd->drawText("^ / v: de/increase", 5, 75, 0xFF);
ssd->show();

fiber_sleep(100);
}

buzzer->setAnalogValue(0);
mcp->disableInterrupt(MCP_GP_BOTTOM);
mcp->disableInterrupt(MCP_GP_UP);
delete cursor;
delete freq;
}
111 changes: 111 additions & 0 deletions samples/OOB/compass.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
#pragma once

#include <algorithm>
#include <cmath>
#include <limits>

#include "lis2mdl.h"
#include "utils.h"

extern codal::STM32Pin* btnMenu;
extern codal::STM32Pin* btnA;
extern codal::LIS2MDL* lis;
extern codal::SSD1327_SPI* ssd;

constexpr float TRI_ANGLE = 155.0 * 0.017452792;

float mapf(float x, float in_min, float in_max, float out_min, float out_max)
{
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}

Lis2Data average_measure(unsigned nb_measure)
{
Lis2Data data{.x = 0, .y = 0, .z = 0};

for (unsigned i = 0; i < nb_measure; ++i) {
Lis2Data magn = lis->readData();

data.x += magn.x;
data.y += magn.y;
}

data.x /= float(nb_measure);
data.y /= float(nb_measure);

return data;
}

void compass_prog()
{
float size = 32;
float cx = 64;
float cy = 64;
float calib_min_x = std::numeric_limits<float>::max();
float calib_max_x = std::numeric_limits<float>::min();
float calib_min_y = std::numeric_limits<float>::max();
float calib_max_y = std::numeric_limits<float>::min();

// calibration
ssd->fill(0x00);
ssd->drawRectangle(25, 14, 102, 40, true, 0xFF);
ssd->drawText("CALIBRATION", 31, 18, 0x00);
ssd->drawText("MAGNETOMETER", 28, 29, 0x00);
ssd->drawText("Move the STeaMi", 21, 49, 0xFF);
ssd->drawText("by making \"8\" shapes", 5, 60, 0xFF);
ssd->drawText("Push \"A\" to finish", 12, 86, 0xFF);
ssd->show();

while (true) {
if (click_button(btnA)) {
break;
}

Lis2Data magn = average_measure(20);

calib_max_x = std::max(calib_max_x, magn.x);
calib_min_x = std::min(calib_min_x, magn.x);

calib_max_y = std::max(calib_max_y, magn.y);
calib_min_y = std::min(calib_min_y, magn.y);
}

printf("Calibration:\n\tx: [%.2f, %.2f]\n\ty: [%.2f, %.2f]\n", calib_min_x, calib_max_x, calib_min_y, calib_max_y);

while (true) {
if (click_button(btnMenu)) {
break;
}

Lis2Data magn = average_measure(10);

float x = mapf(magn.x, calib_min_x, calib_max_x, -1, 1);
float y = -mapf(magn.y, calib_min_y, calib_max_y, -1, 1);
float angle = std::atan2(y, x);


double Acos = cos(angle);
double Asin = sin(angle);
double Bcos = cos(angle - TRI_ANGLE);
double Bsin = sin(angle - TRI_ANGLE);
double Ccos = cos(angle + TRI_ANGLE);
double Csin = sin(angle + TRI_ANGLE);


ssd->fill(0x00);
ssd->drawCircle(64, 64, 50, false, 0xFF);
ssd->drawCircle(64, 64, 2, true, 0xFF);

ssd->drawSegment(cx + (size * Bcos), cy + (size * Bsin), cx + (size * Acos), cy + (size * Asin), 1, 0xFF);
ssd->drawSegment(cx + (size * Ccos), cy + (size * Csin), cx + (size * Acos), cy + (size * Asin), 1, 0xFF);
ssd->drawSegment(cx + (size * Ccos), cy + (size * Csin), cx + (size * Bcos), cy + (size * Bsin), 1, 0xFF);

ssd->drawText("N", 61, 2, 0xFF);
ssd->drawText("S", 61, 119, 0xFF);
ssd->drawText("O", 2, 61, 0xFF);
ssd->drawText("E", 121, 61, 0xFF);
ssd->show();

target_wait(100);
}
}
Loading

0 comments on commit 169a9d5

Please sign in to comment.