Skip to content

Commit 9863ba3

Browse files
authored
右と左でそれぞれ用意されている関数を一つにまとめる(リファクタリング) (#93)
1 parent 5bdf8e3 commit 9863ba3

File tree

4 files changed

+81
-138
lines changed

4 files changed

+81
-138
lines changed

src/drivers/rtmouse.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@
5454
// Raspberry Pi 4 B : 4
5555
#define RASPBERRYPI 2
5656

57+
#define DEV_RIGHT 0
58+
#define DEV_LEFT 1
59+
5760
/* --- Device ID --- */
5861
#define ID_DEV_LED 0
5962
#define ID_DEV_SWITCH 1

src/drivers/rtmouse_dev.c

Lines changed: 40 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -229,10 +229,10 @@ static int getPWMCount(int freq)
229229
}
230230

231231
/*
232-
* left motor function
232+
* motor function
233233
* called by parseMotorCmd() and rawmotor_l_write()
234234
*/
235-
static void set_motor_l_freq(int freq)
235+
static void set_motor_freq(int freq, const int dev_side)
236236
{
237237
int dat;
238238

@@ -244,65 +244,50 @@ static void set_motor_l_freq(int freq)
244244
}
245245

246246
if (freq == 0) {
247-
rpi_gpio_function_set(MOTCLK_L_BASE, RPI_GPF_OUTPUT);
247+
if (dev_side == DEV_LEFT) {
248+
rpi_gpio_function_set(MOTCLK_L_BASE, RPI_GPF_OUTPUT);
249+
} else if (dev_side == DEV_RIGHT) {
250+
rpi_gpio_function_set(MOTCLK_R_BASE, RPI_GPF_OUTPUT);
251+
}
248252
return;
249253
} else {
250-
rpi_gpio_function_set(MOTCLK_L_BASE, RPI_GPF_ALT0);
254+
if (dev_side == DEV_LEFT) {
255+
rpi_gpio_function_set(MOTCLK_L_BASE, RPI_GPF_ALT0);
256+
} else if (dev_side == DEV_RIGHT) {
257+
rpi_gpio_function_set(MOTCLK_R_BASE, RPI_GPF_ALT0);
258+
}
251259
}
252260

253261
if (freq > 0) {
254-
motor_l_freq_is_positive = 1;
255-
rpi_gpio_clear32(RPI_GPIO_P2MASK, 1 << MOTDIR_L_BASE);
262+
if (dev_side == DEV_LEFT) {
263+
motor_l_freq_is_positive = 1;
264+
rpi_gpio_clear32(RPI_GPIO_P2MASK, 1 << MOTDIR_L_BASE);
265+
} else if (dev_side == DEV_RIGHT) {
266+
motor_r_freq_is_positive = 1;
267+
rpi_gpio_clear32(RPI_GPIO_P2MASK, 1 << MOTDIR_R_BASE);
268+
}
256269
} else {
257-
motor_l_freq_is_positive = 0;
258-
rpi_gpio_set32(RPI_GPIO_P2MASK, 1 << MOTDIR_L_BASE);
259-
freq = -freq;
270+
if (dev_side == DEV_LEFT) {
271+
motor_l_freq_is_positive = 0;
272+
rpi_gpio_set32(RPI_GPIO_P2MASK, 1 << MOTDIR_L_BASE);
273+
freq = -freq;
274+
} else if (dev_side == DEV_RIGHT) {
275+
motor_r_freq_is_positive = 0;
276+
rpi_gpio_set32(RPI_GPIO_P2MASK, 1 << MOTDIR_R_BASE);
277+
freq = -freq;
278+
}
260279
}
261280

262281
dat = getPWMCount(freq);
263282

