Initialized repo

Initialized with read me, json, license, firmware, and examples
This commit is contained in:
Travis Elliott 2015-09-03 11:27:48 -05:00
commit a4c4009a2d
7 changed files with 802 additions and 0 deletions

15
LICENSE Normal file
View file

@ -0,0 +1,15 @@
This Library was developed by National Control Devices LLC for use with 16 channel relay
Copyright (C) <2015> <Travis Elliott>
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 3 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, see <http://www.gnu.org/licenses/>.

172
README.md Normal file
View file

@ -0,0 +1,172 @@
# About
This Library is intended for use with NCD 8 Relay Particle Core/Photon compatible relay controllers.
The intention of this library is to make use of the NCD 8 channel relay controller with Particle development web IDE as simple as possible for users.
###Developer information
NCD has been designing and manufacturing computer control products since 1995. We have specialized in hardware design and manufacturing of Relay controllers for 20 years. We pride ourselves as being the industry leader of computer control relay products. Our products are proven reliable and we are very excited to support Particle. For more information on NCD please visit www.controlanything.com
###Requirements
- NCD 8 Channel Particle Core/Photon Compatible Relay board
- Particle Core/Photon module
- Knowledge base for developing and programming with Particle Core/Photon modules.
### Version
1.0.0
### How to use this library
The libary must be imported into your application. This can be done through the Particle WEB IDE by selecting Libraries, then select the NCD8Relay. Click Include in App button. Select the App you want to include the library in. Finally click Add to this app. For more information see [Particles's documentation] [sparkIncludeLibrary]
### Example use
Once the Library is included in your applicaiton you should see an include statement at the top like this:
```cpp
//This #include statement was automatically added by the Particle IDE.
#include "NCD8Relay/NCD8Relay.h"
```
Now you need to instanciate an object of the library for use in your application like this:
```cpp
NCD8Relay relayController;
```
Here is an example use case for the class
```cpp
// This #include statement was automatically added by the Particle IDE.
#include "NCD8Relay/NCD8Relay.h"
NCD8Relay relayController;
void setup() {
Serial.begin(115200);
relayController.setAddress(0, 0, 0);
}
void loop() {
//Test relays
for(int i = 1; i < 9; i++){
relayController.turnOnRelay(i);
int rOnStatus = relayController.readRelayStatus(i);
Serial.print("Relay status after on: ");
Serial.println(rOnStatus);
delay(500);
}
for(int i = 1; i < 9; i++){
relayController.turnOffRelay(i);
int rOffStatus = relayController.readRelayStatus(i);
Serial.print("Relay status after off: ");
Serial.println(rOffStatus);
delay(500);
}
relayController.turnOnAllRelays();
delay(500);
relayController.turnOffAllRelays();
delay(500);
for(int i = 1; i < 9; i++){
relayController.toggleRelay(i);
delay(500);
relayController.toggleRelay(i);
delay(500);
}
relayController.setBankStatus(85);
int rStatus85 = relayController.readRelayBankStatus();
Serial.print("Status after setting relay bank status to 85: ");
Serial.println(rStatus85);
delay(500);
relayController.setBankStatus(170);
delay(500);
relayController.turnOffAllRelays();
delay(500);
}
```
###Public accessible methods
```cpp
void setAddress(int a0, int a1, int a2);
```
>Must be called first before using the object. This method should also be called any time communication with
>the controller is lost or broken to recover communication This method accepts two int arguments. This
>tells the Library what address to direct commands to. a0 and a1 ints are representations of the two
>jumpers on the 8 channel relay controller which are labeled on the board A0, A1, and A2. If the jumper is
>installed then that int in this call should be set to 1. If it is not installed then the int should be set to
So if I have A0, A1, and A2 installed I would call ```relayController.setAddress(1, 1, 1).```
```cpp
void turnOnRelay(int Relay);
```
>This method accepts one int argument. Valid int arguments 1-8. A call to this method will turn on the
>relay indicated by the passed int argument.
```cpp
void turnOffRelay(int Relay);
```
>This method accepts one int argument. Valid int arguments 1-8. A call to this method will turn off the relay
>indicated by the passed int argument.
```cpp
void turnOnAllRelays();
```
>This method does not accept any arguments. A call to this method will turn on all 8 relays on the
>controller.
```cpp
void turnOffAllRelays();
```
>This method does not accept any arguments. A call to this method will turn off all 8 relays on the
>controller.
```cpp
void toggleRelay(int relay);
```
>This method accepts one int argument. Valid int arguments are 1-8. A call to this method will toggle the
>status of the relay indicated by the passed int argument. If the relay was previously off before the method
>call the relay will turn on and vice versa.
```cpp
void setBankStatus(int status);
```
>This method accepts two int arguments. Valid status int arguments 0-255. A call
>to this method will set the status of all relays on the board to the status byte passed in
the argument(status). Each relay on the board(total of 8) are represented as bits in the status
>argument.
```cpp
int readRelayStatus(int relay);
```
>This method accepts one int argument and returns one int. Valid relay int arguments 1-8. A call to this
>method will read the status of the given relay passed by the relay argument and return the current on/off
>status of the given relay. 1 will be returned if the relay is on and 0 will be returned if the relay is off.
>256 will be returned if an error has occured(generally due to lack of communication with the controller).
```cpp
int readRelayBankStatus();
```
>This method accepts no arguments and returns one int. A call to this
>method will read and return the status of all relays on the board.
>Each relay in the bank is represented as a bit in the returned int. Valid returns are 0-255. 256 will be
>returned if an error has occured(generally due to lack of communciation with controller).
###Public accessible variables
```cpp
bool initialized;
```
>This boolean indicates the current status of the interface connection to the controller. This variable should
>be checked often throughout your application. If communication to the board is lost for any reason this
>boolean variable will return false. If all is well it will return true.
License
----
GNU
[sparkIncludeLibrary]:https://docs.particle.io/guide/getting-started/build/photon/

53
examples/application.cpp Normal file
View file

@ -0,0 +1,53 @@
/* Includes ------------------------------------------------------------------*/
#include "application.h"
SYSTEM_MODE(MANUAL);
NCD8Relay relayController;
/* This function is called once at start up ----------------------------------*/
void setup()
{
Serial.begin(115200);
relayController.setAddress(0,0,0);
}
/* This function loops forever --------------------------------------------*/
void loop()
{
//Test relays
for(int i = 1; i < 9; i++){
relayController.turnOnRelay(i);
int rOnStatus = relayController.readRelayStatus(i);
Serial.print("Relay status after on: ");
Serial.println(rOnStatus);
delay(500);
}
for(int i = 1; i < 9; i++){
relayController.turnOffRelay(i);
int rOffStatus = relayController.readRelayStatus(i);
Serial.print("Relay status after off: ");
Serial.println(rOffStatus);
delay(500);
}
relayController.turnOnAllRelays();
delay(500);
relayController.turnOffAllRelays();
delay(500);
for(int i = 1; i < 9; i++){
relayController.toggleRelay(i);
delay(500);
relayController.toggleRelay(i);
delay(500);
}
relayController.setBankStatus(85);
int rStatus85 = relayController.readRelayBankStatus();
Serial.print("Status after setting relay bank status to 85: ");
Serial.println(rStatus85);
delay(500);
relayController.setBankStatus(170);
delay(500);
relayController.turnOffAllRelays();
delay(500);
}

71
examples/application.h Executable file
View file

@ -0,0 +1,71 @@
/**
******************************************************************************
* @file application.h
* @authors Satish Nair, Zachary Crockett, Zach Supalla and Mohit Bhoite
* @version V1.0.0
* @date 30-April-2013
* @brief User Application File Header
******************************************************************************
Copyright (c) 2013-2015 Particle Industries, Inc. All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation, either
version 3 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, see <http://www.gnu.org/licenses/>.
******************************************************************************
*/
#ifndef APPLICATION_H_
#define APPLICATION_H_
#include "particle_version.h"
#ifdef SPARK_PLATFORM
#include "platform_headers.h"
#endif
#include "spark_wiring.h"
#include "spark_wiring_cloud.h"
#include "spark_wiring_interrupts.h"
#include "spark_wiring_string.h"
#include "spark_wiring_print.h"
#include "spark_wiring_usartserial.h"
#include "spark_wiring_usbserial.h"
#include "spark_wiring_usbmouse.h"
#include "spark_wiring_usbkeyboard.h"
#include "spark_wiring_spi.h"
#include "spark_wiring_i2c.h"
#include "spark_wiring_servo.h"
#include "spark_wiring_wifi.h"
#include "spark_wiring_network.h"
#include "spark_wiring_client.h"
#include "spark_wiring_tcpclient.h"
#include "spark_wiring_tcpserver.h"
#include "spark_wiring_udp.h"
#include "spark_wiring_time.h"
#include "spark_wiring_tone.h"
#include "spark_wiring_eeprom.h"
#include "spark_wiring_version.h"
#include "spark_wiring_thread.h"
#include "fast_pin.h"
#include "string_convert.h"
#include "NCD8Relay.h"
// this was being implicitly pulled in by some of the other headers
// adding here for backwards compatibility.
#include "system_task.h"
#include "system_user.h"
#include "stdio.h"
using namespace spark;
#endif /* APPLICATION_H_ */

440
firmware/NCD8Relay.cpp Normal file
View file

@ -0,0 +1,440 @@
#include "NCD8Relay.h"
#include "spark_wiring_usbserial.h"
//Comment line below out to turn off Serial logging
//#define LOGGING
int address = 0x20;
int address2 = 0x21;
int retrys = 0;
byte outputRegister = 0x0A;
//Constructor
NCD8Relay::NCD8Relay(){
}
//Retry added
void NCD8Relay::setAddress(int a0, int a1, int a2){
address = 0x20;
if(a0 == 1){
address = address | 1;
}
if(a1 == 1){
address = address | 2;
}
if(a2 == 1){
address = address | 4;
}
//Start I2C port
Wire.begin();
//Open connection to specified address
retryAddress1:
Wire.beginTransmission(address);
//Set all channels to outputs
Wire.write(0x00);
Wire.write(0x00);
//Determine if device is present at that address
byte status = Wire.endTransmission();
Wire.beginTransmission(address);
Wire.write(0x06);
Wire.write(0x00);
status = Wire.endTransmission();
if(status != 0){
if(retrys < 3){
#ifdef LOGGING
Serial.println("Retry set address command");
#endif
retrys++;
goto retryAddress1;
}else{
#ifdef LOGGING
Serial.println("Set Address failed");
#endif
initialized = false;
retrys = 0;
}
}else{
// Serial.println("Command Successful");
initialized = true;
retrys = 0;
readStatus();
}
}
void NCD8Relay::turnOnRelay(int relay){
if(relay > 8 || relay < 0){
return;
}
byte bankValue = bankOneStatus;
byte registerAddress = 0x0A;
switch(relay){
case 1:
bankValue = bankValue | 1;
break;
case 2:
bankValue = bankValue | 2;
break;
case 3:
bankValue = bankValue | 4;
break;
case 4:
bankValue = bankValue | 8;
break;
case 5:
bankValue = bankValue | 16;
break;
case 6:
bankValue = bankValue | 32;
break;
case 7:
bankValue = bankValue | 64;
break;
case 8:
bankValue = bankValue | 128;
break;
}
turnOnRelayRetry:
Wire.beginTransmission(address);
Wire.write(registerAddress);
Wire.write(bankValue);
byte status = Wire.endTransmission();
if(status != 0){
if(retrys < 3){
#ifdef LOGGING
Serial.println("Retry Turn On Relay command");
#endif
retrys++;
goto turnOnRelayRetry;
}else{
#ifdef LOGGING
Serial.println("Turn On Relay failed");
#endif
initialized = false;
retrys = 0;
}
}else{
initialized = true;
retrys = 0;
readStatus();
}
}
void NCD8Relay::turnOffRelay(int relay){
if(relay > 8 || relay < 0){
return;
}
byte bankValue = bankOneStatus;
byte registerAddress = 0x0A;
switch(relay){
case 1:
bankValue = bankValue & ~1;
break;
case 2:
bankValue = bankValue & ~2;
break;
case 3:
bankValue = bankValue & ~4;
break;
case 4:
bankValue = bankValue & ~8;
break;
case 5:
bankValue = bankValue & ~16;
break;
case 6:
bankValue = bankValue & ~32;
break;
case 7:
bankValue = bankValue & ~64;
break;
case 8:
bankValue = bankValue & ~128;
break;
}
turnOffRelaysRetry:
Wire.beginTransmission(address);
Wire.write(registerAddress);
Wire.write(bankValue);
byte status = Wire.endTransmission();
if(status != 0){
if(retrys < 3){
#ifdef LOGGING
Serial.println("Retry Turn Off Relay command");
#endif
retrys++;
goto turnOffRelaysRetry;
}else{
#ifdef LOGGING
Serial.println("Turn Off Relay command failed");
#endif
initialized = false;
retrys = 0;
}
}else{
initialized = true;
retrys = 0;
readStatus();
}
}
void NCD8Relay::turnOnAllRelays(){
turnOnAllRelays1:
Wire.beginTransmission(address);
Wire.write(0x0A);
Wire.write(0xFF);
byte status = Wire.endTransmission();
if(status != 0){
if(retrys < 3){
#ifdef LOGGING
Serial.println("Retry turn off all relays banks 1-2 command");
#endif
retrys++;
goto turnOnAllRelays1;
}else{
#ifdef LOGGING
Serial.println("Turn off all relays banks 1-2 command failed");
#endif
initialized = false;
retrys = 0;
}
}else{
initialized = true;
retrys = 0;
readStatus();
}
}
void NCD8Relay::turnOffAllRelays(){
turnOffAllRelaysRetry1:
Wire.beginTransmission(address);
Wire.write(0x0A);
Wire.write(0);
byte status = Wire.endTransmission();
if(status != 0){
if(retrys < 3){
#ifdef LOGGING
Serial.println("Retry turn off all relays banks 1-2 command");
#endif
retrys++;
goto turnOffAllRelaysRetry1;
}else{
#ifdef LOGGING
Serial.println("Turn off all relays banks 1-2 command failed");
#endif
initialized = false;
retrys = 0;
}
}else{
initialized = true;
retrys = 0;
readStatus();
}
}
void NCD8Relay::toggleRelay(int relay){
if(relay > 8 || relay < 0){
return;
}
byte bankValue = bankOneStatus;
switch(relay){
case 1:
bankValue = bankValue ^ 1;
break;
case 2:
bankValue = bankValue ^ 2;
break;
case 3:
bankValue = bankValue ^ 4;
break;
case 4:
bankValue = bankValue ^ 8;
break;
case 5:
bankValue = bankValue ^ 16;
break;
case 6:
bankValue = bankValue ^ 32;
break;
case 7:
bankValue = bankValue ^ 64;
break;
case 8:
bankValue = bankValue ^ 128;
break;
}
toggleRelayRetry:
Wire.beginTransmission(address);
Wire.write(0x0A);
Wire.write(bankValue);
byte status = Wire.endTransmission();
if(status != 0){
if(retrys < 3){
#ifdef LOGGING
Serial.println("Retry toggle relay command");
#endif
retrys++;
goto toggleRelayRetry;
}else{
#ifdef LOGGING
Serial.println("Toggle relay command failed");
#endif
initialized = false;
retrys = 0;
}
}else{
initialized = true;
retrys = 0;
readStatus();
}
}
void NCD8Relay::setBankStatus(int status){
setBankStatusRetry:
Wire.beginTransmission(address);
Wire.write(0x0A);
Wire.write(status);
byte s = Wire.endTransmission();
if(s != 0){
if(retrys < 3){
#ifdef LOGGING
Serial.println("Retry set bank status command");
#endif
retrys++;
goto setBankStatusRetry;
}else{
#ifdef LOGGING
Serial.println("Set bank status command failed");
#endif
initialized = false;
retrys = 0;
}
}else{
initialized = true;
retrys = 0;
readStatus();
}
}
int NCD8Relay::readRelayStatus(int relay){
if(relay > 8 || relay < 0){
return 256;
}
int value;
switch(relay){
case 1:
value = 1;
break;
case 2:
value = 2;
break;
case 3:
value = 4;
break;
case 4:
value = 8;
break;
case 5:
value = 16;
break;
case 6:
value = 32;
break;
case 7:
value = 64;
break;
case 8:
value = 128;
break;
}
getRelayStatusRetry:
Wire.beginTransmission(address);
Wire.write(0x0A);
byte status = Wire.endTransmission();
if(status != 0){
if(retrys < 3){
#ifdef LOGGING
Serial.println("Retry read relay status command");
#endif
retrys++;
goto getRelayStatusRetry;
}else{
#ifdef LOGGING
Serial.println("Read relay status command failed");
#endif
initialized = false;
retrys = 0;
return 256;
}
}else{
retrys = 0;
initialized = true;
}
Wire.requestFrom(address, 1);
byte bankStatus = Wire.read();
if(bankStatus & value){
return 1;
}else{
return 0;
}
}
int NCD8Relay::readRelayBankStatus(){
readBankStatusRetry:
Wire.beginTransmission(address);
Wire.write(0x0A);
byte status = Wire.endTransmission();
if(status != 0){
if(retrys < 3){
#ifdef LOGGING
Serial.println("Retry read bank status command");
#endif
retrys++;
goto readBankStatusRetry;
}else{
#ifdef LOGGING
Serial.println("Read bank status command failed");
#endif
initialized = false;
retrys = 0;
return 256;
}
}else{
retrys = 0;
initialized = true;
}
Wire.requestFrom(address, 1);
byte bankStatus = Wire.read();
return bankStatus;
}
void NCD8Relay::readStatus(){
readBankOneRetry:
//Open Connection to controller
Wire.beginTransmission(address);
//Get current status of relays 1-8
Wire.write(0x0A);
byte status = Wire.endTransmission();
if(status != 0){
if(retrys < 3){
#ifdef LOGGING
Serial.println("Retry read status banks 1-2 command");
#endif
retrys++;
goto readBankOneRetry;
}else{
#ifdef LOGGING
Serial.println("Read ststus banks 1-2 command failed");
#endif
initialized = false;
retrys = 0;
}
}else{
retrys = 0;
initialized = true;
Wire.requestFrom(address, 1);
bankOneStatus = Wire.read();
}
}

44
firmware/NCD8Relay.h Normal file
View file

@ -0,0 +1,44 @@
#include "spark_wiring_i2c.h"
#include "spark_wiring_usbserial.h"
#include "spark_wiring_constants.h"
class NCD8Relay{
public:
//Constructor
NCD8Relay(void);
//Set Address. Indicate status of jumpers on board. Send 0 for not installed, send 1 for installed
void setAddress(int a0, int a1, int a2);
//Turn on Relay
void turnOnRelay(int relay);
//Turn off Relay
void turnOffRelay(int relay);
//Turn On all Relays
void turnOnAllRelays();
//Turn Off All Relays
void turnOffAllRelays();
//Toggle Relay
void toggleRelay(int relay);
//Set status of all relays in bank
void setBankStatus(int status);
//Read status of relay. Valid return 0 for off 1 for on. 256 returned if there is an error
int readRelayStatus(int relay);
//Read status of all relays in bank. 0-255 valid. 256 returned on error
int readRelayBankStatus();
//User Accessible Variables
//Whether or not the controller is ready to accept commands
bool initialized;
private:
//internal use method for refreshing bank status variables
void readStatus();
//Status of relays in bank 1
byte bankOneStatus;
//Status of relays in bank 2
byte bankTwoStatus;
//Status of relays in bank 3
byte bankThreeStatus;
//Status of relays in bank 4
byte bankFourStatus;
};

7
spark.json Normal file
View file

@ -0,0 +1,7 @@
{
"name": "NCD8Relay",
"version": "0.0.1",
"author": "Travis Elliott <travis@controlanything.com>",
"license": "GNU",
"description": "Library intended for the operation of NCD 8 channel Spark compatible Relay Controller"
}