mirror of
https://github.com/felis/USB_Host_Shield_2.0.git
synced 2024-03-22 11:31:26 +01:00
Adds UHS2_GPIO class providing simple access to GPIO ports.
This commit is contained in:
parent
f142f80a30
commit
401f4991e9
5 changed files with 237 additions and 0 deletions
74
UHS2_gpio.cpp
Normal file
74
UHS2_gpio.cpp
Normal file
|
@ -0,0 +1,74 @@
|
|||
/* Copyright (C) 2011 Circuits At Home, LTD. All rights reserved.
|
||||
|
||||
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 2 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, write to the Free Software
|
||||
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
|
||||
Contact information
|
||||
-------------------
|
||||
|
||||
Circuits At Home, LTD
|
||||
Web : http://www.circuitsathome.com
|
||||
e-mail : support@circuitsathome.com
|
||||
|
||||
UHS2_GPIO implements "wiring" style GPIO access. Implemented by Brian Walton brian@riban.co.uk
|
||||
*/
|
||||
|
||||
#include "UHS2_gpio.h"
|
||||
|
||||
/** @brief Implement an instance of a UHS2_GPIO object
|
||||
* @param pUSB Pointer to a UHS2 USB object
|
||||
*/
|
||||
UHS2_GPIO::UHS2_GPIO(USB *pUsb) : m_pUsb(pUsb)
|
||||
{
|
||||
}
|
||||
|
||||
/** @brief Set a GPIO output value
|
||||
* @param pin GPIO output pin on USB Host Shield to set
|
||||
* @param val Value to set the pin to (zero value will clear output, non-zero value will assert output)
|
||||
*/
|
||||
void UHS2_GPIO::digitalWrite(uint8_t pin, uint8_t val) {
|
||||
if(pin > 7)
|
||||
return;
|
||||
uint8_t nValue = m_pUsb->gpioRdOutput();
|
||||
uint8_t nMask = 1 << pin;
|
||||
nValue &= (~nMask);
|
||||
if(val)
|
||||
nValue |= (nMask);
|
||||
m_pUsb->gpioWr(nValue);
|
||||
}
|
||||
|
||||
/** @brief Read the value from a GPIO input pin
|
||||
* @param pin GPIO input pin on USB Host Shield to read
|
||||
* @retval int Value of GPIO input (-1 on fail)
|
||||
*/
|
||||
int UHS2_GPIO::digitalRead(uint8_t pin) {
|
||||
if(pin > 7)
|
||||
return -1;
|
||||
uint8_t nMask = 1 << pin;
|
||||
uint8_t nValue = m_pUsb->gpioRd();
|
||||
return ((nValue & nMask)?1:0);
|
||||
}
|
||||
|
||||
/** @brief Read the value from a GPIO output pin
|
||||
* @param pin GPIO output pin on USB Host Shield to read
|
||||
* @retval int Value of GPIO output (-1 on fail)
|
||||
* @note Value of MAX3421E output register, i.e. what the device has been set to, not the physical value on the pin
|
||||
*/
|
||||
int UHS2_GPIO::digitalReadOutput(uint8_t pin) {
|
||||
if(pin > 7)
|
||||
return -1;
|
||||
uint8_t nMask = 1 << pin;
|
||||
uint8_t nValue = m_pUsb->gpioRdOutput();
|
||||
return ((nValue & nMask)?1:0);
|
||||
}
|
45
UHS2_gpio.h
Normal file
45
UHS2_gpio.h
Normal file
|
@ -0,0 +1,45 @@
|
|||
/* Copyright (C) 2011 Circuits At Home, LTD. All rights reserved.
|
||||
|
||||
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 2 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, write to the Free Software
|
||||
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
|
||||
Contact information
|
||||
-------------------
|
||||
|
||||
Circuits At Home, LTD
|
||||
Web : http://www.circuitsathome.com
|
||||
e-mail : support@circuitsathome.com
|
||||
|
||||
UHS2_GPIO implements "wiring" style GPIO access. Implemented by Brian Walton brian@riban.co.uk
|
||||
*/
|
||||
|
||||
#if !defined(__USB2_GPIO_H__)
|
||||
#define __USB2_GPIO_H__
|
||||
|
||||
#include "Usb.h"
|
||||
|
||||
class UHS2_GPIO
|
||||
{
|
||||
public:
|
||||
UHS2_GPIO(USB *pUsb);
|
||||
|
||||
void digitalWrite(uint8_t pin, uint8_t val);
|
||||
int digitalRead(uint8_t pin);
|
||||
int digitalReadOutput(uint8_t pin);
|
||||
|
||||
private:
|
||||
USB* m_pUsb;
|
||||
};
|
||||
|
||||
#endif // __USB2_GPIO_H__
|
40
examples/GPIO/Blink/Blink.ino
Normal file
40
examples/GPIO/Blink/Blink.ino
Normal file
|
@ -0,0 +1,40 @@
|
|||
/* Example of reading and writing USB Host Shield GPI output
|
||||
This example uses "Wiring" style interface. See Blink_LowLevel for example of using low-level UHS interface
|
||||
Author: Brian Walton (brian@riban.co.uk)
|
||||
*/
|
||||
#include <UHS2_gpio.h>
|
||||
|
||||
// Satisfy the IDE, which needs to see the include statment in the ino too.
|
||||
#ifdef dobogusinclude
|
||||
#include <spi4teensy3.h>
|
||||
#endif
|
||||
|
||||
#define OUTPUT_PIN 0
|
||||
|
||||
USB Usb; //Create an UHS2 interface object
|
||||
UHS2_GPIO GPIO(&Usb); //Create a GPIO object
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin( 115200 );
|
||||
#if !defined(__MIPSEL__)
|
||||
while (!Serial); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection
|
||||
#endif
|
||||
Serial.println("Start");
|
||||
|
||||
if (Usb.Init() == -1)
|
||||
Serial.println("OSC did not start.");
|
||||
|
||||
delay( 200 );
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
//Get the current output value, toggle then wait half a second
|
||||
int nValue = GPIO.digitalReadOutput(OUTPUT_PIN);
|
||||
nValue = (nValue?0:1);
|
||||
GPIO.digitalWrite(OUTPUT_PIN, nValue);
|
||||
Serial.print(nValue?"+":"."); //Debug to show what the output should be doing
|
||||
delay(500);
|
||||
}
|
||||
|
40
examples/GPIO/Blink_LowLevel/Blink_LowLevel.ino
Normal file
40
examples/GPIO/Blink_LowLevel/Blink_LowLevel.ino
Normal file
|
@ -0,0 +1,40 @@
|
|||
/* Example of reading and writing USB Host Shield GPI output using low-level functions
|
||||
This example uses low-level UHS interface. See Blink for example of using "Wiring" style interface
|
||||
Author: Brian Walton (brian@riban.co.uk)
|
||||
*/
|
||||
#include <Usb.h>
|
||||
|
||||
// Satisfy the IDE, which needs to see the include statment in the ino too.
|
||||
#ifdef dobogusinclude
|
||||
#include <spi4teensy3.h>
|
||||
#endif
|
||||
#include <SPI.h>
|
||||
|
||||
USB Usb;
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin( 115200 );
|
||||
#if !defined(__MIPSEL__)
|
||||
while (!Serial); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection
|
||||
#endif
|
||||
Serial.println("Start");
|
||||
|
||||
if (Usb.Init() == -1)
|
||||
Serial.println("OSC did not start.");
|
||||
|
||||
delay( 200 );
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
//Get the current output value, toggle then wait half a second
|
||||
uint8_t nGPO = Usb.gpioRdOutput();
|
||||
uint8_t nValue = ((nGPO & 0x01) == 0x01)?0:1;
|
||||
nGPO &= 0xFE; //clear bit 0
|
||||
nGPO |= nValue;
|
||||
Usb.gpioWr(nGPO);
|
||||
Serial.print(nValue?"+":"."); //Debug to show what the output should be doing
|
||||
delay(500);
|
||||
}
|
||||
|
38
examples/GPIO/Input/Input.ino
Normal file
38
examples/GPIO/Input/Input.ino
Normal file
|
@ -0,0 +1,38 @@
|
|||
/* Example of reading USB Host Shield GPI input
|
||||
Author: Brian Walton (brian@riban.co.uk)
|
||||
*/
|
||||
#include <UHS2_gpio.h>
|
||||
|
||||
// Satisfy the IDE, which needs to see the include statment in the ino too.
|
||||
#ifdef dobogusinclude
|
||||
#include <spi4teensy3.h>
|
||||
#endif
|
||||
|
||||
#define INPUT_PIN 0
|
||||
#define OUTPUT_PIN 0
|
||||
|
||||
USB Usb; //Create an UHS2 interface object
|
||||
UHS2_GPIO GPIO(&Usb); //Create a GPIO object
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin( 115200 );
|
||||
#if !defined(__MIPSEL__)
|
||||
while (!Serial); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection
|
||||
#endif
|
||||
Serial.println("Start");
|
||||
|
||||
if (Usb.Init() == -1)
|
||||
Serial.println("OSC did not start.");
|
||||
|
||||
delay( 200 );
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
//Get the value of input, set value of output
|
||||
int nValue = GPIO.digitalRead(INPUT_PIN);
|
||||
nValue = (nValue?0:1);
|
||||
GPIO.digitalWrite(OUTPUT_PIN, nValue);
|
||||
}
|
||||
|
Loading…
Reference in a new issue