2026年2月13日 星期五

[OTTO GO] WiFi以及整合測試

 



Arduino:
 


  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
#include <Arduino.h>
#include <WiFi.h>
#include <WebServer.h>
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ST7789.h>
#include <ESP32Servo.h>
#include "driver/i2s.h"
#include "freertos/semphr.h"

// ===================== Pin map (OTTO GO) =====================
// TFT (SPI)
#define TFT_MOSI 19
#define TFT_SCLK 18
#define TFT_CS    5
#define TFT_DC   16
#define TFT_RST  23

// Ultrasonic
#define US_TRIG 27
#define US_ECHO 39

// Servos
#define SERVO1 21
#define SERVO2 22
#define SERVO3 4
#define SERVO4 14

// Speaker (I2S -> AMP -> SPK white socket)
#define SPK_I2S_LRCK 17
#define SPK_I2S_BCLK 2
#define SPK_I2S_DOUT 32

// Microphone (I2S MEMS)
#define MIC_I2S_SCK 13
#define MIC_I2S_SD  12
#define MIC_I2S_WS  15

// ===================== WiFi AP =====================
const char* AP_SSID = "OTTO_GO_TEST";
const char* AP_PASS = "12345678";

// ===================== Objects =====================
WebServer server(80);
Adafruit_ST7789 tft = Adafruit_ST7789(TFT_CS, TFT_DC, TFT_RST);
Servo s1, s2, s3, s4;

// ===================== Helpers =====================
static inline uint16_t RGB565(uint8_t r, uint8_t g, uint8_t b) {
  return ((r & 0xF8) << 8) | ((g & 0xFC) << 3) | (b >> 3);
}

// ===================== Ultrasonic =====================
long readDistanceCM() {
  digitalWrite(US_TRIG, LOW);
  delayMicroseconds(2);
  digitalWrite(US_TRIG, HIGH);
  delayMicroseconds(10);
  digitalWrite(US_TRIG, LOW);

  unsigned long duration = pulseIn(US_ECHO, HIGH, 30000UL);
  if (duration == 0) return -1;
  return (long)(duration / 58UL);
}
long median3(long a, long b, long c) {
  if (a > b) { long t=a; a=b; b=t; }
  if (b > c) { long t=b; b=c; c=t; }
  if (a > b) { long t=a; a=b; b=t; }
  return b;
}

// ===================== TFT =====================
void tftHello() {
  tft.fillScreen(ST77XX_BLACK);
  tft.setTextColor(ST77XX_WHITE);
  tft.setTextSize(3);
  tft.setCursor(20, 40);
  tft.print("Hello");
}

