Skip to content
This repository has been archived by the owner on Aug 22, 2023. It is now read-only.

Commit

Permalink
Merge pull request #2 from larsgrefer/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
larsgrefer authored Jun 6, 2017
2 parents fb6b42f + 5e44f28 commit dbedeb7
Show file tree
Hide file tree
Showing 19 changed files with 723 additions and 73 deletions.
4 changes: 4 additions & 0 deletions sense-hat/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ repositories {
jcenter()
}

artifacts {
archives jar
}

dependencies {
compile 'org.slf4j:slf4j-api:1.7.25'
compile 'com.squareup.okio:okio:1.13.0'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package de.larsgrefer.sense_hat;

import lombok.Getter;
import lombok.Setter;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.time.LocalTime;

/**
* @author Lars Grefer
*/
@Getter
@Setter
public class BigBinaryClock extends Clock {

private Color hours = Color.RED;
private Color minutes = Color.BLUE;

public BigBinaryClock(SenseHat senseHat) {
super(senseHat);
}

protected void drawTime(BufferedImage bufferedImage, LocalTime now) {
drawNumber(bufferedImage, 0, now.getHour() / 10, hours);
drawNumber(bufferedImage, 1, now.getHour() % 10, hours);

drawNumber(bufferedImage, 2, now.getMinute() / 10, minutes);
drawNumber(bufferedImage, 3, now.getMinute() % 10, minutes);
}

private void drawNumber(BufferedImage image, int column, int number, Color color) {
for (int row = 3, bit = 0; bit <= 3; row--, bit++) {
if (((number >>> bit) & 1) == 1) {
setBigPixel(image, column, row, color);
} else {
setBigPixel(image, column, row, getBackground());
}
}
}

private void setBigPixel(BufferedImage bufferedImage, int x, int y, Color color) {
bufferedImage.setRGB(x * 2, y * 2, color.getRGB());
bufferedImage.setRGB(x * 2, y * 2 + 1, color.getRGB());
bufferedImage.setRGB(x * 2 + 1, y * 2, color.getRGB());
bufferedImage.setRGB(x * 2 + 1, y * 2 + 1, color.getRGB());
}


}
55 changes: 55 additions & 0 deletions sense-hat/src/main/java/de/larsgrefer/sense_hat/Clock.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package de.larsgrefer.sense_hat;

import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.time.Duration;
import java.time.LocalTime;

/**
* Created by larsgrefer on 06.06.17.
*/
@RequiredArgsConstructor
@Getter
@Setter
@Slf4j
public abstract class Clock implements Runnable {

private final SenseHat senseHat;

private java.time.Clock clock = java.time.Clock.systemDefaultZone();

private boolean stop = false;

private Color background = Color.BLACK;

@Override
public void run() {
BufferedImage bufferedImage = new BufferedImage(8,8,BufferedImage.TYPE_USHORT_565_RGB);

while (!stop) {

LocalTime now = LocalTime.now(clock);

drawTime(bufferedImage, now);

try {
senseHat.fadeTo(bufferedImage, Duration.ofMillis(500));
} catch (IOException e) {
log.error(e.getLocalizedMessage(), e);
throw new RuntimeException(e);
}
}
}

public void setBackground(SenseHatColor background) {
this.background = background.toColor();
}

protected abstract void drawTime(BufferedImage bufferedImage, LocalTime now);
}
88 changes: 76 additions & 12 deletions sense-hat/src/main/java/de/larsgrefer/sense_hat/SenseHat.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import lombok.RequiredArgsConstructor;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
import okio.BufferedSink;
import okio.BufferedSource;
import okio.Okio;
Expand All @@ -12,12 +13,14 @@
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.time.Duration;

/**
* @author Lars Grefer
*/
@RequiredArgsConstructor
@ToString
@Slf4j
public class SenseHat {

private final File frameBuffer;
Expand Down Expand Up @@ -64,39 +67,100 @@ public void setPixel(int x, int y, SenseHatColor senseHatColor) throws IOExcepti
public BufferedImage getImage() throws IOException {
BufferedImage image = new BufferedImage(8, 8, BufferedImage.TYPE_USHORT_565_RGB);

short[] data = getDisplayData();
image.getRaster().setDataElements(0, 0, 8, 8, data);

return image;
}

private short[] getDisplayData() throws IOException {
short[] data = new short[64];
try (BufferedSource buffer = Okio.buffer(Okio.source(frameBuffer))) {
short[] data = new short[64];

for (int i = 0; i < 64; i++) {
data[i] = buffer.readShortLe();
}

image.getRaster().setDataElements(0, 0, 8, 8, data);
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
}
return data;
}

return image;
private void setDisplayData(short[] displayData) throws IOException {
try (BufferedSink buffer = Okio.buffer(Okio.sink(frameBuffer))) {

for (int i = 0; i < 64; i++) {
buffer.writeShortLe(displayData[i]);
}
buffer.flush();
} catch (FileNotFoundException e) {
log.error(e.getLocalizedMessage(), e);
}
}

public void setImage(BufferedImage image) throws IOException {
if (image.getType() == BufferedImage.TYPE_USHORT_565_RGB) {

short[] data = (short[]) image.getRaster().getDataElements(0, 0, 8, 8, new short[64]);
setDisplayData(data);

} else {
BufferedImage convertedImg = convertImage(image);

try (BufferedSink buffer = Okio.buffer(Okio.sink(frameBuffer))) {
setImage(convertedImg);
}
}

private BufferedImage convertImage(BufferedImage image) {
BufferedImage convertedImg = new BufferedImage(8, 8, BufferedImage.TYPE_USHORT_565_RGB);
Graphics graphics = convertedImg.getGraphics();
graphics.drawImage(image, 0, 0, null);
return convertedImg;
}

public void fadeTo(BufferedImage image, Duration duration) throws IOException {

if(image.getType() == BufferedImage.TYPE_USHORT_565_RGB) {
short[] oldData = getDisplayData();
SenseHatColor[] oldColors = new SenseHatColor[64];

short[] newData = (short[]) image.getRaster().getDataElements(0, 0, 8, 8, new short[64]);
SenseHatColor[] newColors = new SenseHatColor[64];

for (int i = 0; i < 64; i++) {
buffer.writeShortLe(data[i]);
for (int i = 0; i < 64; i++) {
oldColors[i] = new SenseHatColor(oldData[i]);
newColors[i] = new SenseHatColor(newData[i]);
}

short[] data = new short[64];

long sleepTime = Math.max(10, duration.toMillis() / 64);

float durationInMillis = duration.toMillis();
long start = System.currentTimeMillis();
while (true) {
long currentDuration = System.currentTimeMillis() - start;
float factor = currentDuration / durationInMillis;

if (factor < 1d) {
for (int i = 0; i < 64; i++) {
data[i] = (short) oldColors[i].mix(newColors[i], factor).getSenseHatColor();
}
setDisplayData(data);
try {
Thread.sleep(sleepTime);
} catch (InterruptedException e) {
log.error(e.getLocalizedMessage(), e);
}
} else {
setDisplayData(newData);
break;
}
buffer.flush();
}
} else {
BufferedImage convertedImg = new BufferedImage(8, 8, BufferedImage.TYPE_USHORT_565_RGB);
Graphics graphics = convertedImg.getGraphics();
graphics.drawImage(image, 0, 0, null);

setImage(convertedImg);
} else {
fadeTo(convertImage(image), duration);
}
}

Expand Down
Loading

0 comments on commit dbedeb7

Please sign in to comment.