Esempio n. 1
0
//function for analog measure, return measured value
//255=4.7V; 0 = 0V
inline unsigned char Measure(unsigned char input_pin, unsigned char min, unsigned char max)
{
	//REFS1 REFS0 ADLAR - MUX3 MUX2 MUX1 MUX0
	ADMUX = 0x20 + input_pin;		
	
	//ADEN ADSC ADFR ADIF ADIE ADPS2 ADPS1 ADPS0
	ADCSRA = 0xC6;		//start conversion, divide clk 64
	
	while(readBit(ADCSRA, 6)); //wait until conversion is complete
	
	unsigned char measured = 0;
	
	if (ADCH<min)
	{
		measured=0;
	} 
	if(ADCH<max)
	{
		measured-=min;
	}
	else
	{
		measured=(max-min);		
	}
	
	return measured;	
}
Esempio n. 2
0
int main(int argc, const char* argv[]) {
  FILE* inf = fopen(argv[1], "rb");
  FILE* outf = fopen(argv[2], "wb");
  int8_t in[320*200];
  int8_t out[(320*200)/8];
  memset(out, 0, (320*200)/8);
  
  int8_t* loc = in;
  for(int i=0; i < 320*200; ++i) {
    int16_t sh;
    fread(&sh, 2, 1, inf);
    *loc = 0x03 & sh;
    loc+=1;
  }

  for(int i=0; i < 320*200; ++i) {
    c64WriteBit(out, i, readBit(in, i));
  }

  loc = out;
  for(int i=0; i < (320*200)/8; ++i) {
    fwrite(loc, 1, 1, outf);
    loc+=1;
  }
  
  fclose(inf);
  fclose(outf);
  return 0;
}
Esempio n. 3
0
unsigned ArithmeticDecoder::decode(DataModel* dataModel) {
	uint64_t range = intervalHigh - intervalLow + 1;
	auto scale = dataModel->getCumulativeFreq(dataModel->size() - 1);
	auto cumulativeFreq = ((value - intervalLow + 1) * scale - 1) / range;

	unsigned symbol = 0;
	// find i where cumuliveFreqs[i-1] < cumulativeFreq < cumulativeFreqs[i]
	for (size_t i = 0; i < dataModel->size(); ++i) {
		auto lowerBound = i != 0 ? dataModel->getCumulativeFreq(i - 1) : 0U;
		auto upperBound = dataModel->getCumulativeFreq(i);
		if (lowerBound <= cumulativeFreq && cumulativeFreq < upperBound) {
			symbol = i;
			break;
		}
	}

	// compute new interval bounds

	// interval upper bound is
	intervalHigh = intervalLow + (range * dataModel->getCumulativeFreq(symbol)) / scale - 1;

	// interval lower bound is computed as low + (r * cumFreq(i-1)) / s and cumFreq(-1) == 0 so
	// if symbol == 0 then interval lower bound is not modified 
	if (symbol != 0) {
		intervalLow += (range * dataModel->getCumulativeFreq(symbol - 1)) / scale;
	}

	// enlarge interval and get bits from data
	// loop ends when interval is large enough
	for (;;) {
		// interval is in lower half of possible range
		if (intervalHigh < IntervalTraitsType::HALF) {
			;	// do nothing
		// interval is in upper half of possible range
		} else if (intervalLow >= IntervalTraitsType::HALF) {
			intervalLow -= IntervalTraitsType::HALF;
			intervalHigh -= IntervalTraitsType::HALF;
			value -= IntervalTraitsType::HALF;
		// interval is in middle of possible range
		} else if (intervalLow >= IntervalTraitsType::QUARTER && intervalHigh <= IntervalTraitsType::THREE_QUARTERS) {
			intervalLow -= IntervalTraitsType::QUARTER;
			intervalHigh -= IntervalTraitsType::QUARTER;
			value -= IntervalTraitsType::QUARTER;
		// none of these cases so break loop
		} else
			break;

		intervalLow <<= 1;
		intervalHigh = (intervalHigh << 1) + 1;
		readBit();
	}

	// on adaptive data model we increase symbol frequency
	auto adaptiveModel = dynamic_cast<AdaptiveDataModel*>(dataModel);
	if (adaptiveModel != nullptr)
		adaptiveModel->incSymbolFreq(symbol);

	return symbol;
}
	void HeightEntry::writeBit(unsigned int b, bool v)
	{
		if(v)
		{
			if(!readBit(b))
			{
				height.bytes[b / 8] += 1 << (b % 8);
			}
		}
		else
		{
			if(readBit(b))
			{
				height.bytes[b / 8] -= 1 << (b % 8);
			}
		}
	}
