Class SevenSegmentComponent


public class SevenSegmentComponent extends HT16K33
Implementation of the CrowPi seven-segment display using I2C with Pi4J
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static enum 
    Mapping of segments to their respective bit according to the HT16K33.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected static final Map<Character,Byte>
    Mapping of characters to their respective byte representation.
    protected static final int
    Default I2C bus address for the seven-segment display on the CrowPi
    protected static final int
    Default I2C device address for the seven-segment display on the CrowPi

    Fields inherited from class com.pi4j.crowpi.components.internal.HT16K33

    buffer, i2c

    Fields inherited from class com.pi4j.crowpi.components.Component

    logger
  • Constructor Summary

    Constructors
    Constructor
    Description
    SevenSegmentComponent(com.pi4j.context.Context pi4j)
    Creates a new seven-segment display component with the default bus and device address.
    SevenSegmentComponent(com.pi4j.context.Context pi4j, int bus, int device)
    Creates a new seven-segment display component with a custom bus and device address.
  • Method Summary

    Modifier and Type
    Method
    Description
    protected static byte
    Helper method for creating a raw digit value (byte) from 0-n segments.
    protected com.pi4j.io.i2c.I2C
    Returns the created I2C instance for the seven-segment display
    protected byte
    getRawDigit(int position)
    Gets the raw digit at the specified position.
    void
    print(double d)
    Prints the given double value to the seven-segment display, automatically setting the decimal point if applicable.
    void
    print(int i)
    Prints the given integer value to the seven-segment display.
    void
    Prints the first four letters (or less if applicable) of the given string to the display.
    void
    Prints the hours and minutes of the given LocalTime instance to the seven-segment display.
    void
    setColon(boolean enabled)
    Enables or disables the colon symbol of the seven-segment display.
    void
    setDecimalPoint(int position, boolean enabled)
    Enables or disables the decimal point at the given digit position.
    void
    setDigit(int position, char c)
    Sets the digit at the specified position to the given character.
    void
    setDigit(int position, int i)
    Sets the digit at the specified position to the given integer.
    void
    setDigit(int position, SevenSegmentComponent.Segment... segments)
    Sets the digit at the specified position to match the given segments.
    protected void
    setRawDigit(int position, byte value)
    Sets the raw digit at the specified position.

    Methods inherited from class com.pi4j.crowpi.components.internal.HT16K33

    clear, refresh, setBlinkRate, setBrightness, setEnabled

    Methods inherited from class com.pi4j.crowpi.components.Component

    sleep, triggerSimpleEvent

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • DEFAULT_BUS

      protected static final int DEFAULT_BUS
      Default I2C bus address for the seven-segment display on the CrowPi
      See Also:
    • DEFAULT_DEVICE

      protected static final int DEFAULT_DEVICE
      Default I2C device address for the seven-segment display on the CrowPi
      See Also:
    • CHAR_BITSETS

      protected static final Map<Character,Byte> CHAR_BITSETS
      Mapping of characters to their respective byte representation. Each byte is a bitset where each bit specifies if a specific segment should be enabled (1) or disabled (0).
  • Constructor Details

    • SevenSegmentComponent

      public SevenSegmentComponent(com.pi4j.context.Context pi4j)
      Creates a new seven-segment display component with the default bus and device address.
      Parameters:
      pi4j - Pi4J context
    • SevenSegmentComponent

      public SevenSegmentComponent(com.pi4j.context.Context pi4j, int bus, int device)
      Creates a new seven-segment display component with a custom bus and device address.
      Parameters:
      pi4j - Pi4J context
      bus - Bus address
      device - Device address
  • Method Details

    • print

      public void print(int i)
      Prints the given integer value to the seven-segment display. This works by converting the integer into a string and passing it to print(String). Please note that due to the limitations of the display only the first four digits will be displayed. This will clear the buffer and automatically call HT16K33.refresh() afterwards to immediately display the number.
      Parameters:
      i - Integer to display
    • print

      public void print(double d)
      Prints the given double value to the seven-segment display, automatically setting the decimal point if applicable. This works by converting the double into a string and passing it to print(String). Please note that due to the limitations of the display only the first four digits will be displayed. This will clear the buffer and automatically call HT16K33.refresh() afterwards to immediately display the number.
      Parameters:
      d - Double to display
    • print

      public void print(LocalTime time)
      Prints the hours and minutes of the given LocalTime instance to the seven-segment display. The time will be displayed in 24 hours format as HH:MM with the colon being active for every odd second. This will clear the buffer and automatically call HT16K33.refresh() afterwards to immediately display the time.
      Parameters:
      time - Time to display
    • print

      public void print(String s)
      Prints the first four letters (or less if applicable) of the given string to the display. Additional letters are ignored. Adding a dot after a digit or putting a colon after two digits causes the respective symbols to be displayed too. This will clear the buffer and automatically call HT16K33.refresh() afterwards to immediately display the text.

      Example: "1.2:34." will print the string "1234" to the display, with decimal point #1 and #4 as well as the colon symbol being set

      Parameters:
      s - String which should be printed
    • setColon

      public void setColon(boolean enabled)
      Enables or disables the colon symbol of the seven-segment display. This will only affect the internal buffer and does not get displayed until HT16K33.refresh() gets called.
      Parameters:
      enabled - Specify if colon should be enabled or disabled.
    • setDecimalPoint

      public void setDecimalPoint(int position, boolean enabled)
      Enables or disables the decimal point at the given digit position. Please note that the decimal point is just an additional belonging to the same digit. This means that overriding the specific digit (e.g. using setRawDigit(int, byte) will reset the decimal point. This will only affect the internal buffer and does not get displayed until HT16K33.refresh() gets called.
      Parameters:
      position - Desired position of digit from 0-3.
      enabled - Specify if decimal point should be enabled or disabled.
    • setDigit

      public void setDigit(int position, int i)
      Sets the digit at the specified position to the given integer. The integer must not contain more than one number, in other words it has to be in the range 0-9. This will only affect the internal buffer and does not get displayed until HT16K33.refresh() gets called.
      Parameters:
      position - Desired position of digit from 0-3.
      i - Single digit number which should be displayed.
    • setDigit

      public void setDigit(int position, char c)
      Sets the digit at the specified position to the given character. The character must be representable by the seven-segment display, so only a subset of chars is supported. This will only affect the internal buffer and does not get displayed until HT16K33.refresh() gets called.
      Parameters:
      position - Desired position of digit from 0-3.
      c - Character which should be displayed.
    • setDigit

      public void setDigit(int position, SevenSegmentComponent.Segment... segments)
      Sets the digit at the specified position to match the given segments. This will only affect the internal buffer and does not get displayed until HT16K33.refresh() gets called.
      Parameters:
      position - Desired position of digit from 0-3.
      segments - Segments which should be displayed.
    • setRawDigit

      protected void setRawDigit(int position, byte value)
      Sets the raw digit at the specified position. This method will take a byte value which gets processed by the underlying chip. The byte represents a bitset where each bit belongs to a specific segment and decides if its enabled (1) or disabled (0). Valid values can be crafted using the fromSegments(Segment...) method. This will only affect the internal buffer and does not get displayed until HT16K33.refresh() gets called.
      Parameters:
      position - Desired position of digit from 0-3.
      value - Raw byte value to be displayed.
    • getRawDigit

      protected byte getRawDigit(int position)
      Gets the raw digit at the specified position. This method will return the internal byte value of the underlying chip. The byte represents a bitset where each bit belongs to a specific segment and decides if its enabled (1) or disabled (0).
      Parameters:
      position - Desired position of digit from 0-3.
      Returns:
      Raw byte value at specified position.
    • getI2C

      protected com.pi4j.io.i2c.I2C getI2C()
      Returns the created I2C instance for the seven-segment display
      Returns:
      I2C instance
    • fromSegments

      protected static byte fromSegments(SevenSegmentComponent.Segment... segments)
      Helper method for creating a raw digit value (byte) from 0-n segments. This can be used together with the SevenSegmentComponent.Segment enumeration to create and display your own digits. All segments passed to this method will be flagged as active and enabled when passed to setRawDigit(int, byte)
      Parameters:
      segments - Segments which should be enabled to together
      Returns:
      Raw digit value as byte