SmartCradle v1
Baby smart cradle
motor.c
Go to the documentation of this file.
1
10#include <stdio.h>
11#include <stdlib.h>
12#include <unistd.h>
13#include <fcntl.h>
14#include <linux/types.h>
15#include "../inc/motor.h"
16
17const char RUN = '1';
18const char STOP = '0';
19
20static _Bool motorRunning; /* Status of the motor */
21
22void initMotor(){
23 system("insmod motordriver.ko");
24 motorRunning = 0;
25}
26
27void remMotor(){
28 system("rmmod motordriver");
29}
30
32
33 if(!motorRunning)
34 {
35 int fd0 = open("/dev/motordriver0", O_WRONLY);
36 if(fd0 == -1)
37 {
38 printf("Failed to open motor driver device driver.\n");
39 return 0;
40 }
41
42 if(write(fd0, &RUN, 1) <= 0)
43 {
44 printf("Failed to write on device driver.\n");
45 close(fd0);
46 return 0;
47 }
48
49 motorRunning = 1;
50 close(fd0);
51 }
52 return 1;
53}
54
56
57 if(motorRunning)
58 {
59 int fd0 = open("/dev/motordriver0", O_WRONLY);
60 if(fd0 == -1)
61 {
62 printf("Failed to open motor driver device driver.\n");
63 return 0;
64 }
65
66 if(write(fd0, &STOP, 1) <= 0)
67 {
68 printf("Failed to write on device driver.\n");
69 close(fd0);
70 return 0;
71 }
72
73 motorRunning = 0;
74 close(fd0);
75 }
76 return 1;
77}
78
80{
81 return motorRunning;
82}
void initMotor()
Load the motor driver device driver.
Definition: motor.c:22
int stopMotor()
Stop the motor.
Definition: motor.c:55
void remMotor()
Remove the motor driver device driver.
Definition: motor.c:27
int startMotor()
Start the motor.
Definition: motor.c:31
_Bool getMotorStatus()
Get the Motor Status object.
Definition: motor.c:79