Esempio n. 5
0
void sendByte(unsigned char sByte){
	unsigned char i=0;
	for(i=0;i<8;i++){
		writeBit(sByte>>(7-i));
	}
	writeBit(1); //MACK
	readBit(); //nosak
}
Esempio n. 6
0
/**
  * @brief  get tap axes reg to choose which axis using in tap detection.
  * @param  AXIS axis - axis want to get.
  * @return	true axis already use.
  */
bool ADXL345::getTapAxes(AXIS axis){
	bool state;
	switch (axis) {
		case AXIS_X:
			state = readBit(TAP_AXES_REG, 2);
			break;
		case AXIS_Y:
			state = readBit(TAP_AXES_REG, 1);
			break;
		case AXIS_Z:
			state = readBit(TAP_AXES_REG, 0);
			break;
		default:
			break;
	}
	return state;
}
Esempio n. 7
0
/**
  * @brief  read act inact control register to get which axes 
  *					using in inactivity detection
  * @param  adxl345_axis_t axis - axes want to read
  * @return true axis already using
  */
bool ADXL345::getInactControl(AXIS axis){
	bool state;
	switch (axis) {
		case AXIS_X:
			state = readBit(ACT_INACT_CTL_REG, 2);
			break;
		case AXIS_Y:
			state = readBit(ACT_INACT_CTL_REG, 1);
			break;
		case AXIS_Z:
			state = readBit(ACT_INACT_CTL_REG, 0);
			break;
		default:
			break;
	}
	return state;
}
Esempio n. 8
0
ArithmeticDecoder::ArithmeticDecoder(std::shared_ptr<BitStreamReader>& bsr) : bitStreamReader(bsr),
	intervalLow(0), intervalHigh(IntervalTraitsType::MAX)  {
		
	// read first IntervalTraitsType::BITS from data to value
	value = 0;
	for (size_t i = 0; i < IntervalTraitsType::BITS; ++i)
		readBit();
}
Esempio n. 9
0
uint8_t readByte(void){ 
	uint8_t i = 8, n = 0; 
	while(i--){ 
		//Shift one position right and store read value 
		n >>= 1; 
		n |= (readBit() << 7); 
	} 
	return n; 
} 
Esempio n. 10
0
/* Apply state changes to each GPIO */
void changeStateGPIO(gpio pins[], unsigned char data, int nbPins)
{
	int i;

	if (pins != NULL) {
		for (i = 0; i < nbPins; i++)
			writeValueToGPIO(readBit(data, i), pins[i].name);
	}
}
Esempio n. 11
0
void ArithmeticDecoder::reset() {
	intervalLow = 0;
	intervalHigh = IntervalTraitsType::MAX;

	// read first IntervalTraitsType::BITS from data to value
	value = 0;
	for (size_t i = 0; i < IntervalTraitsType::BITS; ++i)
		readBit();
}
Esempio n. 12
0
	LByte BitReader::readByte()
	{
		LByte rv = 0x00;
		for(int i = 0; i < 8; i++)
		{
			setBit(rv, i, readBit());
		}
		return rv;
	}
