uint8_t MG2639_Cell::begin(unsigned long baud)
{
	unsigned long setBaud = 0;
	uint8_t tries = 0;
	
	initializePins(); // Set up power and UART pin direction
	initializeUART(baud); // Initialize UART to requested baud
	
	// Try turning echo off first (send ATE0 command). If it succeeds,
	// we're already at the target baud and the module's on.
	if (setEcho(0) <= 0)
	{	
		// If setEcho fails, we can't communicate with the shield. The baud
		// may be incorrect, or the shield may be off. First time in, we'll
		// assume the module is on.
		// tries should be at least > 2 - one time assuming module is on,
		// another time assuming module is off
		while (setBaud <= 0 && tries < 4)
		{
			setBaud = autoBaud(); // Try to find the baud rate
			
			if (setBaud <= 0)
			{
				// If autoBaud fails to find anything, the module must be off.
				powerPulse(); // Send a power pulse (hold PWRKEY for 3s)
				delay(MODULE_WARM_UP_TIME); // Delay ~3s for module to warm up
			}
			tries++; // Increment tries and go again
		}
		
		// If we still can't find the baud rate, or communicate with the shield
		// we give up. Return a fail.
		if (setBaud <= 0)
			return 0;
		
		// If autoBaud succeeds, the module is on, we just need to 
		// change the baud rate.
		if (setBaud != baud)
		{
			int baudRsp;
	#if (ARDUINO >= 10601) 
			// Software serial after 1.6.1 works much better, no need for brute force
			baudRsp = changeBaud(setBaud, baud);
	#else
			baudRsp = bruteForceBaudChange(setBaud, baud, 100);
	#endif
			// Look for any error _except_ ERROR_UNKOWN_RESPONSE -- a change from 
			// 115200 will result in that error, even though the change baud 
			// worked. (SoftwareSerial can't read reliably at that high rate.)
			if ((baudRsp == 0) || (baudRsp == ERROR_FAIL_RESPONSE) || 
				(baudRsp == ERROR_TIMEOUT))
			{
				return 0;
			}
		}
		delay(COMMAND_RESPONSE_TIME);
	}
	
	return 1;
}
Beispiel #2
0
// Private
// Scan the keypad and report whether or not a key (or any key) has been pressed.
// 2011-12-23 - Removed from getKeyState() for readability and ease of maintenance.
boolean Keypad::scanKeys() {
	static unsigned int allKeys=0;
	byte curKey=0;
	boolean anyKey;

	// Assume that some other device is sharing the data pins used by the
	// keypad. If that is the case then the pins will need to be re-intialized
	// each time before they are used.
	initializePins();

	// I rewrote this method to provide a status change (anyKey OPEN/CLOSED) to the
	// getKeyState() function which handles debouncing. Now we can scan the keypad
	// without having to worry about huge debounce time delays.
	for( int c=0; c<size.columns; c++) {
		digitalWrite(columnPins[c], LOW);
		for( int r=0; r<size.rows; r++) {
			curKey = digitalRead(rowPins[r]);
			allKeys += curKey;
			if(curKey==0) currentKey = keymap[c+(r*size.columns)];

			// All keys have been scanned. Set 'anyKey' value for use by getKeyState().
			if( r==(size.rows-1) && c==(size.columns-1) ) {
				if( allKeys==(size.rows*size.columns) )
					anyKey = OPEN;
				else
					anyKey = CLOSED;
			}
		}
		digitalWrite(columnPins[c], HIGH);
	}
	allKeys = 0;
	return anyKey;		// Status tells if keys are OPEN or CLOSED.
}
Beispiel #3
0
// System initialization
void init() {
  // Initializes all pins to their default settings (see pins.c)
  initializePins();

  // Initialize system clocks
  cpu_init();

  // Start system tick (used for timing, delay, etc)
  SystemCoreClockUpdate();
  SysTick_Config(SystemCoreClock / 1000);
    
  // Tip from Sony:
  // Prevents hard-faults when booting from USB
  delay(50);
  
  // Tip from Sony:
  // Not quite sure, but I believe a pullup on DP enables charging of a device even if
  // it does not do USB any communication by removing the pre-enum current limit
  if(digitalRead(USB_CONNECTED)) pinMode(USB_DP, INPUT_PULLUP);
  
  // Initialize ADC
  adc_init();

  // Initialize I2C
  i2c_init();
}
Beispiel #4
0
/****************************************************************************
 Function
     StartComServiceSM

 Parameters
     ES_Event CurrentEvent

 Returns
     nothing

 Description
     Does any required initialization for this state machine
 Notes

 Author
     J. Edward Carryer, 02/06/12, 22:15
****************************************************************************/
void StartComServiceSM ( ES_Event CurrentEvent )
{	
	// if there is more than 1 state to the top level machine you will need 
  // to initialize the state variable
  CurrentState = WAITING;
	initializePins();
	initGameStatusLED();
	initializeKartStatus();
  printf("We are Kart %u\r\n", our_kart.kartNumber);
	
  // now we need to let the Run function init the lower level state machines
  // use LocalEvent to keep the compiler from complaining about unused var
  RunComServiceSM(CurrentEvent);
  return;
}
Beispiel #5
0
// <<constructor>> Allows custom keymap, pin configuration, and keypad sizes.
Keypad::Keypad(char *userKeymap, byte *row, byte *col, byte numRows, byte numCols) {
    rowPins = row;
    columnPins = col;
    size.rows = numRows;
    size.columns = numCols;

    begin(userKeymap);

    setDebounceTime(2);
    setHoldTime(500);
	keypadEventListener = 0;

    transitionTo(IDLE);
    stateChanged = false;

    initializePins();
}
// <<constructor>> Allows custom keymap. pin configuration and keypad size
KeypadSimple::KeypadSimple(char *userKeymap, byte *keyPins, byte keys)
{
  Pins = keyPins;

	size.keys = keys;

  begin(userKeymap);

	lastUpdate = 0;
	debounceTime = 50;
	holdTime = 1000;
	keypadEventListener = 0;
	currentKey = NO_KEY;
	state = IDLE;

	initializePins();
}
Beispiel #7
0
// <<constructor>> Allows custom keymap. pin configuration and keypad size
Keypad::Keypad(char *userKeymap, byte *row, byte *col, byte rows, byte cols, byte allowRollKeys)
{
    rowPins = row;
    columnPins = col;
	
	size.rows = rows;
	size.columns = cols;
	
    begin(userKeymap);
	
	lastUpdate = 0;
	debounceTime = 50;
	holdTime = 1000;
	keypadEventListener = 0;
	currentKey = NO_KEY;
	state = IDLE;
	allowRollingKeys = allowRollKeys; // 0 will not pass the key more than once during HOLD.
                                      // 1 will pass the key for as long as it is held.
	initializePins();
}
Beispiel #8
0
/*
|| @constructor
|| | Initialize the Keypad
|| #
||
|| @parameter userKeymap  a user specified key map of the keypad
|| @parameter row         an array of pins that are connected to the row of the keymap
|| @parameter col         an array of pins that are connected to the coloumn of the keymap
|| @parameter rows        the number of rows (the length og the row array)
|| @parameter cols        the number of coloumns (the length og the col array)
*/
Keypad::Keypad(char *userKeymap, byte *row, byte *col, byte rows, byte cols)
{
  rowPins = row;
  columnPins = col;

  size.rows = rows;
  size.columns = cols;

  begin(userKeymap);

  lastUpdate = 0;
  lastPress = 0;
  multiPressTime = 300;
  debounceTime = 50;
  holdTime = 1000;
  keypadEventListener = 0;
  currentKey = NO_KEY;
  lastKey = NO_KEY;
  state = IDLE;
  pressCount = 0;

  initializePins();
}
Beispiel #9
0
void KeyPadInit(char *userKeymap, uint8_t *row, uint8_t *col, uint8_t rows, uint8_t cols)
{
	rowPins = row;
	columnPins = col;

	size.rows = rows;
	size.columns = cols;

	KeyPadBegin(userKeymap);

	lastUpdate = 0;
	debounceTime = 50;
	//debounceTime = 5;
	holdTime = 500;
	//holdTime = 100;

	keypadEventListener = 0;
	currentKey = NO_KEY;
	state = IDLE;

	initializePins();

}