/*
max72.h - Arduino Library for MAX7219.
Copyright 2014 - Laurent Menu-Kerforn
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef max72_h
#define max72_h

#include "Arduino.h"

#define M72_NOOP 0x00;
#define M72_DIG1 0x01
#define M72_DIG2 0x02
#define M72_DIG3 0x03
#define M72_DIG4 0x04
#define M72_DIG5 0x05
#define M72_DIG6 0x06
#define M72_DIG7 0x07
#define M72_DIG8 0x08
#define M72_DECODEMODE 0x09
#define M72_INTENSITY 0x0a
#define M72_SCANLIMIT 0x0b
#define M72_SHUTDOWN 0x0c
#define M72_DISPLAYTEST 0x0f


class max72 {
public:
	max72(int loadpin,int clockpin,int datapin);
	max72(int nbmax,int loadpin,int clockpin,int datapin);
	void setup();
	void setup(int loadpin,int clockpin,int datapin);
	void setup(int nbmax,int loadpin,int clockpin,int datapin);
	// void setblock(byte [] v1,byte v2, byte v3,byte v4,byte v5,byte v6,byte v7,byte v8);
	void setblock(byte v1,byte v2, byte v3,byte v4,byte v5,byte v6,byte v7,byte v8);
	void setblock(byte []);
	//void setblock(byte v1,...);
	void sendcommand(byte com,byte parm);
	void sendMcommand(int max,byte com,byte parm);
	void setrow(int row,byte val);
	void setMblock(int max,byte* );
	void setMblock(int max,byte v1,byte v2, byte v3,byte v4,byte v5,byte v6,byte v7,byte v8);
	void setMrow(int max,int row,byte val);
	void setorientation(char o); //R: rotate right, L rotate left, U: upside Down, O: original, M : row mirror
	void intensity(byte val);
	void shutdown();
	void clear();
	void Mclear(int);
	void switchon();

protected:
	void varinit(int loadpin,int clockpin,int datapin);
	void varinit(int nbmax,int loadpin,int clockpin,int datapin);
	void sendbyte(byte b);
	void send2bytes(byte com,byte parm);
	byte *rotate(byte *in,byte *out);
	byte esrevni(byte);
	int loadpin;
	int nbmax;
	int clockpin;
	int datapin;
	char orientation;
};

#endif

