Skip to content
Snippets Groups Projects
Commit 3d66d4d0 authored by 한소진's avatar 한소진
Browse files

Add new file

parent 79d6970a
Branches main
No related tags found
No related merge requests found
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <stdint.h>
#include <unistd.h>
#include <getopt.h>
#include <sys/ioctl.h>
#include <linux/types.h>
#include <linux/spi/spidev.h>
#define IN 0
#define OUT 1
#define LOW 0
#define HIGH 1
#define POUT 18
#define POUT2 4
#define S_POUT 20
#define VALUE_MAX 30
#define ARRAY_SIZE(array) sizeof(array) / sizeof(array[0])
static const char *DEVICE = "/dev/spidev0.0";
static uint8_t MODE = SPI_MODE_0;
static uint8_t BITS = 8;
static uint32_t CLOCK = 1000000;
static uint16_t DELAY = 5;
void error_handling( char *message){
fputs(message,stderr);
fputc( '\n',stderr);
exit(1);
}
static int
PWMExport(int pwmnum)
{
#define BUFFER_MAX 3
char buffer[BUFFER_MAX];
int bytes_written;
int fd;
fd = open("/sys/class/pwm/pwmchip0/unexport", O_WRONLY);
if (-1 == fd){
fprintf(stderr, "Failed to open in unexport!\n");
return(-1);
}
bytes_written = snprintf(buffer, BUFFER_MAX, "%d", pwmnum);
write(fd, buffer, bytes_written);
close(fd);
sleep(1);
fd = open("/sys/class/pwm/pwmchip0/export", O_WRONLY);
if (-1 == fd){
fprintf(stderr, "Failed to open in export!\n");
return( -1);
}
bytes_written = snprintf(buffer, BUFFER_MAX, "%d", pwmnum);
write(fd, buffer, bytes_written);
close(fd);
sleep(1);
return(0);
}
static int
PWMUnexport(int pwmnum){
char buffer[BUFFER_MAX];
int bytes_written;
int fd;
fd = open(" /sys/class/pwm/pwmchip0/unexport", O_WRONLY);
if (-1 == fd){
fprintf(stderr, "Failed to open in unexport!\n");
return(-1);
}
bytes_written = snprintf(buffer, BUFFER_MAX, "%d", pwmnum);
write(fd, buffer, bytes_written);
close(fd);
sleep(1);
return(0);
}
static int
PWMEnable(int pwmnum)
{
static const char s_unenable_str[] = "0";
static const char s_enable_str[] = "1";
#define DIRECTION_MAX 45
char path[DIRECTION_MAX];
int fd;
snprintf(path, DIRECTION_MAX, "/sys/class/pwm/pwmchip0/pwm%d/enable", pwmnum);
fd = open(path, O_WRONLY);
if (-1 == fd) {
fprintf(stderr, "Failed to open in enable!\n");
return -1;
}
write(fd, s_unenable_str, strlen(s_unenable_str));
close(fd);
fd = open(path, O_WRONLY);
if (-1 == fd){
fprintf(stderr, "Failed to open in enable!\n");
return -1;
}
write(fd, s_enable_str, strlen(s_enable_str));
close(fd);
return(0);
}
static int
PWMWritePeriod(int pwmnum, int value)
{
char s_values_str[VALUE_MAX];
char path[VALUE_MAX];
int fd, byte;
snprintf(path, VALUE_MAX, "/sys/class/pwm/pwmchip0/pwm%d/period", pwmnum);
fd = open(path, O_WRONLY);
if (-1 == fd) {
fprintf(stderr, "Failed to open in period!\n");
return(-1);
}
byte = snprintf(s_values_str, 10,"%d", value);
if (-1 == write(fd, s_values_str, byte)) {
fprintf(stderr, "Failed to write value in period!\n");
close(fd);
return(-1);
}
close(fd);
return(0);
}
static int
PWMWriteDutyCycle(int pwmnum, int value)
{
char path[VALUE_MAX];
char s_values_str[VALUE_MAX];
int fd, byte;
snprintf(path, VALUE_MAX, "/sys/class/pwm/pwmchip0/pwm%d/duty_cycle", pwmnum);
fd = open(path, O_WRONLY);
if (-1 == fd) {
fprintf(stderr, "Failed to open in duty cycle!\n");
return(-1);
}
byte = snprintf(s_values_str, 10, "%d",value);
if (-1 == write(fd, s_values_str, byte)) {
fprintf(stderr, "Failed to write value! in duty_cycle\n");
close(fd);
return(-1);
}
close(fd);
return(0);
}
static int GPIOExport(int pin){
#define BUFFER_MAX 3
char buffer[BUFFER_MAX];
ssize_t bytes_written;
int fd;
fd=open("/sys/class/gpio/export",O_WRONLY);
if(-1==fd){
fprintf(stderr,"Failed to open export for writing!\n");
return(-1);
}
bytes_written=snprintf(buffer,BUFFER_MAX,"%d",pin);
write(fd,buffer,bytes_written);
close(fd);
return(0);
}
static int GPIODirection(int pin,int dir){
static const char s_directions_str[] = "in\0out";
char path[DIRECTION_MAX]="/sys/class/gpio/gpio%d/direction";
int fd;
snprintf(path, DIRECTION_MAX, "/sys/class/gpio/gpio%d/direction",pin);
fd=open(path,O_WRONLY);
if(-1==fd){
fprintf(stderr,"Failed to open gpio direction for writing\n");
return(-1);
}
if(-1==write(fd,&s_directions_str[IN==dir?0:3],IN==dir?2:3)){
fprintf(stderr,"Failed to set direction\n");
close(fd);
return(-1);
}
close(fd);
return(0);
}
static int GPIORead(int pin){
char path[VALUE_MAX];
char value_str[3];
int fd;
snprintf(path,VALUE_MAX, "/sys/class/gpio/gpio%d/value",pin);
fd = open(path,O_RDONLY);
if(-1 == fd){
fprintf(stderr,"Failed to open gpio value for reading!\n");
return(-1);
}
if(-1 == read(fd,value_str,3)){
fprintf(stderr,"Failed to read value!\n");
close(fd);
return(-1);
}
close(fd);
return(atoi(value_str));
}
static int GPIOWrite(int pin, int value){
static const char s_values_str[]="01";
char path[VALUE_MAX];
int fd;
snprintf(path,VALUE_MAX,"/sys/class/gpio/gpio%d/value",pin);
fd=open(path,O_WRONLY);
if(-1==fd){
/// fprintf(stderr,"Failed to open gpio value for writing!\n");
return(-1);
}
if(1!=write(fd, &s_values_str[LOW==value?0:1],1)){
// fprintf(stderr,"Failed to write value!\n");
close(fd);
return(-1);
}
close(fd);
return(0);
}
static int GPIOUnexport(int pin){
char buffer[BUFFER_MAX];
ssize_t bytes_written;
int fd;
fd=open("/sys/class/gpio/unexport",O_WRONLY);
if(-1==fd){
fprintf(stderr,"Failed to open unexport for writing!\n");
return(-1);
}
bytes_written=snprintf(buffer,BUFFER_MAX,"%d",pin);
write(fd,buffer,bytes_written);
close(fd);
return(0);
}
// SGL/DIF = 0 , D2=D1=D0=0
uint8_t control_bits_differential(uint8_t channel){
return (channel & 7) << 4;
}
// SGL/DIF = 1 , D2=D1=D0=0
uint8_t control_bits(uint8_t channel){
return 0x8 | control_bits_differential(channel);
}
int readadc(int fd, uint8_t channel) {
uint8_t tx[] = {1, control_bits(channel), 1};
uint8_t rx[3];
struct spi_ioc_transfer tr = {
.tx_buf = (unsigned long)tx,
.rx_buf = (unsigned long)rx,
.len = ARRAY_SIZE(tx),
.delay_usecs = DELAY,
.speed_hz = CLOCK,
.bits_per_word = BITS,
};
if (ioctl(fd, SPI_IOC_MESSAGE(1), &tr) == 1){
perror("IO Error");
abort();
}
return((rx[1] << 8) & 0x300) | (rx[2] & 0xFF);
}
static int prepare(int fd){
if (ioctl(fd, SPI_IOC_WR_MODE, &MODE) == -1) {
perror("Can't set MODE");
return -1;
}
if (ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &BITS) == -1){
perror("Can't set number of BITS");
return -1;
}
if (ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &CLOCK) == -1){
perror("Can't set write CLOCK");
return -1;
}
if (ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &CLOCK) == -1) {
perror("Can't set read CLOCK");
return -1;
}
return 0;
}
int main(int argc, char *argv[]) {
int sock;
struct sockaddr_in serv_addr;
char msg[2];
char on[2]="1";
int str_len;
int light;
if(argc!=3){
printf("Usage : %s <IP> <port>\n",argv[0]);
exit(1);
}
//Enable GPIO pins
if (-1 == GPIOExport(POUT))
return(1);
//Set GPIO directions
if (-1 == GPIODirection(POUT, OUT))
return(2);
sock = socket(PF_INET, SOCK_STREAM, 0);
if(sock == -1)
error_handling("socket() error");
memset(&serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = inet_addr(argv[1]);
serv_addr.sin_port = htons(atoi(argv[2]));
int fd = open(DEVICE, O_RDWR);
if (fd <= 0){
printf("Device %s not found\n", DEVICE);
return -1;
}
if (prepare(fd) == -1) {
return -1;
}
if(connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr))==-1)
error_handling("connect() error");
int count = 0; //의자용 count
int cnt=0; // 스피커용 count
int number = 0; // 횟수 측정용
while(1){
str_len = read(sock, msg, sizeof(msg));
if(str_len == -1)
error_handling("read() error");
printf("%d Receive message from Server : %s\n",number,msg);
number++;
///////////
int p_one = 0;
int p_two = 0;
if(strcmp("0",msg)==0){
count=0;
GPIOWrite(POUT,0);
cnt=0;
GPIOWrite(S_POUT,0);
}
else{
p_one = readadc(fd, 0); // 압력용
p_two = readadc(fd, 1);
int s_one = readadc(fd, 0); //스피커용
int s_two = readadc(fd, 1);
int k = 0;
printf("1 : %d\n",p_one);
printf("2 : %d\n\n",p_two);
// 의자 XOR
if(p_one>0) p_one=0; else p_one=1;
if(p_two>0) p_two=0; else p_two=1;
if(p_one ^ p_two ==1){
count++;
}else{
count = 0;
}
if(count >= 5){
GPIOExport(POUT2);
GPIODirection(POUT2,1);
GPIOWrite(POUT,1);
GPIOWrite(POUT2,1);
}else if(count < 5){
GPIOExport(POUT);
GPIODirection(POUT,1);
GPIOWrite(POUT,0);
}
//부저
if(s_one>0 && s_two>0){
cnt++;
}else cnt=0;
if(cnt=7){
PWMExport(0); // pwm0 is gpio18
PWMWritePeriod(0, 20000000);
PWMWriteDutyCycle(0, 0);
PWMEnable(0);
for (int i = 0; i < 1000; i++){
PWMWriteDutyCycle(0,i * 10000);
usleep(1000);
}
for (int i = 1000; i > 0; i--){
PWMWriteDutyCycle(0,i*10000);
usleep(1000);
}
}
}
usleep(1000000);
}
close(fd);
close(sock);
//Disable GPIO pins
if (-1 == GPIOUnexport(POUT))
return(4);
return(0);
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment