Created
November 20, 2017 04:43
-
-
Save peterwallhead/1595feacffa240b0d0d02e3e526236b9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* A version of https://github.com/johnwargo/Seeed-Studio-Relay-Board/blob/master/seeed_relay_test.py in C for use with https://www.seeedstudio.com/Raspberry-Pi-Relay-Board-v1.0-p-2409.html */ | |
#include <stdio.h> | |
#include <string.h> | |
#include <errno.h> | |
#include <wiringPiI2C.h> | |
int deviceRegMode1 = 0x06; | |
int deviceRegData = 0xff; | |
int relay (int deviceAddress, int relayNumber, int relayState) { | |
int fd, relayResult; | |
fd = wiringPiI2CSetup(deviceAddress); | |
if(relayNumber == 1 && relayState == 1) { | |
deviceRegData &= ~(0x1 << 0); | |
} | |
if(relayNumber == 1 && relayState == 0) { | |
deviceRegData |= (0x1 << 0); | |
} | |
if(relayNumber == 2 && relayState == 1) { | |
deviceRegData &= ~(0x1 << 1); | |
} | |
if(relayNumber == 2 && relayState == 0) { | |
deviceRegData |= (0x1 << 1); | |
} | |
if(relayNumber == 3 && relayState == 1) { | |
deviceRegData &= ~(0x1 << 2); | |
} | |
if(relayNumber == 3 && relayState == 0) { | |
deviceRegData |= (0x1 << 2); | |
} | |
if(relayNumber == 4 && relayState == 1) { | |
deviceRegData &= ~(0x1 << 3); | |
} | |
if(relayNumber == 4 && relayState == 0) { | |
deviceRegData |= (0x1 << 3); | |
} | |
// All on | |
if(relayNumber == 5 && relayState == 5) { | |
deviceRegData &= ~(0xf << 0); | |
} | |
// All off | |
if(relayNumber == 0 && relayState == 0) { | |
deviceRegData |= (0xf << 0); | |
} | |
relayResult = wiringPiI2CWriteReg16(fd, deviceRegMode1, deviceRegData); | |
return relayResult; | |
} | |
int main(int argc, const char * argv[]) { | |
int result; | |
char rCommand[20]; | |
while(1) { | |
printf("%s", "Input: "); | |
fgets(rCommand,20,stdin); | |
if (strcmp(rCommand, "1on\n") == 0) { | |
result = relay(0x20, 1, 1); | |
} | |
if (strcmp(rCommand, "1off\n") == 0) { | |
result = relay(0x20, 1, 0); | |
} | |
if (strcmp(rCommand, "2on\n") == 0) { | |
result = relay(0x20, 2, 1); | |
} | |
if (strcmp(rCommand, "2off\n") == 0) { | |
result = relay(0x20, 2, 0); | |
} | |
if (strcmp(rCommand, "3on\n") == 0) { | |
result = relay(0x20, 3, 1); | |
} | |
if (strcmp(rCommand, "3off\n") == 0) { | |
result = relay(0x20, 3, 0); | |
} | |
if (strcmp(rCommand, "4on\n") == 0) { | |
result = relay(0x20, 4, 1); | |
} | |
if (strcmp(rCommand, "4off\n") == 0) { | |
result = relay(0x20, 4, 0); | |
} | |
if (strcmp(rCommand, "allon\n") == 0) { | |
result = relay(0x20, 5, 5); | |
} | |
if (strcmp(rCommand, "alloff\n") == 0) { | |
result = relay(0x20, 0, 0); | |
} | |
if(result == -1) { | |
printf("%s\n", "Nothing written"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment