World Conquest Chronicles

World Conquest Chronicles

Robot 7-segment Display, v1.2.0

The library for controlling a 7-segment display.

Add the DisplayGroup structure that unions the Display and DisplaySelector structures and displays the passed characters; support transformation of the Display and DisplaySelector structures to the instance that will set an inverted voltage level.

Change Log

  • structures:
    • the Display structure:
      • supports transformation to the instance that will set an inverted voltage level;
    • the DisplaySelector structure:
      • supports transformation to the instance that will set an inverted voltage level;
    • the DisplayGroup structure unions the Display and DisplaySelector structures and displays the passed characters:
      • uses a built-in non-blocking timer to switch sub-displays of a multiplexed display;
      • erases the previous displayed character to avoid affecting the next display.

Features

  • possibilities:
    • controlling a single display;
    • controlling a multiplexed display;
  • structures:
    • the Character structure represents a character and keeps a set of specific segments that should be active for displaying this character:
      • supports transformation to the same character with a decimal point;
      • supports transformation to the same character without a decimal point;
    • the Display structure represents a single display and keeps mapping between segments and pins to which they are connected:
      • supports transformation to the instance that will set an inverted voltage level;
    • the DisplaySelector structure controls a multiplexed display by switching its sub-displays, and keeps a set of pins using for this:
      • supports transformation to the instance that will set an inverted voltage level;
    • the DisplayGroup structure unions the Display and DisplaySelector structures and displays the passed characters:
      • uses a built-in non-blocking timer to switch sub-displays of a multiplexed display;
      • erases the previous displayed character to avoid affecting the next display.

Examples

A multiplexed display stopwatch:

#include <Character.h>
#include <Display.h>
#include <PinForSegment.h>
#include <DisplaySelector.h>
#include <DisplayGroup.h>
#include <InfiniteTimer.h>
#include <ClockFunc.h>
#include <stdint.h>

using namespace irenica_ideas::robot_7_segment_display;
using namespace irenica_ideas::robot_timer;

constexpr auto DISPLAY_QUANTITY = 4;
constexpr auto DECIMAL_POINT_INDEX = 1;
constexpr auto COUNTER_UPDATE_DELAY_IN_S = 0.1;
constexpr auto DISPLAY_GROUP_UPDATE_DELAY_IN_S = 0.002;

const Character digits[] = {
  Character(0x7e), // digit 0
  Character(0x30), // digit 1
  Character(0x6d), // digit 2
  Character(0x79), // digit 3
  Character(0x33), // digit 4
  Character(0x5b), // digit 5
  Character(0x5f), // digit 6
  Character(0x70), // digit 7
  Character(0x7f), // digit 8
  Character(0x7b)  // digit 9
};

// TODO: specify your own pin for every segment.
const auto display = Display(
  PinForSegment<'a'>{2},
  PinForSegment<'b'>{3},
  PinForSegment<'c'>{5},
  PinForSegment<'d'>{7},
  PinForSegment<'e'>{11},
  PinForSegment<'f'>{13},
  PinForSegment<'g'>{17},
  PinForSegment<'p'>{19}
);
// TODO: specify your own pin for every sub-display.
const auto displaySelector = makeDisplaySelector(23, 29, 31, 37);

auto counterUpdateTimer =
  InfiniteTimer(COUNTER_UPDATE_DELAY_IN_S, clockForArduino);
uint8_t counterDigits[DISPLAY_QUANTITY] = {};
Character characters[DISPLAY_QUANTITY] = {};
auto displayGroup = makeDisplayGroup(
  characters,
  &display,
  &displaySelector,
  DISPLAY_GROUP_UPDATE_DELAY_IN_S,
  clockForArduino
);

void setup() {
  display.initialize();
  displaySelector.initialize();
}

void loop() {
  counterUpdateTimer.update();
  displayGroup.update();

  // update the counter
  if (counterUpdateTimer.didItTick()) {
    auto willBeOverflowed = true;
    for (
      auto index = 0;
      index < DISPLAY_QUANTITY && willBeOverflowed;
      ++index
    ) {
      willBeOverflowed = counterDigits[index] == 9;
      counterDigits[index] = willBeOverflowed ? 0 : counterDigits[index] + 1;

      // show the first character on the first sub-display, the second character
      // on the second sub-display, etc.
      characters[index] = digits[counterDigits[index]];
      if (index == DECIMAL_POINT_INDEX) {
        characters[index] = characters[index].withDecimalPoint();
      }
    }
  }
}

Photos

Without erasure of the previous character

With erasure of the previous character