void drawTigerMouth(bool open) {
  int cx = tft.width()/2;
  int cy = tft.height()/2;

  uint16_t furLight = RGB565(235, 228, 210);
  uint16_t shadow   = RGB565(160, 150, 135);
  uint16_t lineDark = RGB565(40, 40, 40);
  uint16_t dotDark  = RGB565(70, 70, 70);

  tft.fillScreen(ST77XX_BLACK);

  tft.fillCircle(cx - 55, cy + 10, 55, furLight);
  tft.fillCircle(cx + 55, cy + 10, 55, furLight);
  tft.fillRoundRect(cx - 90, cy - 25, 180, 110, 28, furLight);

  tft.fillRoundRect(cx - 85, cy + 60, 170, 22, 12, shadow);
  tft.fillRoundRect(cx - 40, cy + 55, 80, 10, 5, RGB565(120,110,100));

  int dotY1 = cy + 6, dotY2 = cy + 26;
  for (int k=0;k<3;k++){
    tft.fillCircle(cx - 55 - k*12, dotY1 + k*2, 2, dotDark);
    tft.fillCircle(cx - 55 - k*12, dotY2 + k*2, 2, dotDark);
    tft.fillCircle(cx + 55 + k*12, dotY1 + k*2, 2, dotDark);
    tft.fillCircle(cx + 55 + k*12, dotY2 + k*2, 2, dotDark);
  }

  tft.drawLine(cx, cy - 5, cx, open ? (cy + 10) : (cy + 20), lineDark);
  tft.drawLine(cx-1, cy - 5, cx-1, open ? (cy + 10) : (cy + 20), RGB565(80,80,80));

  int mouthY = open ? (cy + 16) : (cy + 18);
  int N = open ? 36 : 34;
  int div = open ? 85 : 95;
  for (int i=0;i<N;i++){
    int y = mouthY + (i*i)/div;
    tft.drawPixel(cx-i, y, lineDark);
    tft.drawPixel(cx-i, y+1, lineDark);
    tft.drawPixel(cx+i, y, lineDark);
    tft.drawPixel(cx+i, y+1, lineDark);
  }

  tft.fillCircle(cx - (open?38:36), mouthY + (open?14:12), 3, lineDark);
  tft.fillCircle(cx + (open?38:36), mouthY + (open?14:12), 3, lineDark);

  if (!open) {
    tft.fillRoundRect(cx - 10, mouthY + 20, 20, 4, 2, RGB565(90,80,70));
  } else {
    uint16_t mouthIn = RGB565(15,15,15);
    uint16_t tongue  = RGB565(210,120,120);
    int w=90, h=45;
    int x=cx-w/2, y0=cy+28;
    tft.fillRoundRect(x, y0, w, h, 18, mouthIn);
    tft.fillCircle(cx, y0+h, 22, mouthIn);
    tft.fillRoundRect(cx-22, y0+18, 44, 20, 10, tongue);
    tft.fillCircle(cx, y0+36, 14, tongue);
  }

  tft.setTextSize(2);
  tft.setTextColor(ST77XX_YELLOW, ST77XX_BLACK);
  tft.setCursor(10, 10);
  tft.print(open ? "MOUTH: OPEN" : "MOUTH: CLOSE");
}

// ===================== I2S0 mode switching (穩定版) =====================
const i2s_port_t I2S_PORT = I2S_NUM_0;

enum I2SMode { MODE_NONE=0, MODE_SPK=1, MODE_MIC=2 };
static volatile I2SMode curMode = MODE_NONE;

static SemaphoreHandle_t i2sMutex = nullptr;

// 狀態顯示
bool spk_ok = false;
bool mic_ok = false;

static void lockI2S()   { if (i2sMutex) xSemaphoreTake(i2sMutex, portMAX_DELAY); }
static void unlockI2S() { if (i2sMutex) xSemaphoreGive(i2sMutex); }

static void i2sStopAndUninstallSafe() {
  if (curMode == MODE_NONE) return;
  // 這兩個呼叫在某些狀態下回傳錯誤是正常的,但不應該崩潰
  i2s_stop(I2S_PORT);
  i2s_driver_uninstall(I2S_PORT);
  curMode = MODE_NONE;
}

bool i2sInitSpeaker() {
  i2sStopAndUninstallSafe();

  i2s_config_t cfg = {
    .mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_TX),
    .sample_rate = 22050,
    .bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT,
    .channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT,
    .communication_format = I2S_COMM_FORMAT_I2S_MSB,
    .intr_alloc_flags = ESP_INTR_FLAG_LEVEL1,
    .dma_buf_count = 4,
    .dma_buf_len   = 128,
    .use_apll = false,
    .tx_desc_auto_clear = true,
    .fixed_mclk = 0
  };

  i2s_pin_config_t pin_cfg = {
    .bck_io_num = SPK_I2S_BCLK,
    .ws_io_num  = SPK_I2S_LRCK,
    .data_out_num = SPK_I2S_DOUT,
    .data_in_num  = I2S_PIN_NO_CHANGE
  };

  esp_err_t e1 = i2s_driver_install(I2S_PORT, &cfg, 0, NULL);
  if (e1 != ESP_OK) return false;

  esp_err_t e2 = i2s_set_pin(I2S_PORT, &pin_cfg);
  if (e2 != ESP_OK) return false;

  i2s_zero_dma_buffer(I2S_PORT);
  i2s_start(I2S_PORT);
  curMode = MODE_SPK;
  return true;
}

