-
Notifications
You must be signed in to change notification settings - Fork 1
/
USB_Host_Shield_GPIO.cpp
81 lines (70 loc) · 2.32 KB
/
USB_Host_Shield_GPIO.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/**
* @mainpage USB Host Shield GPIO
*
* @brief An Arduino library, that allows you to access the GPIO pins fo the Max IC used by [USB Host Shield](http://www.circuitsathome.com/products-page/arduino-shields/usb-host-shield-2-0-for-arduino).
*
* @note Requires v2.0 of USB Host Shield Library available at https://github.com/felis/USB_Host_Shield_2.0
*
* @author Sudar <http://sudarmuthu.com> <http://hardwarefun.com>
* @version 1.0.0
* @copyright BEERWARE
* @date 2012-10-26
* http://hardwarefun.com/projects/usb-host-shield-gpio
*/
/*
* Copyright 2011 Sudar Muthu (http://sudarmuthu.com)
* ----------------------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42):
* <sudar@sudarmuthu.com> wrote this file. As long as you retain this notice you
* can do whatever you want with this stuff. If we meet some day, and you think
* this stuff is worth it, you can buy me a beer or coffee in return - Sudar
* ----------------------------------------------------------------------------
*/
#include "USB_Host_Shield_GPIO.h"
MaxGPIO::MaxGPIO() {
Init();
}
/**
* Write is performed by calling the regWr function of the USB class
*
* @todo Find a better way to check if pin is 0<= pin <= 7
*/
void MaxGPIO::write(byte pin, byte val) {
//TODO: Find a better way to do this comparison
if (pin < 8) {
// process only if the pin is less than 8
uint8_t bit = pinToBitMask(pin);
uint8_t reg = pinToRegister(pin);
uint8_t out = regRd(reg);
if (val == LOW) {
out &= ~bit;
} else {
out |= bit;
}
regWr(reg, out);
} else {
//Serial.print(pin);Serial.println(" not a pin");
}
}
/**
* Read is performed by calling the regRd function of the USB class
*
* @todo Find a better way to check if pin is 0<= pin <= 7
*/
byte MaxGPIO::read(byte pin) {
//TODO: Find a better way to do this comparison
if (pin < 8) {
// process only if the pin is less than 8
uint8_t bit = pinToBitMask(pin);
uint8_t reg = pinToRegister(pin);
uint8_t out = regRd(reg);
if (out & bit) {
return HIGH;
} else {
return LOW;
}
} else {
//Serial.print(pin);Serial.println(" not a pin");
return LOW;
}
}