Wednesday 23 May 2012

Issue with Wifly and LCD Screen

Problem

The issue is with the Sparkfun Wifly (http://www.sparkfun.com/products/9954) and Sparkfun LCD screen (http://www.sparkfun.com/products/9363), I was using the Wifly Library and ColorLCDShield Library.
When you want to connect everything together you will not be able to communicate with the LCD after you initialized the Wifly, that because for some reason the SPI bus still communicate with the Wifi.

Solution

The fix is quiet simple, you need to modify 6 files of the Wifly Library:

_Spi.h
//Add two new methods
void end();
void restore();

_Spi.cpp
//That will disable the SPI
void SpiDevice::end() {
SPCR = 01010000;
}

void SpiDevice::restore() {
_initSpi();
}

SpiUart.h
//Add two new methods
void end();
void restore();

SpiUart.cpp
void SpiUartDevice::end() {
SpiDevice::end();
}

void SpiUartDevice::restore() {
SpiDevice::restore();
}

WiFlyDevice.h
//Add two new methods
void end();
void restore();

WiFlyDevice.cpp
void WiFlyDevice::end() {
uart.end();
}
void WiFlyDevice::restore() {
uart.restore();
}

Example

//Start the connection with the Wifly
WiFly.begin();
  
  if (!WiFly.join(ssid, passphrase)) {
    Serial.println("Association failed.");
    while (1) {
      // Hang on failure.
    }
  }  

//Stop for a moment the connection between the SPI and Wifly
WiFly.end();

//Do something with the LCD
lcd.init(PHILLIPS);    //Initialize the LCD
lcd.contrast(40);   // Sets LCD contrast, adjust this if your contrast is odd looking.
lcd.clear(WHITE);    // Clear LCD to solid white

//Restore the communication with the Wifly
WiFly.restore();

So, every time that you want to communicate with the LCD screen, you need to end the SPI communication and leave that to the LCD screen.