/* ADCTask.cpp -- main Sensor loop task template for testing
 *
 * Available Sensors:
 *  Analog inputs
 *	 A0 --
 *	 A1 --
 *	 A2 --
 *	 A3 --
 *	 A4 -- (note: overlaps I2C so may not be used)
 *	 A5 -- (note: overlaps I2C so may not be used)
 *	 A6 --
 *	 A7 --
 *
 *  I2C
 *    Accelerometer -- X, Y, Z acceleration
 *
 *  Digital inputs
 *	 D0 -- serial RX
 *	 D1 -- serial TX
 *	 D2  -- D2 (ext conn) [extINT]
 *   D3  -- D3 (ext conn) or Transw-A-Q5 [Tmr2pwm]}
 *   D4  -- D4 (ext conn)
 *   D5  -- D5 (ext conn) or Transw-B-Q6 [Tmr0pwm]}
 *   D6  -- D6 (ext conn) or Transw-C-Q7 [Tmr0pwm]}
 *   D7  -- D7 (ext conn)
 *   D8  -- D8 (ext conn) or L298b direction on HBSWboard
 *   D9  -- L298a En/PWM [Tmr1pwm]
 *   D10 -- L298b En/PWM [Tmr1pwm]
 *   D11 -- L298b direction or Transw-D-Q8 [Tmr2pwm] on HBSWboard
 *   D12 -- L298a direction
 *
 *  Digital outputs
 *	 D13 -- onboard LED
 *
 */
 
// the things we use
// Get the prototypes for the schip-scheduler
//   accessed from default cores/arduino directory)
#include "schip/scheduler.h"
// this library's external defines
#include "L298.h"
// global objects from other libraries
#include <Plot.h>	// used by everybody in my world
Plot plot;	// should be instantiated in the main.ino file eventually

#define SENDTEXT	// send text values for all sensor input
//#define SENDPLOT	// send SimPlot values, uncomment what we want below
//#define SENDGRAPH	// send Android BlueTooth values, uncomment what below

// sample defines for pins and input indices
// Input pin assignments
// ADC, also input array index for each value
#define iADC0		0		// ADC 0
#define iADC1		1		// ADC 1
#define iADC2		2		// ADC 2
#define iADC3		3		// ADC 3
#define iADC4		4		// ADC 4 -- or I2C
#define iADC5		5		// ADC 5 -- or I2C
#define iADC6		6		// ADC 6
#define iADC7		7		// ADC 7
//
// Digital input pin numbers, note -- NOT the input index
#define Pin2	2
#define Pin3	3
#define Pin4	4
#define Pin5	5
#define Pin6	6
#define Pin7	7
#define Pin8	8

/** Task posted when ADC averaging gets a new set of values.
 **  Posted after every 64 conversions of all channels
 **    @110us/channel ~= 43ms or 23 times a second when 6 channels are used
 **  Use analogReadAvg( channel ); to get 8 bit averaged results.
 ** @param numADC -- number of channels available -- required but ignored
 **/
byte sCnt;	// util sensor cycle counter, will wrap every 256 sample cycles
void ADCTask( uint16_t numADC )
{
	int values[8];
	byte i;
	word tmp = 0;
#ifdef LOGTIME
	unsigned long mytime = millis();
#endif // LOGTIME

	++sCnt; // global running count of samples we have seen

	// alternate LED on/off
	digitalWrite( LED, sCnt&0x01 );

	// read all eight averaged ADC results
	//  will probably ignore channels 4 & 5 due to I2C use
	//  this is controlled by defines in cores/arduino/wiring_analog.c
	for( i=0; i<8; ++i )
	{
		// read the averaged value for each input:
		values[i] = analogReadAvg( i );
	}

	// read digital pins #2-#8
	//  if this is HBSWboard 3,5,6,8 will be transw or L298 output values...
	for( i=0; i<7; ++i )
	{
		tmp |= (digitalRead( i+2 ) << i);
	}

	// only log every 64th sample set
	// if( sCnt % 64 ) return;

#ifdef LOGTIME	// log millisec that this all took to run
	tmp = (int)(millis() - mytime);
#elif defined LOGRAM	// log free memory
	tmp = freeRam();
#endif // not LOGTIME

// else log digital inputs as set above

#ifdef SENDGRAPH // send Android graphing values
	plot.graph( values[iADC0], values[iADC1], values[iADC2], tmp );
#endif // SENDPLOT

#ifdef SENDPLOT // send SimPlot values
	// first 4 ADC values
	plot.plot( values[iADC0], values[iADC1], values[iADC2], values[iADC3] );
#endif // SENDPLOT

#ifdef SENDTEXT	// send text values for everything
	// ADC values 
	plot.textn( values[iADC0], values[iADC1], values[iADC2], values[iADC3] );
	// ADC values 
	plot.textn( values[iADC4], values[iADC5], values[iADC6], values[iADC7] );
	// temp value whatever it is...
	plot.text( 0, 0, 0, tmp );
#endif // SENDTEXT

	return;
}
 
/** initialize the whole input thing
 **/
void ADCTask_init()
{
	// LED is initialized in L298/Transw code, but do it here just in case
	pinMode( LED, OUTPUT );

	// Serial comm should be set by plot.init() in main
	// // everyone uses comms -- default to 9600 baud for Bluetooth adapter
	plot.init(DEFBAUD);

	// example set input mode on all available digital pins
//	pinMode( Pin2, INPUT_PULLUP ); // example to turn on internal pullup
	pinMode( Pin2, INPUT );
	pinMode( Pin4, INPUT );
	pinMode( Pin7, INPUT );
#ifndef HBSWboard
	pinMode( Pin3, INPUT );
	pinMode( Pin5, INPUT );
	pinMode( Pin6, INPUT );
	pinMode( Pin8, INPUT );
#endif

	sCnt = 0;				// sample counter

	// required to start the ADC interrupt cycle
	// which will eventually repeatedly post ADCTask()
	analogRead( 0 );

	return;
}
