Skip to content

Instantly share code, notes, and snippets.

@lab101
Last active December 26, 2015 17:18
Show Gist options
  • Save lab101/7185718 to your computer and use it in GitHub Desktop.
Save lab101/7185718 to your computer and use it in GitHub Desktop.
Loading images from a Transcend SD wifi in Cinder.
//
// TranscendSDWifi.h
// Puppeteer
//
// Created by Kris Meeusen on 28/08/13.
//
//
#ifndef __Puppeteer__TranscendSDWifi__
#define __Puppeteer__TranscendSDWifi__
#include <iostream>
#include <memory>
#include "cinder/Thread.h"
#include "cinder/Surface.h"
class TranscendSDWifi{
typedef std::shared_ptr<std::thread> ThreadRef;
volatile bool mRunning;
ThreadRef mThread;
std::string mIp;
public:
void loadData();
void refresh();
void start(std::string ip);
void stop();
void rotate(ci::Surface& image);
};
#endif /* defined(__Puppeteer__TranscendSDWifi__) */
//
// TranscendSDWifi.cpp
// Puppeteer
//
// Created by Kris Meeusen on 28/08/13.
//
//
#include "TranscendSDWifi.h"
#include "cinder/app/AppBasic.h"
#include "cinder/Url.h"
#include "cinder/Utilities.h"
#include "cinder/ImageIo.h"
#include "cinder/Thread.h"
//project specific.
#include "Photo.h"
using namespace ci;
using namespace std;
void TranscendSDWifi::start(std::string ip)
{
mIp = ip;
stop();
mRunning = true;
mThread = ThreadRef( new std::thread( &TranscendSDWifi::loadData, this ) );
}
void TranscendSDWifi::stop()
{
mRunning = false;
if ( mThread ) {
mThread->join();
mThread.reset();
}
}
void TranscendSDWifi::refresh(){
}
void TranscendSDWifi::loadData(){
while (mRunning) {
try{
string data = loadString( loadUrl( "http://" + mIp + "/cgi-bin/tslist?PATH=/mnt/sd/DCIM/575___09",UrlOptions().timeout(3.0f).ignoreCache() ) );
std::istringstream f(data);
string line;
int lineCount = 0;
while (std::getline(f, line)) {
if(++lineCount == 3){
//std::cout << line << std::endl;
std::vector<string> files = split(line, '&');
for(vector<string>::iterator it = files.begin();it != files.end();++it){
if((*it).find("FileName") !=std::string::npos){
std::vector<string> filenames = split((*it), '=');
// string filePath = ;
ci::fs::path filePath(app::getAssetPath("/photos/").string() + "/" + filenames[1]);
if(!fs::exists(filePath)){
Surface image = loadImage(loadUrl("http://" + mIp + "/sd/DCIM/575___09/" + filenames[1]));
if(image.getWidth() > image.getHeight()){
rotate(image);
}
writeImage(filePath, image);
Photo p(filePath);
SettingManager::Instance()->photos.push_back(p);
cout << filenames[1] << std::endl;
}
}
}
}
}
}catch(...){
std::cout << "error loading data" << endl;
}
ci::sleep(4000);
}
// std::cout << "url data: " << html << std::endl;
}
void TranscendSDWifi::rotate(Surface& image){
Surface rotatedImage(image.getHeight(),image.getWidth(),false);
for (int x = 0; x < image.getWidth(); x++) {
for (int y = 0; y < image.getHeight(); y++) {
rotatedImage.setPixel(Vec2i(y,x), image.getPixel(Vec2i(x,y)));
}
}
image = rotatedImage;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment