ti-pote/apps/robot-hardware/legacy/robot-hardware.ino
2026-04-08 18:37:08 +02:00

82 lines
2.3 KiB
C++

#include <U8g2lib.h>
#include <SPI.h>
#include <math.h>
#define CS 5
#define DC 16
#define RESET 17
U8G2_SSD1309_128X64_NONAME0_F_4W_HW_SPI u8g2(U8G2_R2, CS, DC, RESET);
// Yeux ronds formés de 10 barres horizontales, la 4e depuis le haut supprimée
const int EYE_R = 29; // rayon du cercle
const int BAR_THICK = 5; // épaisseur
const int BAR_PITCH = 6; // espacement centre-à-centre
const int BAR_ROUND = 2; // rayon coin arrondi des barres
const int N_BARS = 10;
const int SKIP_BAR = 3; // 4e en partant du haut (index 0 = top)
const int OVER_BAR = 2; // 3e en partant du haut, redessinée par-dessus la pupille
const int EYE_L_CX = 32;
const int EYE_R_CX = 96;
const int EYE_CY = 32;
const int PUPIL_R = 11;
const int PUPIL_DY = -3;
void drawEye(int cx, int cy) {
for (int i = 0; i < N_BARS; i++) {
if (i == SKIP_BAR) continue;
// offset vertical centré autour de cy (10 barres symétriques)
float off = (i - (N_BARS - 1) / 2.0f) * BAR_PITCH;
int dy = (int)roundf(off);
int dy2 = dy * dy;
int r2 = EYE_R * EYE_R;
if (dy2 >= r2) continue;
int halfW = (int)sqrtf((float)(r2 - dy2));
int topY = cy + dy - BAR_THICK / 2;
int w = 2 * halfW + 1;
// garde-fou pour drawRBox (w et h doivent être > 2*r)
if (w > 2 * BAR_ROUND && BAR_THICK > 2 * BAR_ROUND) {
u8g2.drawRBox(cx - halfW, topY, w, BAR_THICK, BAR_ROUND);
} else {
u8g2.drawBox(cx - halfW, topY, w, BAR_THICK);
}
}
// pupille : disque sombre creusé
u8g2.setDrawColor(0);
u8g2.drawDisc(cx, cy + PUPIL_DY, PUPIL_R);
u8g2.setDrawColor(1);
// redessine le 3e trait par-dessus la pupille
{
float off = (OVER_BAR - (N_BARS - 1) / 2.0f) * BAR_PITCH;
int dy = (int)roundf(off);
int dy2 = dy * dy;
int r2 = EYE_R * EYE_R;
if (dy2 < r2) {
int halfW = (int)sqrtf((float)(r2 - dy2));
int topY = cy + dy - BAR_THICK / 2;
int w = 2 * halfW + 1;
if (w > 2 * BAR_ROUND && BAR_THICK > 2 * BAR_ROUND) {
u8g2.drawRBox(cx - halfW, topY, w, BAR_THICK, BAR_ROUND);
} else {
u8g2.drawBox(cx - halfW, topY, w, BAR_THICK);
}
}
}
}
void setup() {
u8g2.begin();
u8g2.clearBuffer();
drawEye(EYE_L_CX, EYE_CY);
drawEye(EYE_R_CX, EYE_CY);
u8g2.sendBuffer();
}
void loop() {
// image statique
}