/******************************************************************************
 Title:    Code for doing a light flashy thing with the 
                  led array from photo copiers
 Author:   rue_mohr
 Date:     Jan 8 2006
 Software: AVR-GCC 3.3 
 Hardware: attiny26
 
    
*******************************************************************************/
#include<avr/io.h>

#define Forward            1
#define Reverse            -1

#define OUTPUT             1
#define INPUT              0

// Bit positions
#define Strobe             7
#define Data               5
#define Clock              6

#define NumLeds            123

#define SetBit(BIT, PORT)     PORT |= (1<<BIT)
#define ClearBit(BIT, PORT)   PORT &= ~(1<<BIT)
#define IsHigh(BIT, PORT)    (PORT & (1<<BIT)) != 0
#define IsLow(BIT, PORT)     (PORT & (1<<BIT)) == 0
#define NOP()                 asm volatile ("nop"::)

#define StrobeH()    SetBit(Strobe, PORTA)
#define StrobeL()    ClearBit(Strobe, PORTA)
#define DataH()      SetBit(Data, PORTB)
#define DataL()      ClearBit(Data, PORTB)
#define ClockH()     SetBit(Clock, PORTB)
#define ClockL()     ClearBit(Clock, PORTB)
#define ClockPulse() ClockH(); NOP(); ClockL()

void Send (unsigned char bits);
void SetLights(unsigned char bits);
void Delay(unsigned int delay);


int main (void)  {
 
  char direction;
  unsigned char temp;
 
  DDRA = (INPUT << PA0 | INPUT << PA1 |INPUT << PA2 |INPUT << PA3 |INPUT << PA4 |INPUT << PA5 |INPUT << PA6 |OUTPUT << PA7);
  DDRB = (INPUT << PB0 | INPUT << PB1 | INPUT << PB2 | INPUT << PB3 | INPUT << PB4 |OUTPUT << PB5 |OUTPUT << PB6 |INPUT << PB7);
    
  temp = 0;
  direction = Forward;
  
  while(1) {                
    SetLights(temp); 
    temp += direction;    
    if ((temp == 0) | (temp == NumLeds))  { direction *= (-1); }                      
    Delay(300);
  } 
}
    
//------------------------| FUNCTIONS |------------------------

void Delay(unsigned int delay) {
  unsigned int x;
  for (x = delay; x != 0; x--) {
    NOP();
  }
}

void SetLights(unsigned char bits) {
  StrobeH();
  Send(bits);
  StrobeL();
}

void Send (unsigned char bits) {
  int temp;  
  for (temp = NumLeds; temp != 0; temp--) {
    DataL();
    if (temp == bits) {
      DataH();
    }
    ClockPulse();   
  }  
}

