Exemple #1
0
void ngc_handle_packet() {
    // translate samples in sample_buff to joydata_ngc
	uint8_t idx = sample_w - sample_buff;
    // dbgs("ngc packet len: "); dbgsval(idx); dbgs("\n");
    
    // 89 = 24 bits request, 1 stopbit, + 64 bits of data
	if (idx == 89) {        
		uint8_t* w = (uint8_t*)&joydata_ngc_raw;
        
		// bit 0 is not sampled, then 0-23 are request, 24 is stop bit, 25-88 is data
		int8_t* r = (int8_t*)(sample_buff + 25);
		for (uint8_t i = 0; i < sizeof(ngc_packet_t); i++) {
            *w++ = pack_byte(r);
            r += 8;
		}

        packets.ngc_avail = true;
	}
    else if (idx != 8 && idx != 24) { // 8,24 for poll, with no controller attached/fake mode
        // dbgs("ngc unexpected packet len: "); dbgsval(idx); dbgs("\n");
    }
}
Exemple #2
0
static void ch_damage(struct rwstat_ch* ch, uint8_t val, bool rand,
	uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2)
{
	struct rwstat_ch_priv* chp = ch->priv;
	struct arcan_shmif_cont* cont = chp->cont;

/* useless in this setting */
	if (chp->map == MAP_TUPLE || chp->map == MAP_TUPLE_ACC)
		return;

	if (y2 > cont->h || x2 > cont->w)
		return;

	for (size_t y = y1; y < y2; y++)
		for (size_t x = x1; x < x2; x++)
			cont->vidp[y*chp->cont->pitch+x] = pack_byte(val, rand, chp->pack);

/* don't increment the counter, we stay at the same position etc.
 * just send the new damaged buffer. If we want to do fault-injection,
 * it'll need to be on the sensor- level */
	arcan_shmif_signal(chp->cont, SHMIF_SIGVID);
}
Exemple #3
0
// reads the sequence data from the FASTA file, encodes and packs it into a .pac file,
// sequence annotations are stored into an .ann file
void fasta2pac(const char *fastaFname, const char* pacFname, const char* annFname) {
	FILE * fastaFile = (FILE*) fopen(fastaFname, "r");
	if (fastaFile == NULL) {
		printf("fasta2pac: Cannot open FASTA file: %s!\n", fastaFname);
		exit(1);
	}
	FILE * pacFile = (FILE*) fopen(pacFname, "wb");
	if (pacFile == NULL) {
		printf("fasta2pac: Cannot open PAC file: %s!\n", pacFname);
		exit(1);
	}
	FILE * annFile = (FILE*) fopen(annFname, "wb");
	if (annFile == NULL) {
		printf("fasta2pac: Cannot open ANN file: %s!\n", annFname);
		exit(1);
	}

	unsigned char seqBuffer[SEQ_BATCH_ALLOC_LEN];
	unsigned char packedSeqBuffer[SEQ_BATCH_ALLOC_LEN/CHARS_PER_BYTE];
	bwtint_t allocatedSeqLen = SEQ_BATCH_ALLOC_LEN;
	char* seq = (char*) malloc(allocatedSeqLen * sizeof(char));
	bwtint_t totalSeqLen = 0;
	bwtint_t seqBufferCount = 0;

	fasta_annotations_t* annotations = (fasta_annotations_t*) calloc(1, sizeof(fasta_annotations_t));
	bwtint_t allocatedAnnNum = ANN_ALLOC_LEN;
	annotations->seq_anns = (seq_annotation_t*) malloc(allocatedAnnNum * sizeof(seq_annotation_t));

	char c = (char) getc(fastaFile);
	if(c != '>') fasta_error(fastaFname);

	while(!feof(fastaFile)) {
		if(allocatedAnnNum == annotations->num_seq) {
			allocatedAnnNum <<= 1;
			annotations->seq_anns = (seq_annotation_t*)realloc(annotations->seq_anns, allocatedAnnNum * sizeof(seq_annotation_t));
		}
		seq_annotation_t* seqAnnotation = &(annotations->seq_anns[annotations->num_seq]);

		c = (char) getc(fastaFile);

		// sequence description line (> ...)
		bwtint_t seqNameLen = 0;
		while(c != '\n' && seqNameLen < MAX_SEQ_NAME_LEN && !feof(fastaFile)){
			seqAnnotation->name[seqNameLen] = c;
			seqNameLen++;
			c = (char) getc(fastaFile);
		}
		seqAnnotation->name[seqNameLen]='\0';
		while(c != '\n' && !feof(fastaFile)){
			c = (char) getc(fastaFile);
		}
		if(feof(fastaFile)) fasta_error(fastaFname);

		// sequence data
		bwtint_t seqLen = 0;
		while(c != '>' && !feof(fastaFile)){
			if (c != '\n'){
				if (c >= 'a' && c <= 'z'){
					c += 'A'-'a';
				}
				// reallocate twice as much memory
				if (seqLen >= allocatedSeqLen) {
					allocatedSeqLen <<= 1;
					seq = (char*)realloc(seq, sizeof(char)*allocatedSeqLen);
				}

				*(seq + seqLen) = c;
				seqLen++;
			}
			c = (char) getc(fastaFile);
		}
		// add $ as a separator between chromosomes and bubbles
		// s.t. no match is found spanning multiple sequences
		*(seq + seqLen) = '$';
		seqLen++;
		printf("Done reading a sequence of size %" PRIbwtint_t " from FASTA\n", seqLen);

		// pack and store the sequence
		for(bwtint_t i = 0; i < seqLen; i++) {
			if (seqBufferCount >= SEQ_BATCH_ALLOC_LEN) {
				pack_byte(seqBuffer, packedSeqBuffer, SEQ_BATCH_ALLOC_LEN);
				fwrite(packedSeqBuffer, 1, SEQ_BATCH_ALLOC_LEN / CHARS_PER_BYTE, pacFile);
				seqBufferCount = 0;
			}
			seqBuffer[seqBufferCount] = nt16_table[(unsigned int) seq[i]];
			//if(seqBuffer[seqBufferCount] == 10) { // ambiguous base
				// replace by a random ACGT base to improve performance
				//seqBuffer[seqBufferCount] = nt4_gray[lrand48()&3];
			//}
			seqBufferCount++;
		}

		// record the sequence range in the concatenated genome
		seqAnnotation->start_index = totalSeqLen;
		seqAnnotation->end_index = totalSeqLen + seqLen - 1;

		totalSeqLen += seqLen;
		annotations->num_seq++;
	}

	// pack any remaining chars
	if (seqBufferCount > 0) {
		pack_byte(seqBuffer, packedSeqBuffer, seqBufferCount);
		fwrite(packedSeqBuffer, 1, ceil((float)seqBufferCount/ CHARS_PER_BYTE), pacFile);
		seqBufferCount = 0;
	}
	c = (char)(totalSeqLen % CHARS_PER_BYTE);
	fwrite(&c, 1, 1, pacFile);

	// store annotations info
	fprintf(annFile, "%" PRIbwtint_t "\t%d\n", totalSeqLen, annotations->num_seq);
	for(int i = 0; i < annotations->num_seq; i++) {
		seq_annotation_t seqAnnotation = annotations->seq_anns[i];
		fprintf(annFile, "%s\t%" PRIbwtint_t "\t%" PRIbwtint_t "\n", seqAnnotation.name, seqAnnotation.start_index, seqAnnotation.end_index);
	}
	printf("Done reading FASTA file. Total sequence length read = %" PRIbwtint_t "\n", totalSeqLen);

	fclose(fastaFile);
	fclose(pacFile);
	fclose(annFile);

	free(annotations->seq_anns);
	free(annotations);
	free(seq);
}