コード例 #1
0
// ----------------------------------------------------------------------------
//
UINT AbstractDMXDriver::run(void) {
    DMXStudio::log_status( "DMX driver started [%s]", m_connection_info );

    while ( isRunning() ) {
        DMX_STATUS status;
        
        status = dmx_send( DMX_PACKET_SIZE+1, m_blackout ? m_blackout_packet : m_packet );
        if ( status != DMX_OK ) {
            DMXStudio::log( "DMX send error %d", status );
        }
        
        DWORD next_frame = GetTickCount() + getPacketDelayMS();

        if ( m_debug ) {
            CString buffer;
            for ( channel_t chan=1; chan <= DMX_PACKET_SIZE; chan++ ) {
                buffer.Format( "%03d=%02x ", chan, m_packet[chan] );
                if ( chan % 16 == 0 ) {
                    DMXStudio::log( buffer );
                    buffer.Empty();
                }
            }
            if ( buffer.GetLength() != 0 )
                DMXStudio::log( buffer );
        }

        Sleep( m_packet_min_delay );		                // Minimal sleep time

        // MTBP - Mark time between packets
        long sleep_ms = next_frame - GetTickCount();

        if ( ::WaitForSingleObject( m_wake.m_hObject, sleep_ms ) == WAIT_OBJECT_0 ) {
            if ( m_latch ) {
                memcpy( m_packet, m_pending_packet, sizeof(m_packet) );
                m_latch = false;
            }
        }
    }

    DMXStudio::log_status( "DMX driver stopped [%s]", m_connection_info );

    AfxEndThread( DMX_OK );

    return DMX_OK;
}
コード例 #2
0
ファイル: dmx_server.c プロジェクト: elionne/lutopia
int main(int argc, char* argv[])
{
    dmx_handler h;
    int s = socket(AF_INET, SOCK_DGRAM, 0);

    if(s <= 0 ){
        printf("Error while opening socket, abort. %s\n", strerror(errno));
        return EXIT_FAILURE;
    }

    struct sockaddr_in in;

    in.sin_addr.s_addr = INADDR_ANY;
    in.sin_port = htons(21812);
    in.sin_family = AF_INET;

    if( bind(s, (struct sockaddr*)&in, sizeof(in)) < 0 ){
        printf("Could not bind the socket. %s\n", strerror(errno));
        return EXIT_FAILURE;
    }


    h = dmx_open(dmx_drv_list[0], 0);
    if(!h)
      return EXIT_FAILURE;

    do{
        struct timeval before, after, timeout = { 0, 24e3 };
        unsigned char dmx_data[513];
        int len = 0;
        /* send data at full speed rate or wait until data incoming to send to dmx */
        fd_set fd;
        FD_ZERO(&fd);
        FD_SET(s, &fd);

        gettimeofday(&before, 0);

        int error = select(s+1, &fd, 0, 0, &timeout);

        gettimeofday(&after, 0);
        if( error == 0 )
            wake_up_task("popo");
        else if( error > 0 )
        {
            /* flush the buffer, returns only the last packet sent */
            int count = 0;
            struct timeval flush = { 0, 0};
            FD_ZERO(&fd);
            FD_SET(s, &fd);
            do{
                len = recv(s, dmx_data, sizeof(dmx_data), 0);
                count++;
            }while( select(s+1, &fd, 0, 0, &flush ) > 0 );

            if( len < sizeof(dmx_data) )
                printf("corrupted data receive\n");
            #if 1
            if( count > 1 )
                printf("multiple data received %i\n", count);
            #endif

            struct timeval timediff;
            timersub(&after, &before, &timediff);
            timersub(&timeout, &timediff, &timeout);

        }
        dmx_data[0] = 0;

        len = dmx_send(dmx_drv_list[0], h, dmx_data);
        if( len != 513)
            printf("send %i bytes to dmx\r", len);

    }while(1);

    dmx_close(dmx_drv_list[0], h);
    close(s);
    return EXIT_SUCCESS;
}