Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

file 86 lines (70 sloc) 2.251 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
#include <iostream>
#include <cstdlib>
#include <string>
#include <vector>
#include <pthread.h>

#include "LedStrip.h"
#include "SocketListener.h"

char* global_data;

pthread_mutex_t load_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t load_signal = PTHREAD_COND_INITIALIZER;

void * dataloader(void * arg)
{
    LedStrip* strip = (LedStrip*) arg;
    while(1) {
        // Load phase
        pthread_mutex_lock(&load_mutex);
        pthread_cond_wait(&load_signal, &load_mutex);
        pthread_mutex_unlock(&load_mutex);

        strip->Flip();
        strip->LoadData(global_data + 1);
    }

    return NULL;
}

int main( int argc, const char* argv[] ) {
    // Create a thread for each LED strip
    std::vector<pthread_t*> threads;

    // Connect to a LED strip
    std::vector<LedStrip*> strips;

    // Specify the screen geometry and strips on the command line, like this:
    // host 160 40 /dev/ttyACM0 0 /dev/ttyACM1 8

    int display_height = atoi(argv[1]);
    int display_width = atoi(argv[2]);

    std::cout << "Initializing display"
              << ", width=" << display_width
              << ", height=" << display_height
              << std::endl;

    // Init a data buffer, let's only make this once. TODO: WTF!
    global_data = new char[display_width*display_height*3+1];

    for(int i = 3; i < argc; i+=2) {
        std::cout << "Adding output device, port=" << argv[i]
                  << ", offset=" << atoi(argv[i+1])
                  << std::endl;
        strips.push_back(new LedStrip(
            display_width,
            display_height,
            atoi(argv[i+1]))
        );
        strips.back()->Connect(argv[i]);

        threads.push_back(new pthread_t);
        pthread_create(
            threads.back(),
            NULL,
            dataloader,
            (void *)(strips.back())
        );
    }

    SocketListener listener;
    listener.Connect("0.0.0.0", 58082);

    while(1) {
        int status = listener.GetFrame(
            global_data,
            display_width*display_height*3+1
        );
        if (status == 0) {
// pthread_mutex_unlock(&load_mutex);
            pthread_cond_broadcast(&load_signal);
// pthread_mutex_lock(&load_mutex);
        }
    }
}
Something went wrong with that request. Please try again.