This repository has been archived by the owner on Apr 23, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseven7segment.c
188 lines (143 loc) · 5.39 KB
/
seven7segment.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/* Necessary includes for drivers */
#include <linux/init.h>
#include <linux/module.h>
#include <linux/gpio.h>
#include <linux/kernel.h>
#include <linux/kobject.h> // Using kobjects for the sysfs bindings
#include <linux/kthread.h> // Using kthreads for the flashing functionality
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Sofiia Tesliuk <[email protected]>");
MODULE_DESCRIPTION("Seven segment driver - Linux device driver for Raspberry Pi");
#define DEVICE_NAME "seven7segment"
#define MY_MAX_MINORS 1
#define PINS_NUMBER 7
#define A 4
#define B 5
#define C 6
#define D 12
#define E 13
#define F 16
#define G 17
#define end -1
int all_pins[8] = {A, B, C, D, E, F, G, end};
int number_0[7] = {A, B, C, D, E, F, end};
int number_1[3] = {B, C, end};
int number_2[6] = {A, B, D, E, G, end};
int number_3[6] = {A, B, C, D, G, end};
int number_4[5] = {B, C, F, G, end};
int number_5[6] = {A, C, D, F, G, end};
int number_6[7] = {A, C, D, E, F, G, end};
int number_7[4] = {A, B, C, end};
int number_8[8] = {A, B, C, D, E, F, G, end};
int number_9[7] = {A, B, C, D, F, G, end};
int* numbers[10] = {number_0, number_1, number_2, number_3, number_4,
number_5, number_6, number_7, number_8, number_9};
bool active_mode = true; // Default mode is active
int current_digit = 0; // Default number is 0
void sleep_mode(void);
void set_digit(int digit);
static ssize_t seven7segment_show_digit(struct kobject *kobj, struct kobj_attribute *attr, char *buf){
return sprintf(buf, "%d\n", current_digit);
}
static ssize_t seven7segment_store_digit(struct kobject *kobj, struct kobj_attribute *attr, const char *buf, size_t count){
if (strncmp(buf, "0", count-1)==0) { set_digit(0); }
else if (strncmp(buf,"1",count-1)==0) { set_digit(1);}
else if (strncmp(buf,"2",count-1)==0) { set_digit(2);}
else if (strncmp(buf,"3",count-1)==0) { set_digit(3);}
else if (strncmp(buf,"4",count-1)==0) { set_digit(4);}
else if (strncmp(buf,"5",count-1)==0) { set_digit(5);}
else if (strncmp(buf,"6",count-1)==0) { set_digit(6);}
else if (strncmp(buf,"7",count-1)==0) { set_digit(7);}
else if (strncmp(buf,"8",count-1)==0) { set_digit(8);}
else if (strncmp(buf,"9",count-1)==0) { set_digit(9);}
else{
printk("[seven7segment] - Invalid digit.\n");
}
return count;
}
static ssize_t seven7segment_show_mode(struct kobject *kobj, struct kobj_attribute *attr, char *buf){
if (active_mode)
return sprintf(buf, "active\n");
else
return sprintf(buf, "sleep\n");
}
static ssize_t seven7segment_store_mode(struct kobject *kobj, struct kobj_attribute *attr, const char *buf, size_t count){
if (strncmp(buf, "a", count-1)==0) { active_mode = true; set_digit(current_digit); }
else if (strncmp(buf,"s",count-1)==0) { active_mode = false; sleep_mode(); }
else{
printk("[seven7segment] - Invalid mode.\n");
}
return count;
}
static struct kobj_attribute digit_attr = __ATTR(digit, 0660, seven7segment_show_digit, seven7segment_store_digit);
static struct kobj_attribute mode_attr = __ATTR(mode, 0660, seven7segment_show_mode, seven7segment_store_mode);
static struct attribute *seven7segment_attrs[] = {
&digit_attr.attr,
&mode_attr.attr,
NULL,
};
static struct attribute_group attr_group = {
.name = DEVICE_NAME, // The name is generated in ebbLED_init()
.attrs = seven7segment_attrs, // The attributes array defined just above
};
static struct kobject *seven7segment_kobj; // The pointer to the kobject
void sleep_mode(){
int g;
for (g = 0; g < PINS_NUMBER; g++){
gpio_set_value(all_pins[g], false);
}
}
void set_digit(int digit){
if ((digit < 0) || (digit > 9)){
printk("[seven7segment] - Digit is out of range.\n");
return;
}
sleep_mode();
current_digit = digit;
if (active_mode){
int* cur_num = numbers[digit];
while(*cur_num != end){
gpio_set_value(*cur_num, true);
cur_num++;
}
printk("[seven7segment] - Set digit %d.\n", digit);
}
}
static int __init seven7segment_init(void);
static void __exit seven7segment_exit(void);
static int __init seven7segment_init(void) {
int result = 0;
seven7segment_kobj = kobject_create_and_add(DEVICE_NAME, kernel_kobj->parent);
if(!seven7segment_kobj) {
printk("[seven7segment]: error creating kobj.\n");
return -ENOMEM;
}
result = sysfs_create_group(seven7segment_kobj, &attr_group);
if(result) {
printk(KERN_ALERT "[seven7segment]: failed to create sysfs group.\n");
kobject_put(seven7segment_kobj); // clean up -- remove the kobject sysfs entry
return result;
}
int g;
char *pins_name = { "A", "B", "C", "D", "E", "F", "G" };
for (g = 0; g < PINS_NUMBER; g++) {
gpio_request(all_pins[g], &pins_name[g]);
}
for (g = 0; g < PINS_NUMBER; g++) {
gpio_direction_output(all_pins[g], 0);
}
set_digit(0);
printk("[seven7segment] - Inserting module.\n");
return 0;
}
static void __exit seven7segment_exit(void) {
sleep_mode();
kobject_put(seven7segment_kobj);
int g;
for (g = 0; g < PINS_NUMBER; g++) {
gpio_free(all_pins[g]);
}
printk("[seven7segment] - Removing module.\n");
}
module_init(seven7segment_init);
module_exit(seven7segment_exit);