---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);