Something went wrong. Try again.
RaspberryPi version of the Hack&Tell countdown timer.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274#include <ctime>#include <iostream>#include <locale>#include "ofApp.h"
//--------------------------------------------------------------void ofApp::setup(){ if(wiringPiSetup() == -1){ printf("Error on wiringPi setup"); } pinMode(8,OUTPUT); ofSetVerticalSync(true); resetButton.addListener(this,&ofApp::resetButtonPressed); matelightFont.loadFont("OSP-DIN.ttf", 68, true, true, true); signsFont.loadFont("OSP-DIN.ttf", 100, true, true, true ); textFont.loadFont("OSP-DIN.ttf", 32, true, true, true); //ofxGuiSetFont("OSP-DIN.ttf", 14, true, true, true); //ofxGuiSetDefaultHeight(22); lastTime = 0; isMenuHidden = false; isTimerRunning = false; gui.setup("Main menu (h)"); // gui.add(screenSize.setup("Main menu", "")); gui.add(resetButton.setup("Reset timer (r)")); gui.add(paused.setup("Pause (p/space)", true)); gui.add(minutes.setup("Minutes", 5, 1, 120)); gui.add(showApplause.setup("Show applause (a)", false)); gui.add(showMatelightPreview.setup("Show ML preview", false));
millisecondsTotal = minutes; millisecondsLeft = minutes * 60 * 1000; matelightFbo.allocate(160, 64, GL_RGBA); matelightPixels.allocate(160, 64, OF_PIXELS_RGBA); matelightSmall.allocate(40, 16, OF_PIXELS_RGBA); udpConnection.Create(); udpConnection.Connect("10.0.1.39", 1337); udpConnection.SetNonBlocking(true);
}
void ofApp::resetButtonPressed() { paused = true; showApplause = false; millisecondsLeft = minutes * 60 * 1000;}
//--------------------------------------------------------------void ofApp::update(){ if (!showApplause) { updateTimeLeft(); } else { paused = true; } // update time left display int minutes = millisecondsLeft / 1000 / 60; int seconds = (millisecondsLeft - (minutes * 60 * 1000)) / 1000; int millis = millisecondsLeft - (minutes * 1000 * 60) - (seconds * 1000);
std::stringstream ss; ss << setfill('0') << setw(2) << seconds; std::stringstream ms; ms << setfill('0') << setw(3) << millis; std::string s1 = ofToString(minutes) + ":" + ofToString(ss.str()); std::string s2 = s1 + "." + ofToString(ms.str()); strncpy(timeLeftStr, s2.c_str(), sizeof(timeLeftStr)); // update the local time std::time_t t = std::time(NULL); std::strftime(localTimeStr, sizeof(localTimeStr), "%H:%M:%S", std::localtime(&t)); updateMatelight(s1);}
//--------------------------------------------------------------void ofApp::updateTimeLeft(){ if (showApplause) { return; } if (paused) { lastTime = 0; return; } int now = ofGetElapsedTimeMillis(); int elapsed = now - lastTime; if (lastTime == 0) { lastTime = now; return; } millisecondsLeft = millisecondsLeft - elapsed; lastTime = now; if (millisecondsLeft <= 0) { showApplause = true; }
}
//--------------------------------------------------------------void ofApp::updateMatelight(std::string text) { matelightFbo.begin(); ofClear(255,255,255, 0); ofBackground(0, 0, 0); ofSetColor(150, 0, 150); drawStringMono(&matelightFont, text, 20, 62, 34); ofSetColor(255, 255, 255); matelightFbo.end(); matelightFbo.readToPixels(matelightPixels); matelightPixels.resizeTo(matelightSmall, OF_INTERPOLATE_NEAREST_NEIGHBOR); matelightPreview.setFromPixels(matelightSmall); char message[1924];
int size = matelightSmall.size(); int idx = 0; for(int y = 0; y < 16; y++){ for(int x = 0; x < 40; x++) { ofColor_<unsigned char> c = matelightSmall.getColor(x, y); char r = c.r; char g = c.g; char b = c.b; message[idx + 0] = r; message[idx + 1] = g; message[idx + 2] = b; idx = idx + 3; } } message[1920] = 0x0; message[1921] = 0x0; message[1922] = 0x0; message[1923] = 0x0; udpConnection.Send(message, 1924);}
//--------------------------------------------------------------void ofApp::drawStringMono(ofTrueTypeFont* font, std::string text, float x, float y, float w) { for (int i = 0 ; i < text.length(); i++) { std::string c = ofToString(text[i]); // some characters (e.g. '.' and ':' are very small and need to be // shifted to the center of the character. The variable 'add' is added // float add = 0; if (c == ":" || c == ".") { add = w * 0.24; } font->drawString(c, x + (i * w)+ add, y); } }
//--------------------------------------------------------------void ofApp::drawApplause() { int now = ofGetSeconds(); if (now % 2 == 0) { digitalWrite(8, HIGH) ; ofSetColor(0, 0, 0); ofBackground(255, 255, 255); signsFont.drawString("APPLAUSE", 155, 250); } else { digitalWrite(8, LOW) ; ofBackground(0, 0, 0); ofSetColor(255, 255, 255); signsFont.drawString("APPLAUSE", 155, 250); }}
//--------------------------------------------------------------void ofApp::drawCountDown() { ofBackground(0, 0, 0); textFont.drawString("Time left", 104, 150); drawStringMono(&signsFont, timeLeftStr, 100, 260, 50); ofSetColor(255, 255, 255); textFont.drawString("Current local time", 104, 380); drawStringMono(&signsFont, localTimeStr, 100, 490, 50);}
//--------------------------------------------------------------void ofApp::draw(){ if (showApplause) { drawApplause(); } else { digitalWrite(8, LOW); drawCountDown(); } if (showMatelightPreview) { matelightPreview.draw(680, 560); } if (!isMenuHidden) { gui.draw(); }}
//--------------------------------------------------------------void ofApp::exit(){ resetButton.removeListener(this,&ofApp::resetButtonPressed);}
//--------------------------------------------------------------void ofApp::keyPressed(int key){ if (key == 'h') { isMenuHidden = !isMenuHidden; return; } if (key == 'a') { showApplause = !showApplause; } if (key == 'r') { resetButtonPressed(); return; } if (key == 'p' || key == ' ') { paused = !paused; } if (key == 'q') { ofExit(); }}
//--------------------------------------------------------------void ofApp::keyReleased(int key){
}
//--------------------------------------------------------------void ofApp::mouseMoved(int x, int y ){
}
//--------------------------------------------------------------void ofApp::mouseDragged(int x, int y, int button){
}
//--------------------------------------------------------------void ofApp::mousePressed(int x, int y, int button){ }
//--------------------------------------------------------------void ofApp::mouseReleased(int x, int y, int button){
}
//--------------------------------------------------------------void ofApp::windowResized(int w, int h){ }
//--------------------------------------------------------------void ofApp::gotMessage(ofMessage msg){
}
//--------------------------------------------------------------void ofApp::dragEvent(ofDragInfo dragInfo){
}