Skip to content

Commit 07a15d3

Browse files
committed
set_motor_l_*とset_motor_r_*を一つの関数にまとめる
1 parent c482fcf commit 07a15d3

File tree

1 file changed

+39
-55
lines changed

1 file changed

+39
-55
lines changed

src/drivers/rtmouse_dev.c

Lines changed: 39 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,49 @@ 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+
} else if (dev_side == DEV_RIGHT) {
274+
motor_r_freq_is_positive = 0;
275+
rpi_gpio_set32(RPI_GPIO_P2MASK, 1 << MOTDIR_R_BASE);
276+
freq = -freq;
277+
}
260278
}
261279

262280
dat = getPWMCount(freq);
263281

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;
282+
if (dev_side == DEV_LEFT) {
283+
rpi_pwm_write32(RPI_PWM_RNG1, dat);
284+
rpi_pwm_write32(RPI_PWM_DAT1, dat >> 1);
285+
} else if (dev_side == DEV_RIGHT) {
286+
rpi_pwm_write32(RPI_PWM_RNG2, dat);
287+
rpi_pwm_write32(RPI_PWM_DAT2, dat >> 1);
299288
}
300289

301-
dat = getPWMCount(freq);
302-
303-
rpi_pwm_write32(RPI_PWM_RNG2, dat);
304-
rpi_pwm_write32(RPI_PWM_DAT2, dat >> 1);
305-
306290
return;
307291
}
308292

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

327311
mutex_lock(&lock);
328312

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

332316
msleep_interruptible(time_val);
333317

334-
set_motor_l_freq(0);
335-
set_motor_r_freq(0);
318+
set_motor_freq(0, DEV_LEFT);
319+
set_motor_freq(0, DEV_RIGHT);
336320

337321
mutex_unlock(&lock);
338322

@@ -751,7 +735,7 @@ static ssize_t rawmotor_l_write(struct file *filep, const char __user *buf,
751735
DRIVER_NAME, __func__);
752736
return ret;
753737
}
754-
set_motor_l_freq(freq);
738+
set_motor_freq(freq, DEV_LEFT);
755739

756740
return count;
757741
}
@@ -772,7 +756,7 @@ static ssize_t rawmotor_r_write(struct file *filep, const char __user *buf,
772756
return ret;
773757
}
774758

775-
set_motor_r_freq(freq);
759+
set_motor_freq(freq, DEV_RIGHT);
776760

777761
return count;
778762
}

0 commit comments

Comments
 (0)