pdf-icon

Arduino Quick Start

2. Devices & Examples

Unit Glass Arduino Tutorial

1.Preparation

2.Example Program

Compilation Requirements
M5Stack Board Package Version >= 3.2.2
M5Unified Library Version >= 0.2.7
M5GFX Library Version >= 0.2.9
cpp
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
#include <Wire.h>
#include <M5UnitGLASS.h>
#include <M5Unified.h>
#include <M5GFX.h>

#define GLASS_BUZZ_PARAM_REG 0xC0
#define GLASS_BUZZ_ONOFF_REG 0xC3
#define GLASS_KEY_A_REG 0xD0
#define GLASS_KEY_B_REG 0xD1

int index_unit_glass;

void setup() {
  Wire.setClock(400000);

  auto cfg = M5.config();
  cfg.external_display.unit_glass = true;
  M5.begin(cfg);

  index_unit_glass = M5.getDisplayIndex(m5::board_t::board_M5UnitGLASS);
  M5.Displays(index_unit_glass).setTextSize(2);
  M5.Displays(index_unit_glass).clear();

  M5.Displays(index_unit_glass).setCursor(0, 0);
  M5.Displays(index_unit_glass).print("This is \nUnit Glass");
  delay(1000);
  M5.Displays(index_unit_glass).clear();

  M5.Displays(index_unit_glass).drawRect(0, 0, 128, 64, 0xFFFF);  // x, y, w, h, color
  delay(500);
  M5.Displays(index_unit_glass).drawCircle(20, 20, 15, 0xFFFF);  // x, y, r, color
  delay(500);
  M5.Displays(index_unit_glass).fillArc(50, 45, 17, 20, 100, 330, 0xFFFF);  // x, y, r0, r1, angle0, angle1, color
  delay(500);
  M5.Displays(index_unit_glass).fillRect(45, 10, 25, 10, 0xFFFF);  // x, y, w, h, color
  delay(500);
  M5.Displays(index_unit_glass).fillTriangle(90, 10, 80, 40, 110, 55, 0xFFFF);  // x0, y0, x1, y1, x2, y2, color
  delay(500);
  M5.Displays(index_unit_glass).drawLine(110, 0, 128, 64, 0xFFFF);  // x0, y0, x1, y1, color
  delay(1000);

  M5.Displays(index_unit_glass).clear();
}

void loop() {
  bool keyA = get_keyA_state();
  bool keyB = get_keyB_state();

  if (keyA || keyB) {
    M5.Displays(index_unit_glass).clear();

    if (keyA) {
      M5.Displays(index_unit_glass).setCursor(0, 0);
      M5.Displays(index_unit_glass).print("Key A \npressed");
      set_buzz_param(2000, 128);  // freq, duty
    }
    if (keyB) {
      M5.Displays(index_unit_glass).setCursor(0, 0);
      M5.Displays(index_unit_glass).print("Key B \npressed");
      set_buzz_param(5000, 128);  // freq, duty
    }

    set_buzz_onoff(true);
  } else {
    M5.Displays(index_unit_glass).setCursor(0, 0);
    M5.Displays(index_unit_glass).print("No key \npressed");
    set_buzz_onoff(false);
  }

  delay(10);
}

// Set buzzer frequency (Hz) and duty (0-255)
void set_buzz_param(int freq, int duty) {
  Wire.beginTransmission(M5UNITGLASS_ADDR);
  Wire.write(GLASS_BUZZ_PARAM_REG);
  Wire.write(freq & 0xFF);         // frequency low bit
  Wire.write((freq >> 8) & 0xFF);  // frequency high bit
  Wire.write(duty);
  Wire.endTransmission();
}

// Toggle buzzer on / off
void set_buzz_onoff(bool on) {
  Wire.beginTransmission(M5UNITGLASS_ADDR);
  Wire.write(GLASS_BUZZ_ONOFF_REG);
  Wire.write(on ? 0x01 : 0x00);
  Wire.endTransmission();
}

// Read key A state
bool get_keyA_state() {
  Wire.beginTransmission(M5UNITGLASS_ADDR);
  Wire.write(GLASS_KEY_A_REG);
  Wire.endTransmission(false);
  Wire.requestFrom(M5UNITGLASS_ADDR, 1);
  bool keyAState = !Wire.read();
  return keyAState;
}

// Read key B state
bool get_keyB_state() {
  Wire.beginTransmission(M5UNITGLASS_ADDR);
  Wire.write(GLASS_KEY_B_REG);
  Wire.endTransmission(false);
  Wire.requestFrom(M5UNITGLASS_ADDR, 1);
  bool keyBState = !Wire.read();
  return keyBState;
}

Runtime Effect:

3.API

On This Page