264-
rpi_pwm_write32(RPI_PWM_RNG1, dat);
265-
rpi_pwm_write32(RPI_PWM_DAT1, dat >> 1);
266-
267-
return;
268-
}
269-
270-
/*
271-
* right motor function
272-
* called by parseMotorCmd() and rawmotor_r_write()
273-
*/
274-
static void set_motor_r_freq(int freq)
275-
{
276-
int dat;
277-
278-
rpi_gpio_function_set(BUZZER_BASE, RPI_GPF_OUTPUT);
279-
280-
// Reset uncontrollable frequency to zero.
281-
if (abs(freq) < MOTOR_UNCONTROLLABLE_FREQ) {
282-
freq = 0;
283-
}
284-
285-
if (freq == 0) {
286-
rpi_gpio_function_set(MOTCLK_R_BASE, RPI_GPF_OUTPUT);
287-
return;
288-
} else {
289-
rpi_gpio_function_set(MOTCLK_R_BASE, RPI_GPF_ALT0);
290-
}
291-
292-
if (freq > 0) {
293-
motor_r_freq_is_positive = 1;
294-
rpi_gpio_set32(RPI_GPIO_P2MASK, 1 << MOTDIR_R_BASE);
295-
} else {
296-
motor_r_freq_is_positive = 0;
297-
rpi_gpio_clear32(RPI_GPIO_P2MASK, 1 << MOTDIR_R_BASE);
298-
freq = -freq;
283+
if (dev_side == DEV_LEFT) {
284+
rpi_pwm_write32(RPI_PWM_RNG1, dat);
285+
rpi_pwm_write32(RPI_PWM_DAT1, dat >> 1);
286+
} else if (dev_side == DEV_RIGHT) {
287+
rpi_pwm_write32(RPI_PWM_RNG2, dat);
288+
rpi_pwm_write32(RPI_PWM_DAT2, dat >> 1);
299289
}
300290

301-
dat = getPWMCount(freq);
302-
303-
rpi_pwm_write32(RPI_PWM_RNG2, dat);
304-
rpi_pwm_write32(RPI_PWM_DAT2, dat >> 1);
305-
306291
return;
307292
}
308293

@@ -326,13 +311,13 @@ static int parseMotorCmd(const char __user *buf, size_t count, int *ret)
326311

327312
mutex_lock(&lock);
328313

329-
set_motor_l_freq(l_motor_val);
330-
set_motor_r_freq(r_motor_val);
314+
set_motor_freq(l_motor_val, DEV_LEFT);
315+
set_motor_freq(r_motor_val, DEV_RIGHT);
331316

332317
msleep_interruptible(time_val);
333318

334-
set_motor_l_freq(0);
335-
set_motor_r_freq(0);
319+
set_motor_freq(0, DEV_LEFT);
320+
set_motor_freq(0, DEV_RIGHT);
336321

337322
mutex_unlock(&lock);
338323

@@ -751,7 +736,7 @@ static ssize_t rawmotor_l_write(struct file *filep, const char __user *buf,
751736
DRIVER_NAME, __func__);
752737
return ret;
753738
}
754-
set_motor_l_freq(freq);
739+
set_motor_freq(freq, DEV_LEFT);
755740

756741
return count;
757742
}
@@ -772,7 +757,7 @@ static ssize_t rawmotor_r_write(struct file *filep, const char __user *buf,
772757
return ret;
773758
}
774759

775-
set_motor_r_freq(freq);
760+
set_motor_freq(freq, DEV_RIGHT);
776761

777762
return count;
778763
}

src/drivers/rtmouse_i2c.c

