initial version of FastShiftOut

This commit is contained in:
Rob Tillaart 2013-08-28 22:30:00 +02:00
parent fabe3ba99b
commit b99f202fac
3 changed files with 110 additions and 0 deletions

View File

@ -0,0 +1,59 @@
//
// FILE: FastShiftOut.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.1
// PURPOSE:
// URL:
//
// Released to the public domain
//
#include "FastShiftOut.h"
FastShiftOut::FastShiftOut(uint8_t datapin, uint8_t clockpin, uint8_t bitOrder)
{
_datapin = datapin;
_clockpin = clockpin;
_bitorder = bitOrder;
_value = -1;
pinMode(_datapin, OUTPUT);
pinMode(_clockpin, OUTPUT);
_datatimer = digitalPinToTimer(_datapin);
_databit = digitalPinToBitMask(_datapin);
_dataport = digitalPinToPort(_datapin);
// if (_datatimer != NOT_ON_TIMER) turnOffPWM(_datatimer);
_dataout = portOutputRegister(_dataport);
_clocktimer = digitalPinToTimer(_clockpin);
_clockbit = digitalPinToBitMask(_clockpin);
_clockport = digitalPinToPort(_clockpin);
// if (_clocktimer != NOT_ON_TIMER) turnOffPWM(_clocktimer);
_clockout = portOutputRegister(_clockport);
}
void FastShiftOut::write(uint8_t data)
{
_value = data;
for (uint8_t i = 0; i < 8; i++)
{
uint8_t v;
if (_bitorder == LSBFIRST) v = !!(_value & (1 << i));
else v = !!(_value & (1 << (7 - i)));
uint8_t oldSREG = SREG;
cli();
if (v == LOW) *_dataout &= ~_databit;
else *_dataout |= _databit;
*_clockout |= _clockbit;
*_clockout &= ~_clockbit;
SREG = oldSREG;
}
}
int FastShiftOut::read()
{
return _value;
}
// -- END OF FILE --

View File

@ -0,0 +1,48 @@
//
// FILE: FastShiftOut.h
// AUTHOR: Rob Tillaart
// VERSION: 0.1
// PURPOSE:
// URL:
//
// Released to the public domain
//
#ifndef FastShiftOut_h
#define FastShiftOut_h
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#define FASTSHIFTOUT_LIB_VERSION "0.1.00"
class FastShiftOut
{
public:
FastShiftOut(uint8_t, uint8_t, uint8_t);
void write(uint8_t);
int read(void);
private:
uint8_t _datapin;
uint8_t _clockpin;
uint8_t _bitorder;
int _value;
uint8_t _datatimer;
uint8_t _databit;
uint8_t _dataport;
volatile uint8_t *_dataout;
uint8_t _clocktimer;
uint8_t _clockbit;
uint8_t _clockport;
volatile uint8_t *_clockout;
};
#endif
// -- END OF FILE --

View File

@ -0,0 +1,3 @@
TODO
- examples