M5ez / Core2ez

GNU Lesser General Public License v3.0
15 stars 4 forks source link

ezWindow: no default font #9

Open vkichline opened 3 years ago

vkichline commented 3 years ago

Haven't dug deep yet, but it's clear you have to be careful to set certain values before using windows. I have a window w/ spriteBuffer. When I receive a key from a CardKB board attached to PortA, I update the window. If I did not set the window's font in setup, I get a crash when I process a keystroke. Ideally, there should be safe, reasonable defaults for required values (or return an error rather than crashing.)

Probably not necessary, but the full program is below. Note the line containing // Comment out this line, you will crash when a key arrives

/*
    A small editor using the CardKB I2C expansion card w/ Core2
*/
#include <M5Core2.h>
#include <Core2ez.h>

#define CARDKB_ADDR 0x5F

String    input;
ezWindow  display(20, 60, 280, 64);

void update_display() {
  display.fillScreen(BLACK);
  display.drawString(input, 0, 0);
  display.push();
}

void process_key(char c) {
  switch(c) {
    // TBD: Handle CR, BS, etc. as special cases
    default:
      input += c;
      update_display();
  }
}

void setup() {
  ez.begin();
  Wire1.begin(32, 33);
  display.spriteBuffer();
  ez.Screen.add(display);
  ez.Screen.focus();

  ez.Screen.fillScreen(BLACK);
  ez.Screen.setTextColor(YELLOW, BLACK);
  ez.Screen.setTextSize(1);
  ez.Screen.drawCentreString("Input text with CardKB", 160, 4, 4);
  display.setTextDatum(TL_DATUM);
  display.setTextPadding(0);
  display.ezFont(FSSB12);       // Comment out this line, you will crash when a key arrives
  display.setTextColor(YELLOW);
  display.fillScreen(BLACK);
  display.push();
}

void loop() {
  Wire1.requestFrom(CARDKB_ADDR, 1);
  while(Wire1.available()) {
    char c = Wire1.read(); // receive a byte as character
    if (c != 0) process_key(c);
  }
  delay(10);
}