Lines changed: 36 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -68,86 +68,23 @@ static struct i2c_driver i2c_counter_driver = {
6868
/* -- Device Addition -- */
6969
MODULE_DEVICE_TABLE(i2c, i2c_counter_id);
7070

71-
static int rtcntr_i2c_create_cdev(struct rtcnt_device_info *dev_info)
72-
{
73-
int minor;
74-
int alloc_ret = 0;
75-
int cdev_err = 0;
76-
dev_t dev;
77-
78-
/* 空いているメジャー番号を確保する */
79-
alloc_ret = alloc_chrdev_region(&dev, DEV_MINOR, NUM_DEV[ID_DEV_CNT],
80-
DEVNAME_CNTR);
81-
if (alloc_ret != 0) {
82-
printk(KERN_ERR "alloc_chrdev_region = %d\n", alloc_ret);
83-
return -1;
84-
}
85-
86-
/* 取得したdev( = メジャー番号 + マイナー番号)
87-
* からメジャー番号を取得して保持しておく */
88-
dev_info->device_major = MAJOR(dev);
89-
dev = MKDEV(dev_info->device_major, DEV_MINOR);
90-
91-
/* cdev構造体の初期化とシステムコールハンドラテーブルの登録 */
92-
cdev_init(&dev_info->cdev, &dev_fops[ID_DEV_CNT]);
93-
dev_info->cdev.owner = THIS_MODULE;
94-
95-
/* このデバイスドライバ(cdev)をカーネルに登録する */
96-
cdev_err = cdev_add(&dev_info->cdev, dev, NUM_DEV[ID_DEV_CNT]);
97-
if (cdev_err != 0) {
98-
printk(KERN_ERR "cdev_add = %d\n", alloc_ret);
99-
unregister_chrdev_region(dev, NUM_DEV[ID_DEV_CNT]);
100-
return -1;
101-
}
102-
103-
/* このデバイスのクラス登録をする(/sys/class/mydevice/ を作る) */
104-
#if LINUX_VERSION_CODE < KERNEL_VERSION(6, 4, 0)
105-
dev_info->device_class = class_create(THIS_MODULE, DEVNAME_CNTR);
106-
#else
107-
dev_info->device_class = class_create(DEVNAME_CNTR);
108-
#endif
109-
110-
if (IS_ERR(dev_info->device_class)) {
111-
printk(KERN_ERR "class_create\n");
112-
cdev_del(&dev_info->cdev);
113-
unregister_chrdev_region(dev, NUM_DEV[ID_DEV_CNT]);
114-
return -1;
115-
}
116-
117-
for (minor = DEV_MINOR; minor < DEV_MINOR + NUM_DEV[ID_DEV_CNT];
118-
minor++) {
119-
120-
struct device *dev_ret;
121-
dev_ret = device_create(dev_info->device_class, NULL,
122-
MKDEV(dev_info->device_major, minor),
123-
NULL, "rtcounter_r%d", minor);
124-
125-
/* デバイスファイル作成の可否を判定 */
126-
if (IS_ERR(dev_ret)) {
127-
/* デバイスファイルの作成に失敗した */
128-
printk(KERN_ERR "device_create failed minor = %d\n",
129-
minor);
130-
/* リソースリークを避けるために登録された状態cdevを削除する
131-
*/
132-
cdev_del(&(cdev_array[cdev_index]));
133-
return PTR_ERR(dev_ret);
134-
}
135-
}
136-
137-
return 0;
138-
}
139-
14071
// called by rtcnt_i2c_probe()
141-
static int rtcntl_i2c_create_cdev(struct rtcnt_device_info *dev_info)
72+
static int rtcnt_i2c_create_cdev(struct rtcnt_device_info *dev_info,
73+
const int dev_side)
14274
{
14375
int minor;
14476
int alloc_ret = 0;
14577
int cdev_err = 0;
14678
dev_t dev;
14779

14880
/* 空いているメジャー番号を確保する */
149-
alloc_ret = alloc_chrdev_region(&dev, DEV_MINOR, NUM_DEV[ID_DEV_CNT],
150-
DEVNAME_CNTL);
81+
if (dev_side == DEV_LEFT) {
82+
alloc_ret = alloc_chrdev_region(
83+
&dev, DEV_MINOR, NUM_DEV[ID_DEV_CNT], DEVNAME_CNTL);
84+
} else if (dev_side == DEV_RIGHT) {
85+
alloc_ret = alloc_chrdev_region(
86+
&dev, DEV_MINOR, NUM_DEV[ID_DEV_CNT], DEVNAME_CNTR);
87+
}
15188
if (alloc_ret != 0) {
15289
printk(KERN_ERR "alloc_chrdev_region = %d\n", alloc_ret);
15390
return -1;
@@ -172,9 +109,19 @@ static int rtcntl_i2c_create_cdev(struct rtcnt_device_info *dev_info)
172109

173110
/* このデバイスのクラス登録をする(/sys/class/mydevice/ を作る) */
174111
#if LINUX_VERSION_CODE < KERNEL_VERSION(6, 4, 0)
175-
dev_info->device_class = class_create(THIS_MODULE, DEVNAME_CNTL);
112+
if (dev_side == DEV_LEFT) {
113+
dev_info->device_class =
114+
class_create(THIS_MODULE, DEVNAME_CNTL);
115+
} else if (dev_side == DEV_RIGHT) {
116+
dev_info->device_class =
117+
class_create(THIS_MODULE, DEVNAME_CNTR);
118+
}
176119
#else
177-
dev_info->device_class = class_create(DEVNAME_CNTL);
120+
if (dev_side == DEV_LEFT) {
121+
dev_info->device_class = class_create(DEVNAME_CNTL);
122+
} else if (dev_side == DEV_RIGHT) {
123+
dev_info->device_class = class_create(DEVNAME_CNTR);
124+
}
178125
#endif
179126

180127
if (IS_ERR(dev_info->device_class)) {
@@ -189,9 +136,17 @@ static int rtcntl_i2c_create_cdev(struct rtcnt_device_info *dev_info)
189136
minor++) {
190137

191138
struct device *dev_ret;
192-
dev_ret = device_create(dev_info->device_class, NULL,
193-
MKDEV(dev_info->device_major, minor),
194-
NULL, "rtcounter_l%d", minor);
139+
if (dev_side == DEV_LEFT) {
140+
dev_ret =
141+
device_create(dev_info->device_class, NULL,
142+
MKDEV(dev_info->device_major, minor),
143+
NULL, "rtcounter_l%d", minor);
144+
} else if (dev_side == DEV_RIGHT) {
145+
dev_ret =
146+
device_create(dev_info->device_class, NULL,
147+
MKDEV(dev_info->device_major, minor),
148+
NULL, "rtcounter_r%d", minor);
149+
}
195150

196151
/* デバイスファイル作成の可否を判定 */
197152
if (IS_ERR(dev_ret)) {
@@ -260,10 +215,10 @@ static int rtcnt_i2c_probe(struct i2c_client *client,
260215

261216
/* create character device */
262217
if ((int)(id->driver_data) == 0) {
263-
if (rtcntl_i2c_create_cdev(dev_info))
218+
if (rtcnt_i2c_create_cdev(dev_info, DEV_LEFT))
264219
return -ENOMEM;
265220
} else if ((int)(id->driver_data) == 1) {
266-
if (rtcntr_i2c_create_cdev(dev_info))
221+
if (rtcnt_i2c_create_cdev(dev_info, DEV_RIGHT))
267222
return -ENOMEM;
268223
}
269224

@@ -301,10 +256,10 @@ static int rtcnt_i2c_probe(struct i2c_client *client)
301256

302257
/* create character device */
303258
if ((int)(id->driver_data) == 0) {
304-
if (rtcntl_i2c_create_cdev(dev_info))
259+
if (rtcnt_i2c_create_cdev(dev_info, DEV_LEFT))
305260
return -ENOMEM;
306261
} else if ((int)(id->driver_data) == 1) {
307-
if (rtcntr_i2c_create_cdev(dev_info))
262+
if (rtcnt_i2c_create_cdev(dev_info, DEV_RIGHT))
308263
return -ENOMEM;
309264
}
310265

src/drivers/rtmouse_main.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* rtmouse_main.c
44
* Raspberry Pi Mouse device driver
55
*
6-
* Version: 3.3.3
6+
* Version: 3.3.4
77
*
88
* Copyright (C) 2015-2024 RT Corporation <[email protected]>
99
*
@@ -26,7 +26,7 @@
2626

2727
MODULE_AUTHOR("RT Corporation");
2828
MODULE_LICENSE("GPL");
29-
MODULE_VERSION("3.3.3");
29+
MODULE_VERSION("3.3.4");
3030
MODULE_DESCRIPTION("Raspberry Pi Mouse device driver");
3131

3232
/*

0 commit comments

Comments
 (0)