Jump to content
View in the app

A better way to browse. Learn more.

Tinkerunity

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Geschrieben

 

void setCounterValue(short value); // value 0..9999

 

Would be really nice. Any plans on adding this?

 

(just trying to make a simple little clock)

 

Geschrieben
  • Autor

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...
Geschrieben

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.

Gast
Reply to this topic...

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.