/*
  schip_analog.h - header for interrupt driven analog input
  					with multiple options to be defined

  Copyright (c) 2015 M.Schippling

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library 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
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General
  Public License along with this library; if not, write to the
  Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  Boston, MA  02111-1307  USA

  $Id: schip_analog.h,v 1.1 2015/10/28 19:51:26 schip Exp $
*/

// note: can detect if this library used with SCHIP_ANALOG_H
#ifndef SCHIP_ANALOG_H
#define SCHIP_ANALOG_H

#include "Arduino.h"

/*** optional options ***/

// include the schip scheduler, or not, to use schip's task scheduler
//  to post ADCTask() when SCHIPAVG defined, otherwise it does nothing
// note that the file contains a switch to enable a special
//  cores scheduler version that you probably will never use...
#include "schip_scheduler.h" // go to the library version for advice...

//#define SCHIPAVG	// to do schip's averaging of readings

#define SCHIPBUFFER	// to include schip's buffering in interrupt
//  note: the timing may be off when combined with SCHIPAVG, YMMV
#define SBUFSIZE	64	// number of samples to buffer before reporting

// These are for testing the timing of stuff, not production code
// #define SCHIP_ISRLED	    // toggle LED p13 for ADC ISR method length
// #define SCHIP_ADC0PERIOD // toggle LED p13 ADC0 ISR cycle period

// number of ADCs to convert...
//  in order to play nice with I2C on pins shared with ADC4,5
//  this will skip channels 4&5 when SCHIPSKIPI2C is defined
//  so only 6 conversions are done on each cycle through the
//  number can be set smaller (probably 4) as needed...
#define NUM_ADCCHANNEL 4	// convenient UNO minimum
//#define NUM_ADCCHANNEL 8	// maximum for atmega 328
#define SCHIPSKIPI2C 		// Skip ADC4,5 for I2C to use instead

#ifdef __cplusplus
extern "C"{
#endif

// externals for this library
extern int analogReadMS( byte pin );  // the 'regular' immediate value ADC
#define analogRead analogReadMS	      // replaces analogRead()
extern byte analogReadAvg( byte pin );	// averaged value access
extern word *SchipBUF0;	// semaphore from ADC for interrupt buffering
extern void ADCTask( uint16_t numADC );	// task posted from ADC averaging

// and... a bonus utility to return the number of bytes of free RAM
extern uint16_t freeRam(void);

#ifdef __cplusplus
} // extern "C"
#endif

#endif // SCHIP_ANALOG_H
