terça-feira, 10 de junho de 2014

Código-fonte: mux.h

#ifndef MUX_H
#define MUX_H

#include <Arduino.h>

class MUX
{
private:
  unsigned A0, A1, A2;
  unsigned Z;
  unsigned Y;
  void initPins();
  static unsigned toBin(const unsigned);
  static unsigned id_counter;
  unsigned _id;
public:  
  MUX(unsigned, unsigned, unsigned, unsigned);
  unsigned readZ();
  unsigned getY();
  void shiftPorts();
  unsigned id();
};

unsigned MUX::id_counter = 0;

MUX::MUX(unsigned A0, unsigned A1, unsigned A2, unsigned Z)
{
  this->A0 = A0;
  this->A1 = A1;
  this->A2 = A2;
  this->Z  = Z;
  Y = 0;
  _id = ++id_counter;
  initPins();
}

unsigned MUX::id()
{
  return _id;
}

void MUX::initPins()
{
  pinMode(this->A0, OUTPUT);
  pinMode(this->A1, OUTPUT);
  pinMode(this->A2, OUTPUT);
  pinMode(this->Z, INPUT);
}

unsigned MUX::toBin(const unsigned number)
{
  int a = number, m = 1, res = 0;
  while (a > 0)
  {
    res += a % 2 * m;
    a /= 2;
    m *= 10;
  }
  return res;
}

unsigned MUX::readZ()
{
  return analogRead(Z);
}

unsigned MUX::getY()
{
  return Y;
}

void MUX::shiftPorts()
{
  Y = (Y + 1) % 8;
  int bin = toBin(Y);
  
  digitalWrite(this->A0, bin % 10);
  digitalWrite(this->A1, bin / 10 % 10);
  digitalWrite(this->A2, bin / 100);
}

#endif

Nenhum comentário:

Postar um comentário