bool i2sInitMic() {
  i2sStopAndUninstallSafe();

  i2s_config_t cfg = {
    .mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_RX),
    .sample_rate = 16000,
    .bits_per_sample = I2S_BITS_PER_SAMPLE_32BIT,
    .channel_format = I2S_CHANNEL_FMT_ONLY_LEFT,
    .communication_format = I2S_COMM_FORMAT_I2S_MSB,
    .intr_alloc_flags = ESP_INTR_FLAG_LEVEL1,
    .dma_buf_count = 4,
    .dma_buf_len   = 128,
    .use_apll = false,
    .tx_desc_auto_clear = false,
    .fixed_mclk = 0
  };

  i2s_pin_config_t pin_cfg = {
    .bck_io_num = MIC_I2S_SCK,
    .ws_io_num  = MIC_I2S_WS,
    .data_out_num = I2S_PIN_NO_CHANGE,
    .data_in_num  = MIC_I2S_SD
  };

  esp_err_t e1 = i2s_driver_install(I2S_PORT, &cfg, 0, NULL);
  if (e1 != ESP_OK) return false;

  esp_err_t e2 = i2s_set_pin(I2S_PORT, &pin_cfg);
  if (e2 != ESP_OK) return false;

  i2s_zero_dma_buffer(I2S_PORT);
  i2s_start(I2S_PORT);
  curMode = MODE_MIC;
  return true;
}

bool ensureSpeakerModeLocked() {
  if (curMode == MODE_SPK) return true;
  spk_ok = i2sInitSpeaker();
  return spk_ok;
}

bool ensureMicModeLocked() {
  if (curMode == MODE_MIC) return true;
  mic_ok = i2sInitMic();
  return mic_ok;
}

// ===================== Speaker sound =====================
static uint32_t rng = 1;
static inline int16_t noise16() {
  rng = rng * 1664525UL + 1013904223UL;
  return (int16_t)(rng >> 16);
}

void playBeep1kHz_200ms_locked() {
  if (!ensureSpeakerModeLocked()) return;

  const int sampleRate = 22050;
  const float freq = 1000.0f;
  const float durSec = 0.2f;
  const int total = (int)(sampleRate * durSec);

  const int N = 256;
  int16_t stereo[N * 2];
  float phase = 0.0f;

  for (int i=0;i<total;){
    int n = (total-i > N) ? N : (total-i);
    for (int k=0;k<n;k++){
      phase += 2.0f * 3.1415926f * freq / (float)sampleRate;
      if (phase > 2.0f*3.1415926f) phase -= 2.0f*3.1415926f;

      int16_t v = (int16_t)(sinf(phase) * 12000);
      stereo[2*k+0] = v;
      stereo[2*k+1] = v;
    }
    size_t written=0;
    i2s_write(I2S_PORT, (const char*)stereo, n*2*sizeof(int16_t), &written, portMAX_DELAY);
    i += n;
  }
}

void playRoarOnce_locked() {
  if (!ensureSpeakerModeLocked()) return;

  const int sampleRate = 22050;
  const float durSec = 0.6f;
  const int total = (int)(sampleRate * durSec);

  const int N = 256;
  int16_t stereo[N * 2];
  float phase = 0.0f;

  for (int i=0;i<total;){
    int n = (total-i > N) ? N : (total-i);

    for (int k=0;k<n;k++){
      float t = (float)(i+k)/(float)total;
      float f = 120.0f - 50.0f*t;
      phase += 2.0f * 3.1415926f * f / (float)sampleRate;
      if (phase > 2.0f*3.1415926f) phase -= 2.0f*3.1415926f;

      float env = (t < 0.08f) ? (t/0.08f) : (1.0f - (t-0.08f)/0.92f);
      if (env < 0) env = 0;

      float s  = sinf(phase)*0.65f + sinf(phase*0.5f)*0.35f;
      float nz = (noise16()/32768.0f)*0.35f;
      float out = (s + nz) * env;

      float gain = 0.85f;
      int32_t v = (int32_t)(out * gain * 30000.0f);
      if (v > 32767) v = 32767;
      if (v < -32768) v = -32768;

      int16_t vv = (int16_t)v;
      stereo[2*k+0] = vv;
      stereo[2*k+1] = vv;
    }

    size_t written=0;
    i2s_write(I2S_PORT, (const char*)stereo, n*2*sizeof(int16_t), &written, portMAX_DELAY);
    i += n;
  }
}

