Exemplo n.º 1
0
/**
 * @brief Read a control command (GO, SPIN, STOP, FADE LED)
 */
void ReadCommand::readControlCommand(void) {
	type = COMMAND_NONE;

	uint8_t actionByte = readByte();

	if (actionByte > 0x03)
		return;

	type = (COMMAND_TYPE)actionByte;

	switch (type) {
		case COMMAND_GO:
			cmd.go.direction = (Direction)readByte();
			cmd.go.speed = readByte();
			cmd.go.duration = readTwoBytes();
			break;

		case COMMAND_SPIN:
			cmd.spin.rotation = (Rotation)readByte();
			cmd.spin.speed = readByte();
			cmd.spin.angle = readTwoBytes();
			break;

		case COMMAND_STOP:
			break;

		case COMMAND_FADE:
			cmd.fade.indicator = (LedIndicator)readByte();
			cmd.fade.startR = readByte();
			cmd.fade.startG = readByte();
			cmd.fade.startB = readByte();
			cmd.fade.endR = readByte();
			cmd.fade.endG = readByte();
			cmd.fade.endB = readByte();
			cmd.fade.duration = readTwoBytes();

		default:
			break;
	}

	_header = 0; /* Reset the header to tell that the control command has been handled */
}
Exemplo n.º 2
0
struct MIDIfile * loadFile(const char * filename)
{
    struct MIDIfile * mfload;
    int file = rb->open (filename, O_RDONLY);

    if(file < 0)
    {
        printf("Could not open file");
        return NULL;
    }

    mfload = &midi_file;

    rb->memset(mfload, 0, sizeof(struct MIDIfile));

    int fileID = readID(file);
    if(fileID != ID_MTHD)
    {
        if(fileID == ID_RIFF)
        {
            printf("Detected RMID file");
            printf("Looking for MThd header");
            char dummy[17];
            rb->read(file, &dummy, 16);
            if(readID(file) != ID_MTHD)
            {
                rb->close(file);
                printf("Invalid MIDI header within RIFF.");
                return NULL;
            }

        } else
        {
            rb->close(file);
            printf("Invalid file header chunk.");
            return NULL;
        }
    }

    if(readFourBytes(file)!=6)
    {
        rb->close(file);
        printf("Header chunk size invalid.");
        return NULL;
    }

    if(readTwoBytes(file)==2)
    {
        rb->close(file);
        printf("MIDI file type 2 not supported");
        return NULL;
    }

    mfload->numTracks = readTwoBytes(file);
    mfload->div = readTwoBytes(file);

    int track=0;

    printf("File has %d tracks.", mfload->numTracks);

    while(! eof(file) && track < mfload->numTracks)
    {
        unsigned char id = readID(file);


        if(id == ID_EOF)
        {
            if(mfload->numTracks != track)
            {
                printf("Warning: file claims to have %d tracks. I only see %d here.", mfload->numTracks, track);
                mfload->numTracks = track;
            }
            rb->close(file);
            return mfload;
        }

        if(id == ID_MTRK)
        {
            mfload->tracks[track] = readTrack(file);
            track++;
        } else
        {
            printf("SKIPPING TRACK");
            int len = readFourBytes(file);
            while(--len)
                readChar(file);
        }
    }

    rb->close(file);
    return mfload;

}
Exemplo n.º 3
0
errorT
TreeCache::ReadFile (const char * fname)
{
    // Only read the file if the cache is empty:
    if (NumInUse > 0) { return OK; }
#ifdef WINCE
    /*FILE * */Tcl_Channel fp;
    fileNameT fullname;
    strCopy (fullname, fname);
    strAppend (fullname, TREEFILE_SUFFIX);

    //fp = fopen (fullname, "rb");
    fp = mySilent_Tcl_OpenFileChannel(NULL, fullname, "r", 0666);
    if (fp == NULL) {
        return ERROR_FileOpen;
    }
 my_Tcl_SetChannelOption(NULL, fp, "-encoding", "binary");
 my_Tcl_SetChannelOption(NULL, fp, "-translation", "binary");

    uint magic = readFourBytes (fp);
    if (magic != TREEFILE_MAGIC) {
        //fclose (fp);
        my_Tcl_Close(NULL, fp);

#else
    FILE * fp;
    fileNameT fullname;
    strCopy (fullname, fname);
    strAppend (fullname, TREEFILE_SUFFIX);

    fp = fopen (fullname, "rb");
    if (fp == NULL) {
        return ERROR_FileOpen;
    }

    uint magic = readFourBytes (fp);
    if (magic != TREEFILE_MAGIC) {
        fclose (fp);
#endif
        return ERROR_Corrupt;
    }
    readTwoBytes (fp);  // Scid Version; unused
    uint cacheSize = readFourBytes (fp);
    SetCacheSize (cacheSize);
    NumInUse = readFourBytes (fp);
    LowestTotal = readFourBytes (fp);
    LowestTotalIndex = readFourBytes(fp);

    for (uint count=0; count < NumInUse; count++) {
        cachedTreeT * ctree = &(Cache[count]);
        ctree->toMove = readOneByte (fp);
        for (squareT sq=0; sq < 64; sq++) {
            ctree->board[sq] = readOneByte (fp);
        }

        // Read the moves:
        ctree->tree.moveCount = readFourBytes (fp);
        ctree->tree.totalCount = readFourBytes (fp);

        uint numMoves = ctree->tree.moveCount;
        for (uint i=0; i < numMoves; i++) {
            // Read this move node:

            treeNodeT * tnode = &(ctree->tree.node[i]);
            readSimpleMove (fp, &(tnode->sm));
            readString (fp, tnode->san, 8);
            for (uint res = 0; res < 4; res++) {
                tnode->freq[res] = readFourBytes (fp);
            }
            tnode->total = readFourBytes (fp);
            tnode->score = readFourBytes (fp);
            tnode->ecoCode = readTwoBytes (fp);
            tnode->eloCount = readFourBytes (fp);
            tnode->eloSum = readFourBytes (fp);
            tnode->perfCount = readFourBytes (fp);
            tnode->perfSum = readFourBytes (fp);
            tnode->yearCount = readFourBytes (fp);
            tnode->yearSum = readFourBytes (fp);
        }

        // Read the compressed filter:
        ctree->cfilter = new CompressedFilter;
        ctree->cfilter->ReadFromFile (fp);
    }
#ifdef WINCE
     my_Tcl_Close(NULL, fp);
#else
    fclose (fp);
#endif
    return OK;
}