flipdot/mqtt_flipdot_simulator/mqtt_flipdot_simulator.pde

98 lines
2.5 KiB
Plaintext
Raw Normal View History

2024-12-03 15:49:24 +00:00
import mqtt.*;
MQTTClient client;
static int pixeldistance=16;
static int pixelsize=14;
static int displaywidth=75;
static int displayheight=16;
static float pixelcornerradius=3;
static float pixelborderweight=1;
color colorWhite=color(240,255,150);
color colorBlack=color(50,50,50);
2024-12-03 20:09:07 +00:00
color colorBG=color(127,127,127);
2024-12-03 15:49:24 +00:00
String lastDataReceived="";
2024-12-03 20:09:07 +00:00
int lastDataReceivedTime=0;
2024-12-03 15:49:24 +00:00
PGraphics pg;
2024-12-03 20:09:07 +00:00
PFont smallfont;
// The font must be located in the sketch's
// "data" directory to load successfully
2024-12-03 15:49:24 +00:00
void setup() {
size(1300,300);
2024-12-03 20:09:07 +00:00
smooth(0);
2024-12-03 15:49:24 +00:00
pg = createGraphics(int(displaywidth*pixeldistance+pixelborderweight/2),int(displayheight*pixeldistance+pixelborderweight/2));
2024-12-03 20:09:07 +00:00
background(colorBG);
//printArray(PFont.list());
smallfont = createFont("Monospaced.plain", 12);
2024-12-03 15:49:24 +00:00
client = new MQTTClient(this);
client.connect("mqtt://192.168.1.6", "processing-flipdot");
}
void draw() {
2024-12-03 20:09:07 +00:00
fill(colorBG);
rect(0,0,width,height);
2024-12-03 15:49:24 +00:00
pg.beginDraw();
pg.clear();
for (int y=0;y<displayheight;y++){
for (int x=0;x<displaywidth;x++){
pg.strokeWeight(pixelborderweight);
2024-12-03 20:09:07 +00:00
if (getPixelFromString(x,y)) {
2024-12-03 15:49:24 +00:00
pg.stroke(0,0,0);
pg.fill(colorWhite);
}else{
pg.stroke(0,0,0);
pg.fill(colorBlack);
}
pg.rect(x*pixeldistance+(pixeldistance-pixelsize)/2,y*pixeldistance+(pixeldistance-pixelsize)/2, pixelsize,pixelsize, pixelcornerradius) ;
}
}
pg.endDraw();
image(pg,5,5);
2024-12-03 20:09:07 +00:00
textFont(smallfont);
fill(255,255,255);
int showFirstData=100;
int showLastData=32;
text(lastDataReceived.substring(0,min(showFirstData,lastDataReceived.length()))+"..",5,pg.height+16);
text(".."+lastDataReceived.substring(max(lastDataReceived.length()-showLastData,0),lastDataReceived.length()), (showFirstData+2)*7.3 ,pg.height+16);
text("length="+lastDataReceived.length(), (showFirstData+2)*7.3+(showLastData+2)*7.3 ,pg.height+16);
text((millis()-lastDataReceivedTime)/1000.0+"s", 5,pg.height+16+16);
2024-12-03 15:49:24 +00:00
}
boolean getPixelFromString(int x,int y){
2024-12-03 20:09:07 +00:00
if (lastDataReceived.length()>x*displayheight+y){
return lastDataReceived.charAt(x*displayheight+y)=='1';
}else{
return false;
}
2024-12-03 15:49:24 +00:00
}
void clientConnected() {
println("client connected");
client.subscribe("testflipdot/display/data/set");
}
void messageReceived(String topic, byte[] payload) {
//println("new message: " + topic + " - " + new String(payload));
println("new message: " + topic + " len= " + (new String(payload)).length());
lastDataReceived=new String(payload);
2024-12-03 20:09:07 +00:00
lastDataReceivedTime=millis();
2024-12-03 15:49:24 +00:00
}
void connectionLost() {
println("connection lost");
}