RobTillaart / HT16K33

Arduino Library for HT16K33 4x7segment display
MIT License
24 stars 6 forks source link

Add raw 7-seg segment codes for `seg.displayRaw` #28

Closed DeflateAwning closed 10 months ago

DeflateAwning commented 10 months ago

The codes, copied from https://github.com/avishorp/TM1637/blob/master/TM1637Display.h


#define SEG_A   0b00000001
#define SEG_B   0b00000010
#define SEG_C   0b00000100
#define SEG_D   0b00001000
#define SEG_E   0b00010000
#define SEG_F   0b00100000
#define SEG_G   0b01000000
#define SEG_DP  0b10000000

These are standard codes, and can be combined to produce messages.

A complex example of this, which could be cool to include in an example, is a scrolling "Press Start" message:


void display_scrolling_press_start(uint32_t idle_start_time_ms) {
    const int msg_len = 14;
    int offset = ((millis() - idle_start_time_ms) / 500) % (msg_len);

    const uint8_t SEG_PRESS_START[] = {
        SEG_A | SEG_B | SEG_F | SEG_E | SEG_G,           // P
        SEG_E | SEG_G,                                   // r
        SEG_A | SEG_D | SEG_E | SEG_F | SEG_G,           // E
        SEG_A | SEG_F | SEG_G | SEG_C | SEG_D,           // S
        SEG_A | SEG_F | SEG_G | SEG_C | SEG_D,           // S
        0,
        SEG_A | SEG_F | SEG_G | SEG_C | SEG_D,           // S
        SEG_F | SEG_E | SEG_G | SEG_D,                   // t
        SEG_A | SEG_B | SEG_C | SEG_E | SEG_F | SEG_G,   // A
        SEG_E | SEG_G,                                   // r
        SEG_F | SEG_E | SEG_G | SEG_D,                   // t
        0,0,0
    };
    uint8_t seg1_data[] = {SEG_D, SEG_D, SEG_D, SEG_D};
    // uint8_t seg2_data[] = {SEG_D, SEG_D, SEG_D, SEG_D};

    for (int i = 0; i < 4; i++) {
        seg1_data[i] = SEG_PRESS_START[(i+offset)%msg_len];
        // seg2_data[i] = SEG_PRESS_START[(i+offset+5)%msg_len];
    }

    display.displayRaw(seg1_data);
}

The magic numbers in the raw "elsa" example could/should be constructed with this method as well.

RobTillaart commented 10 months ago

Nice to add, especially the example. Thanks

DeflateAwning commented 10 months ago

I'll add that it would be helpful to include a link to this graphic alongside the codes/examples, and maybe even include the graphic in the README, as it shows which segments are which: https://en.wikipedia.org/wiki/File:7_segment_display_labeled.svg

RobTillaart commented 10 months ago

Agree, I would use const uint8_t SEG_A = 0b00000001; to prevent #define conflicts. Furthermore I would add const uint8_t SEG_NONE = 0b00000000; (SEG_EMPTY? or ???) to handle the "magix zero"

DeflateAwning commented 10 months ago

To be specific, I think those constants should be added to the library (as macros or other), as they are included in many other similar 7-seg libraries.

RobTillaart commented 10 months ago

To be specific, I think those constants should be added to the library (as macros or other), as they are included in many other similar 7-seg libraries.

That is exactly the reason why not to use #define as other libs might have defines them already.

They will come in the .h file

RobTillaart commented 10 months ago

wrapped the function in a sketch

//
//    FILE: demo_displayRaw.ino
//  AUTHOR: Rob Tillaart, DeflateAwning
// PURPOSE: demo
//     URL: http://www.adafruit.com/products/1002
//     URL: https://github.com/RobTillaart/HT16K33

#include "HT16K33.h"

HT16K33 display(0x70);

uint32_t start = 0;

void display_scrolling_press_start(uint32_t idle_start_time_ms) 
{
  const int msg_len = 14;
  int offset = ((millis() - idle_start_time_ms) / 500) % (msg_len);

  const uint8_t SEG_PRESS_START[] = {
    SEG_A | SEG_B | SEG_F | SEG_E | SEG_G,           // P
    SEG_E | SEG_G,                                   // r
    SEG_A | SEG_D | SEG_E | SEG_F | SEG_G,           // E
    SEG_A | SEG_F | SEG_G | SEG_C | SEG_D,           // S
    SEG_A | SEG_F | SEG_G | SEG_C | SEG_D,           // S
    SEG_NONE,                                        // [space]
    SEG_A | SEG_F | SEG_G | SEG_C | SEG_D,           // S
    SEG_F | SEG_E | SEG_G | SEG_D,                   // t
    SEG_A | SEG_B | SEG_C | SEG_E | SEG_F | SEG_G,   // A
    SEG_E | SEG_G,                                   // r
    SEG_F | SEG_E | SEG_G | SEG_D,                   // t
    SEG_NONE,                                        // [space]
    SEG_NONE,                                        // [space]
    SEG_NONE                                         // [space]
  };
  uint8_t seg1_data[] = {SEG_D, SEG_D, SEG_D, SEG_D};
  // uint8_t seg2_data[] = {SEG_D, SEG_D, SEG_D, SEG_D};

  for (int i = 0; i < 4; i++) {
    seg1_data[i] = SEG_PRESS_START[(i + offset) % msg_len];
    // seg2_data[i] = SEG_PRESS_START[(i+offset+5)%msg_len];
  }

  display.displayRaw(seg1_data);
}

void setup()
{
  Serial.begin(115200);
  Serial.println(__FILE__);
  Serial.print("HT16K33_LIB_VERSION: ");
  Serial.println(HT16K33_LIB_VERSION);

  Wire.begin();
  Wire.setClock(100000);
  display.begin();

  display.displayOn();
  display.setBrightness(2);
  display.displayClear();

  start = millis();
}

void loop()
{
  display_scrolling_press_start(start);
  delay(100);
}

//  -- END OF FILE --

Can you verify I used it correctly?

RobTillaart commented 10 months ago

@DeflateAwning Develop branch and Pull Request created (includes above sketch as example.

DeflateAwning commented 10 months ago

Looks good to me! Nice work!

I left a couple quick comments on the linked commit

RobTillaart commented 10 months ago

thanks, will look into it tomorrow (fresh eyes)