123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- #include <stdio.h>
- #include <unistd.h>
- #include <malloc.h>
- #include "emitlib.h"
- unsigned int timings[4][2] = {
- {250, 250},
- {250, 1200},
- {250, 2600},
- {250, 9900}
- };
- unsigned char numbering[4] = {0x05, 0x06, 0x09, 0x0A};
- BUFFER createBuffer()
- {
- BUFFER buffer;
- buffer.byteSize = 1;
- buffer.bitSize = 0;
- buffer.data = (char*) malloc(1);
- buffer.data[0] = 0;
- return buffer;
- }
- void destroyBuffer(BUFFER buffer)
- {
- free(buffer.data);
- }
- void printfBinaryBuffer(BUFFER buffer)
- {
- printf("bytes: %d\nbits: %d\n", buffer.byteSize, buffer.bitSize);
- unsigned char byte;
- unsigned char bit;
- unsigned int x;
- int i;
- for(x=0; x<buffer.byteSize; x++) {
- byte = buffer.data[x];
- for(i=0x80; i>0; i>>=1) {
- if ((bit==0) && (byte & i)) {
- fprintf(stdout, "\n");
- }
- bit = ((byte & i) == i);
- fprintf(stdout, "%d", bit);
- }
- }
- }
- void pushBit(BUFFER* buffer, unsigned char bit)
- {
- buffer->data[buffer->byteSize-1] |= bit << (7-buffer->bitSize);
- buffer->bitSize++;
- if (buffer->bitSize == 8) {
- buffer->bitSize = 0;
- buffer->byteSize++;
- buffer->data = (char*)realloc(buffer->data, buffer->byteSize);
- buffer->data[buffer->byteSize-1] = 0;
- }
- }
- void appendBit(BUFFER* buffer, unsigned char bit, unsigned int usec, unsigned int freq)
- {
- unsigned int i;
- for(i=0; i<(usec * freq) / 1e6; i++) {
- pushBit(buffer, bit);
- }
- }
- void appendData(BUFFER* buffer, unsigned int type, unsigned int freq)
- {
- appendBit(buffer, 1, timings[type][0], freq);
- appendBit(buffer, 0, timings[type][1], freq);
- }
- void appendByte(BUFFER* buffer, unsigned char byte, unsigned int freq)
- {
- int i;
- for(i=0x80; i>0; i>>=1) {
- appendData(buffer, ((byte & i) == i), freq);
- }
- printf("%02X ", byte);
- }
- void pushCode(BUFFER* buffer, unsigned char* id, unsigned char section, unsigned char nb, unsigned char on, unsigned char global, unsigned int freq)
- {
- unsigned char byte6 = (id[6] & 0xf0) + numbering[on + (global ? 2 : 0)];
- unsigned char byte7 = (numbering[section] << 4) + numbering[nb];
- unsigned int i;
- if (global) {
- byte7 = 0x55;
- }
- appendData(buffer, START_OF_FRAME, freq);
- printf("SOF ");
- for(i=0; i<6; i++) {
- appendByte(buffer, id[i], freq);
- }
- appendByte(buffer, byte6, freq);
- appendByte(buffer, byte7, freq);
- appendData(buffer, END_OF_DATA, freq);
- printf("EOD ");
- }
|