Jump to content

Recommended Posts

Posted

Without the API, you need to jump through a few hoops to manage the segments yourself..

package com.softwarepearls.apps.hardware.tinkerforge.clock.sevensegment;

import static com.softwarepearls.lego.hardware.tinkerforge.enums.BrickletType.BRICKLET_SEGMENT_DISPLAY_4x7;

import com.softwarepearls.lego.graphics.SevenSegmentDigits;
import com.softwarepearls.lego.hardware.tinkerforge.interfaces.io.SegmentDisplay4x7;
import com.softwarepearls.lego.hardware.tinkerforge.stack.BrickletDescriptor;
import com.softwarepearls.lego.hardware.tinkerforge.stack.TinkerForgeStack;
import com.softwarepearls.lego.time.Frequency;
import com.softwarepearls.lego.time.TimeAndDateKit;
import com.tinkerforge.TinkerforgeException;

import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
* Bare-bones 24 hour HH:MM clock rendered on a Tinkerforge 7-segment display.
*/
public final class Clock {

private static final int NUM_DIGITS = 4;
private static final String DISPLAY_UID = "kTz";
private static final String TO_TF_SEGMENTS_LUT = "AFBGECD";
private final static DateFormat DATE_FORMATTER = new SimpleDateFormat("HHmm");

private static final List<BrickletDescriptor> EXPECTED_BRICKLETS = Arrays.asList(
//
		new BrickletDescriptor(BRICKLET_SEGMENT_DISPLAY_4x7, DISPLAY_UID));

private static void go() throws TinkerforgeException, IOException {

	final TinkerForgeStack tinkerForgeStack = TinkerForgeStack.initializeTinkerForgeStack(EXPECTED_BRICKLETS);

	final SegmentDisplay4x7 display = (SegmentDisplay4x7) tinkerForgeStack.getBricklet(DISPLAY_UID);

	final Frequency clockRefreshFrequency = Frequency.EVERY_SECOND;
	while (true) {

		final int hhmmDigits = timeDigitsFor(new Date());

		final short[] segments = getSegmentBitsFor(hhmmDigits);
		final boolean flashingColon = (TimeAndDateKit.currentSecond() & 1) == 0;

		display.setSegments(segments, (short) 2, flashingColon);

		clockRefreshFrequency.delay();
	}
}

private static int timeDigitsFor(final Date time) {

	final String timeString = DATE_FORMATTER.format(time);

	return Integer.parseInt(timeString);
}

private static short[] getSegmentBitsFor(final int value) {

	if ((value < 0) || (value > 9999)) {
		throw new IllegalArgumentException("Illegal value for 7-segment display: " + value);
	}

	final short[] segments = new short[NUM_DIGITS];
	int dddd = value;
	for (int i = 0; i < NUM_DIGITS; i++) {
		segments[NUM_DIGITS - 1 - i] = segmentBitsForDigit(dddd);
		dddd = dddd / 10;
	}
	return segments;
}

private static short segmentBitsForDigit(final int d) {

	final int digit = d % 10;

	final String litSegments = SevenSegmentDigits.DIGIT_DEFINITIONS[digit];
	short segmentBits = 0x00;
	for (int i = 0; i < litSegments.length(); i++) {
		final char ch = litSegments.charAt(i);
		final int bitNumber = TO_TF_SEGMENTS_LUT.charAt(ch - 'A') - 'A';
		segmentBits |= 1 << bitNumber;
	}
	return segmentBits;
}

public static void main(final String[] args) throws TinkerforgeException, IOException {

	go();
}
}

  • 2 weeks later...
Posted

Something like an additional "displayValue(uint16 value, bool useTrailingZeros)" wouldn't hurt in the API. I wrote it on my TODO list (which is pretty long ;)).

 

But in the meantime i think you could also use startCounter with valueFrom and valueTo set to the same value.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...