PRO-1:停车系统框架完善

---main.c

---include

                 :key.h

:led.h

:my.h

:pwm.h

---lib

              :fun.c

:key.c

:led.c

pwm.c



--------------------------------main.c------------------------------

#include <my.h>
#include"key.h"
#include"led.h"
#include"pwm.h"


int main(void)
{
USR *user = NULL;


user = user_init();
led_init();
key_init();
pwm_open();
pwm_outset();
led_outset();


for(;;)
{
input_userinfo(user);
start_time(user);
printf("PRESS ENTER KEY CONTINUE...\n");
getchar();
end_time(user);
print_userinfo(user);
}
user_exit(user);
led_exit();
key_exit();
pwm_close();
return 0;
}

----------------------------------fun.c----------------------------------------

#include <my.h>
#include"key.h"


USR *user_init(void)
{
USR *temp = NULL;


if((temp = (USR *)malloc(sizeof(USR))) == NULL)
{
perror("MALLOC ERROR");
exit(EXIT_FAILURE);
}
return temp;
}




void user_exit(USR *temp)
{
free(temp);
return;
}


void input_userinfo(USR *temp)
{
int num;
printf("===WELCOME===\n");
printf("Please input name:>");
scanf("%s", temp->name);
printf("Please input card:>");
scanf("%s", temp->card);


FLAG:
printf("TYPE LIST:\n");
printf("1----20/h\n");
printf("2----15/h\n");
printf("3----10/h\n");
printf("4----5/h\n");
printf("please choose:>");
num=key_read();
pwm_outset();
switch(num)
{
case 1: temp->type=1;
temp->money=20.00f;
break;
case 2: temp->type=2;
temp->money=15.00f;
break;
case 3: temp->type=3;
temp->money=10.00f;
break;
case 4: temp->type=4;
temp->money=5.00f;
break;
default:
printf("ERROR, INPUT AGAIN!");
goto FLAG;
}
/* scanf("%d", &temp->type);


switch(temp->type)
{
case 1: temp->money = 20.00f; break;
case 2: temp->money = 15.00f; break;
case 3: temp->money = 10.00f; break;
case 4: temp->money = 5.00f; break;
default:
printf("ERROR, INPUT AGAIN!");
goto FLAG;
}
*/
getchar();
return;
}


void start_time(USR *temp)
{
time_t mytm;
struct tm *val = NULL;


mytm = time(NULL);
val = localtime(&mytm);


memcpy(&temp->start_time, val, sizeof(struct tm));
printf("start time: %d-%d-%d %d:%d:%d\n", val->tm_year+1900, val->tm_mon+1\
,val->tm_mday, val->tm_hour, val->tm_min, val->tm_sec);
return;
}




void end_time(USR *temp)
{
time_t mytm;
struct tm *val = NULL;


mytm = time(NULL);
val = localtime(&mytm);


memcpy(&temp->end_time, val, sizeof(struct tm));
printf("end time: %d-%d-%d %d:%d:%d\n", val->tm_year+1900, val->tm_mon+1\
,val->tm_mday, val->tm_hour, val->tm_min, val->tm_sec);
return;
}


void print_userinfo(USR *temp)
{
int val;


val = temp->end_time.tm_sec - temp->start_time.tm_sec;
if(val < 0)
val += 60;


printf("NAME:%s\n", temp->name);
printf("CARD:%s\n", temp->card);
printf("TYPE:%d\n", temp->type);
printf("MONEY:%.2f\n", temp->money * val);
printf("==BYEBYE===\n");
return;
}


------------------------------------------key.c----------------------------------------------

#include"key.h"




void key_init(void)
{
if((key_fd=open(KEY_NAME,O_RDWR))<0)
{
perror("OPEN FAIL");
exit(-1);
}
}


void key_exit(void)
{
if(close(key_fd)<0)
{
perror("EXIT FAIL");
exit(-1);
}
}
int key_read(void)
{
int num;


while(read(key_fd,&num,sizeof(int))<0);
return num;
}

--------------------------------------------led.c----------------------------------------------

#include"led.h"




void led_init()
{
if((led_fd=open(LED_NAME,O_RDWR))<0)
{
perror("OPEN FAIL");
exit(-1);
}


}
void led_exit(void)
{
if(close(led_fd)<0)
{
perror("EXIT FAIL");
exit(-1);
}
}
void led_on(int num)
{
if(ioctl(led_fd,CMD_LED_ON,num)<0)
{
perror("ON FAIL");
exit(-1);
}
}
void led_off(int num)
{
if(ioctl(led_fd,CMD_LED_OFF,num)<0)
{
perror("OFF FAIL");
exit(-1);
}
}
void led_outset(void)
{
led_on(1);
led_on(2);
led_on(3);
usleep(50000);
led_off(1);
led_off(2);
led_off(3);


led_on(1);
usleep(50000);
led_off(1);
led_on(2);
usleep(50000);
led_off(2);
led_on(3);
usleep(50000);
led_off(3);


}

----------------------------------------pwm.c---------------------------------------------------

#include"pwm.h"




void pwm_open(void)
{
if((pwm_fd=open("/dev/pwm",O_RDWR))<0)
{
perror("OPEN FILE FAIL!!!");
exit(EXIT_FAILURE);
}
}
void pwm_close(void)
{
close(pwm_fd);
}
void pwm_fre(int num)
{
ioctl(pwm_fd,SET_FRE,num);
}
void pwm_beep_on(void)
{
ioctl(pwm_fd,BEEP_ON);
}
void pwm_beep_off(void)
{
ioctl(pwm_fd,BEEP_OFF);
}
void pwm_outset(void)
{
int i,wide=100;
for (i=0; i<3; i++)
{
wide+=1500;
pwm_fre(wide);
pwm_beep_on();
usleep(50000);
pwm_beep_off();
}
}


-----------------------------------------------key.h--------------------------------------------------

#ifndef _KEY_H_
#define _KEY_H_


#include<stdio.h>
#include<stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>


#define KEY_NAME "/dev/key"


int key_fd;
void key_init(void);
void key_exit(void);
int key_read(void);
#endif

---------------------------------------led.h--------------------------------------------------------------

#ifndef _LED_H_
#define _LED_H_  
 
#include<stdio.h>
#include<stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
 
#define LED_NAME "/dev/led"
#define CMD_LED_ON _IO('k',0)
#define CMD_LED_OFF _IO('k',1)


int led_fd;


void led_init(void);
void led_exit(void);
void led_on(int);
void led_off(int);


#endif

----------------------------------------------my.h-------------------------------------------------------

#ifndef _MY_H_
#define _MY_H_


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>


#define LEN 20


typedef struct  
{
char name[20];
char card[20];
int type;
float money;
struct tm start_time;
struct tm end_time;
}USR;


USR *user_init(void);
void user_exit(USR *);
void input_userinfo(USR *);
void start_time(USR *);
void end_time(USR *);
void print_userinfo(USR *);


#endif

-----------------------------------------------pwm.h--------------------------------------------------



#include<stdio.h>
#include<stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#define magic_number 'k'
#define BEEP_ON _IO(magic_number,0)
#define BEEP_OFF _IO(magic_number,1)
#define SET_FRE _IO(magic_number,2)


int pwm_fd;
void pwm_open(void);
void pwm_close(void);
void pwm_fre(int);
void pwm_beep_on(void);
void pwm_beep_off(void);



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

许进进

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值