// ===================== Microphone energy =====================
static const int MIC_FRAME = 128;
static int32_t micBuf[MIC_FRAME];

float readMicEnergyOnce_locked() {
  if (!ensureMicModeLocked()) return 0;

  size_t bytesRead = 0;
  i2s_read(I2S_PORT, (void*)micBuf, sizeof(micBuf), &bytesRead, 20 / portTICK_PERIOD_MS);
  int n = bytesRead / 4;
  if (n <= 0) return 0;

  uint64_t sumAbs = 0;
  for (int i=0;i<n;i++){
    int16_t s16 = (int16_t)(micBuf[i] >> 16);
    sumAbs += (uint16_t)abs(s16);
  }
  return (float)sumAbs / (float)n;
}

// ===================== Web UI =====================
String htmlPage() {
  String h;
  h += "<!doctype html><html><head><meta charset='utf-8'/>";
  h += "<meta name='viewport' content='width=device-width,initial-scale=1'/>";
  h += "<title>OTTO GO Test</title>";
  h += "<style>body{font-family:Arial;margin:16px}button{padding:10px 14px;margin:6px;border-radius:10px;border:0;background:#2d7;color:#fff;font-size:16px}";
  h += ".row{margin:10px 0}.card{padding:12px;border:1px solid #ddd;border-radius:12px;margin:10px 0}";
  h += "input[type=range]{width:260px}</style></head><body>";
  h += "<h2>OTTO GO 功能測試(192.168.4.1)</h2>";

  h += "<div class='card'><h3>Speaker(SPK 白色座)</h3>";
  h += "<button onclick=\"fetch('/spk?m=beep')\">Beep</button>";
  h += "<button onclick=\"fetch('/spk?m=roar')\">老虎大叫</button>";
  h += "<div>spk_ok:<span id='spkok'>--</span></div>";
  h += "</div>";

  h += "<div class='card'><h3>Microphone(I2S MEMS)</h3>";
  h += "<div>音量:<span id='mic'>--</span> mic_ok:<span id='micok'>--</span></div>";
  h += "</div>";

  h += "<div class='card'><h3>TFT</h3>";
  h += "<button onclick=\"fetch('/tft?m=hello')\">Hello</button>";
  h += "<button onclick=\"fetch('/tft?m=mouth_open')\">虎嘴張開</button>";
  h += "<button onclick=\"fetch('/tft?m=mouth_close')\">虎嘴閉合</button>";
  h += "<button onclick=\"fetch('/tft?m=clear')\">清屏</button>";
  h += "</div>";

  h += "<div class='card'><h3>超音波測距</h3>";
  h += "<div>距離:<span id='dist'>--</span> cm</div>";
  h += "</div>";

  h += "<div class='card'><h3>Servo</h3>";
  h += "<button onclick=\"fetch('/servo?all=90')\">四顆 90 度校正</button><br/>";
  for (int i=1;i<=4;i++){
    h += "<div class='row'>S"; h += i; h += " 角度:";
    h += "<input type='range' min='0' max='180' value='90' id='s"; h += i; h += "' ";
    h += "oninput=\"setServo("; h += i; h += ",this.value)\"/>";
    h += " <span id='sv"; h += i; h += "'>90</span></div>";
  }
  h += "</div>";

  h += "<script>";
  h += "function setServo(ch,v){document.getElementById('sv'+ch).innerText=v;fetch('/servo?ch='+ch+'&ang='+v);}";

  // ★重要:降低輪詢頻率(避免 I2S 反覆卸載/安裝造成崩潰)
  h += "async function tick(){";
  h += "try{let r=await fetch('/mic'); let j=await r.json();";
  h += "document.getElementById('mic').innerText=(+j.energy).toFixed(0);";
  h += "document.getElementById('micok').innerText=j.mic_ok;";
  h += "document.getElementById('spkok').innerText=j.spk_ok;";
  h += "}catch(e){}";
  h += "try{let r2=await fetch('/ultra'); let j2=await r2.json(); document.getElementById('dist').innerText=j2.cm;}catch(e){}";
  h += "setTimeout(tick,1500);"; // ← 300ms 改成 1500ms
  h += "}";
  h += "tick();</script>";

  h += "</body></html>";
  return h;
}

// ===================== Web handlers =====================
void handleRoot() {
  server.send(200, "text/html; charset=utf-8", htmlPage());
}

void handleTFT() {
  String m = server.arg("m");
  if (m == "hello") tftHello();
  else if (m == "mouth_open") drawTigerMouth(true);
  else if (m == "mouth_close") drawTigerMouth(false);
  else if (m == "clear") tft.fillScreen(ST77XX_BLACK);
  server.send(200, "text/plain", "OK");
}

void handleSPK() {
  String m = server.arg("m");
  lockI2S();
  if (m == "beep") playBeep1kHz_200ms_locked();
  else if (m == "roar") playRoarOnce_locked();
  unlockI2S();
  server.send(200, "text/plain", "OK");
}

void handleMic() {
  float e = 0;
  lockI2S();
  e = readMicEnergyOnce_locked();
  // 讀完麥克風後,立刻切回 Speaker 模式(確保隨時可播放)
  spk_ok = ensureSpeakerModeLocked();
  unlockI2S();

  String json = "{\"energy\":" + String(e, 1) +
                ",\"mic_ok\":" + String(mic_ok ? 1 : 0) +
                ",\"spk_ok\":" + String(spk_ok ? 1 : 0) + "}";
  server.send(200, "application/json", json);
}

void handleUltra() {
  long d1 = readDistanceCM();
  long d2 = readDistanceCM();
  long d3 = readDistanceCM();
  long d  = median3(d1,d2,d3);
  if (d < 0) d = -1;
  String json = "{\"cm\":" + String(d) + "}";
  server.send(200, "application/json", json);
}

void handleServo() {
  if (server.hasArg("all")) {
    int a = constrain(server.arg("all").toInt(), 0, 180);
    s1.write(a); s2.write(a); s3.write(a); s4.write(a);
    server.send(200, "text/plain", "OK");
    return;
  }
  int ch = server.arg("ch").toInt();
  int ang = constrain(server.arg("ang").toInt(), 0, 180);
  switch(ch){
    case 1: s1.write(ang); break;
    case 2: s2.write(ang); break;
    case 3: s3.write(ang); break;
    case 4: s4.write(ang); break;
    default: break;
  }
  server.send(200, "text/plain", "OK");
}

// ===================== Setup/Loop =====================
void setup() {
  Serial.begin(115200);

  i2sMutex = xSemaphoreCreateMutex();

  pinMode(US_TRIG, OUTPUT);
  pinMode(US_ECHO, INPUT);

  SPI.begin(TFT_SCLK, -1, TFT_MOSI, TFT_CS);
  tft.init(240, 320);
  tft.setRotation(1);
  tftHello();

  s1.attach(SERVO1, 600, 2400);
  s2.attach(SERVO2, 600, 2400);
  s3.attach(SERVO3, 600, 2400);
  s4.attach(SERVO4, 600, 2400);
  s1.write(90); s2.write(90); s3.write(90); s4.write(90);

  // 開機先穩定進入 Speaker 模式並 beep
  lockI2S();
  spk_ok = i2sInitSpeaker();
  if (spk_ok) playBeep1kHz_200ms_locked();
  unlockI2S();

  WiFi.mode(WIFI_AP);
  WiFi.softAP(AP_SSID, AP_PASS);
  Serial.print("AP IP: ");
  Serial.println(WiFi.softAPIP());

  server.on("/", handleRoot);
  server.on("/tft", handleTFT);
  server.on("/spk", handleSPK);
  server.on("/mic", handleMic);
  server.on("/ultra", handleUltra);
  server.on("/servo", handleServo);
  server.begin();

  Serial.println("Web server started.");
}

void loop() {
  server.handleClient();
}


沒有留言:

張貼留言