Esempio n. 13
0
void SoftI2C::write(uint8_t value)
{
    uint8_t mask = 0x80;
    while (mask != 0) {
        writeBit((value & mask) != 0);
        mask >>= 1;
    }
    if (readBit())  // 0: ACK, 1: NACK
        acked = false;
}
Esempio n. 14
0
uint32_t SWD::read(bool ap, int reg) {
	int ack = writeCommand(ap, 1, reg);
	if (ack == ACK_OK) {
		uint32_t value = 0;
		int parity = 0;
		for (unsigned int i = 0; i < 32; i++) {
			value >>= 1;
			int bit = readBit();
			value |= bit << 31;
			parity ^= bit;
		}
		if (parity == readBit()) {
			pinMode(SWDAT, OUTPUT);
			writeBit(0);
			errors = std::max(errors - 1, 0);
			return value;
		}
		std::cout << "value: " << std::hex << value << std::endl;
	} else if (ack == ACK_FAULT) {
Esempio n. 15
0
unsigned char Decoder::readByte()
{
    unsigned char c=0;
    for (unsigned short i = 0; i < 8; i++) {
        if (readBit()) {
            c |= (1 << (7 - i));
        }
    }
    return c;
}
	HeightArray HeightEntry::getHeightBits() const
	{
		HeightArray bits;

		for(unsigned int i = 0; i < HEIGHT_BITS; i++)
		{
			bits[i] = readBit(i);
		}

		return bits;
	}
Esempio n. 17
0
void test_readBit(void){

 { setPinLow_CMockExpect(49, 5); setPinHigh_CMockExpect(49, 5); getPin_CMockExpectAndReturn(49, 4, 1); };

 uint8_t bit;

 bit = readBit(4);

 UnityAssertEqualNumber((_U_SINT)((1)), (_U_SINT)((bit)), (((void *)0)), (_U_UINT)52, UNITY_DISPLAY_STYLE_INT);

}
Esempio n. 18
0
unsigned char lastRead(void){
	unsigned char i=0;
	unsigned char dummy = 0;
	for(i=0;i<8;i++){
		dummy |= readBit();
		dummy <<= 1;
	}
	writeBit(0); //NoMACK
	sak(); //sak
	
	return dummy;
}
void GammaCompressor::decompressUnsignedInt(ifstream * invertedFile, unsigned int * number, unsigned char * bitCode, int * currentBit)
{
    int numberSizeByBit = 1;
    while (readBit(invertedFile, bitCode, currentBit) == 1) {
        numberSizeByBit++;
    }
    if (numberSizeByBit == 1) {
        if (readBit(invertedFile, bitCode, currentBit) == 0) {
            *number = 0;
        }
        else {
            *number = 1;
        }
    }
    else {
        *number = 1;
        while (numberSizeByBit > 1) {
            *number = *number * 2 + readBit(invertedFile, bitCode, currentBit);
            numberSizeByBit--;
        }
    }
}
Esempio n. 20
0
unsigned char readByte(void){
	unsigned char i=0;
	unsigned char dummy = 0;
		
	for(i=0;i<8;i++){
		dummy |= readBit();
		dummy <<= 1;
	}
	writeBit(1); //MACK
	sak(); //sak
	
	return dummy;
}
Esempio n. 21
0
// Read BYTE Function
uint8_t OneWire::read(void)
{
	uint8_t byte = 0;
	uint8_t i;
	for (i = 0; i < 8; i++)
	{
		byte >>= 1;
		if (readBit())
		{
			byte |= 0x80;
		}
	}
	return byte;
}
Esempio n. 22
0
void Decoder::makeFile()
{
    Node *node = &huffmanTree->getRoot();
    while (!inFile.eof()) {
        if(readBit()){
            node = &node->right();
        }else{
            node = &node->left();
        }
        if (&node->left() == &HT::NIL and &node->right() == &HT::NIL) {
            outFile << static_cast<char>(node->key());
            node = &huffmanTree->getRoot();
        }
    }
}
Esempio n. 23
0
/** DS18B20 busy waiting.
 *
 * @return 0 if no timeout has occurred.
 */
int DS18B20::busyWait()
{
  unsigned int enterTime = mstimer_get();

  while ((!readBit()) && (mstimer_get() - enterTime < TEMP_CONVERSION_TIMEOUT));

  if ((mstimer_get() - enterTime) >= TEMP_CONVERSION_TIMEOUT)
  {
    return -1;
  }
  else
  {
    return 0;
  }
}
Esempio n. 24
0
//-----------------------------------------------------------------------------
// Read 1-Wire data byte and return it
//
int OWReadByte(void)
{
        int loop, result=0;

        for (loop = 0; loop < 8; loop++)
        {
                // shift the result to get it ready for the next bit
                result >>= 1;

                // if result is one, then set MS bit
                if (readBit())
                        result |= 0x80;
        }
        return result;
}
	std::string HeightEntry::toString() const
	{
		std::string str = "";

		str += coplanarTriangles ? '1' : '0';
		str += coplanarSquares ? '1' : '0';
		str += unused[0] ? '1' : '0';
		str += unused[1] ? '1' : '0';

		for(int i = HEIGHT_BITS - 1; i >= 0; i--)
		{
			str += readBit(i) ? '1' : '0';
		}

		return str;
	}
Esempio n. 26
0
uint8_t SoftI2C::read()
{
    uint8_t value = 0;
    for (uint8_t bit = 0; bit < 8; ++bit)
        value = (value << 1) | readBit();
    if (readCount > 1) {
        // More bytes left to read - send an ACK.
        writeBit(false);
        --readCount;
    } else {
        // Last byte - send the NACK and a stop condition.
        writeBit(true);
        stop();
        readCount = 0;
    }
    return value;
}
Esempio n. 27
0
			BitVectorInput(std::vector<std::string> const & rfilenames, uint64_t const offset)
			: filenames(rfilenames), nextfile(0), bitsleft(0), shift(-1), v(0)
			{
				std::pair<uint64_t,uint64_t> O = getOffset(filenames,offset);
				nextfile = O.first;
				
				if ( nextfile < filenames.size() )
				{
					std::string const fn = filenames[nextfile++];

					libmaus2::aio::InputStreamInstance::unique_ptr_type tistr(
						new libmaus2::aio::InputStreamInstance(fn)
					);
					istr = UNIQUE_PTR_MOVE(tistr);
							
					istr->seekg(-8,std::ios::end);
							
					libmaus2::aio::SynchronousGenericInput<uint64_t>::unique_ptr_type nSGI(
						new libmaus2::aio::SynchronousGenericInput<uint64_t>(*istr,1)
					);
					
					bool const ok = nSGI->getNext(bitsleft);
					assert ( ok );
					
					// number of complete words to skip
					uint64_t wordskip = O.second / 64;

					istr->clear();
					istr->seekg(8*wordskip,std::ios::beg);
					
					bitsleft -= 64*wordskip;
					O.second -= 64*wordskip;

					libmaus2::aio::SynchronousGenericInput<uint64_t>::unique_ptr_type tSGI(
						new libmaus2::aio::SynchronousGenericInput<uint64_t>(*istr,8*1024)
					);
					SGI = UNIQUE_PTR_MOVE(tSGI);
					
					// skip over rest of offset by reading the bits
					while ( O.second )
					{
						readBit();
						--O.second;
					}
				}
			}
void GammaCompressor::readAndDecompress(ifstream * inputInvertedFile, DocumentTerm ** postingList, int size)
{
    DocumentTerm *myPostingList = (DocumentTerm*)malloc(sizeof(DocumentTerm)*size);
    unsigned int delta = 0;
    unsigned int realNumber = 0;
    unsigned char bitCode = 0;
    int currentBit = 7;
    readBit(inputInvertedFile, &bitCode, &currentBit);
    for (int i = 0; i < size; i++) {
        DocumentTerm docTerm;
        decompressUnsignedInt(inputInvertedFile, &delta,&bitCode,&currentBit);
        realNumber += delta;
        docTerm.documentIndex = realNumber;
        decompressUnsignedInt(inputInvertedFile, &(docTerm.ftd), &bitCode, &currentBit);
        myPostingList[i] = docTerm;
    }
    *postingList = myPostingList;
}
Esempio n. 29
0
Decoder::Node& Decoder::readHeader()
{
        if (readBit())
        {
            Node *z = new Node(readByte(),0);
            z->setParent(HT::NIL).setLeft(HT::NIL).setRight(HT::NIL);
            return *z;
        }
        else
        {
            Node* leftChild = &readHeader();
            Node* rightChild = &readHeader();
            Node* z = new Node(0,0);
            z->setLeft(*leftChild);
            z->setRight(*rightChild);
            return *z;
        }
}
Esempio n. 30
0
/*!
    @brief Returns temperature read from BS18B20
 
    @note Reading is returned mutiplied by 10000 i.e. 
          for 25.8750 degrees the result will be 258750
*/
uint32_t ds18b20GetTemparature(){ 
	//Reset, skip ROM and start temperature conversion 
	if(reset()) {
        printf("DS18B20 is not responding%s", CFG_PRINTF_NEWLINE);
        return 0;
    }
	writeByte(CMD_SKIPROM); 
	writeByte(CMD_CONVERTTEMP); 
    	
    //Wait until conversion is complete 
    uint16_t timeout = 0xFFFF;
	while(!readBit() && timeout > 0) {
        timeout--;    
    }
    if(0 == timeout) {
        printf("BS18B20 temperature conversion has timed out%s", CFG_PRINTF_NEWLINE);
        return 0;
    }

	//Reset, skip ROM and send command to read Scratchpad 
	if(reset()) {
        printf("DS18B20 is not responding%s", CFG_PRINTF_NEWLINE);
        return 0;
    }
	writeByte(CMD_SKIPROM); 
	writeByte(CMD_RSCRATCHPAD); 
	
	uint8_t scratchpad[2] = {0, 0};
    //Read Scratchpad (only 2 first bytes) 
	scratchpad[0] = readByte(); 
	scratchpad[1] = readByte(); 
	
    //Store temperature integer digits and decimal digits 
	int8_t digit = scratchpad[0] >> 4; 
	digit |= (scratchpad[1] & 0x7) << 4; 
	//Store decimal digits 
	uint16_t decimal = scratchpad[0] & 0xf; 
	decimal *= DECIMAL_STEPS_12BIT; 
    
    return digit * 10000 + decimal;
}