Motor.H
Go to the documentation of this file.
1 
7 // The contents of this file are subject to the Mozilla Public License
8 // Version 1.0 (the "License"); you may not use this file except in
9 // compliance with the License. You may obtain a copy of the License
10 // at http://www.mozilla.org/MPL/
11 //
12 // Software distributed under the License is distributed on an "AS IS"
13 // basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
14 // the License for the specific language governing rights and
15 // limitations under the License.
16 
17 #ifndef _Motor_H_
18 #define _Motor_H_
19 
20 #include <config.h>
21 
22 #if defined(CONF_DMOTOR)
23 
24 #include <conio.h> // for the delay() function
25 #include <dmotor.h>
26 
38 class Motor {
39 public:
43  enum Port {
44  A,
45  B,
46  C
47  };
48 
52  enum Limits {
53  min = 0,
54  max = 255
55  };
56 
61  Motor(const Port port)
62  : ms(port == A ? motor_a_speed :
63  (port == B) ? motor_b_speed :
64  motor_c_speed),
65  md(port == A ? motor_a_dir :
66  (port == B) ? motor_b_dir :
68  { }
73  ~Motor() {off();}
80  const void speed(const unsigned char speed) const { (*ms)(speed); }
86  const void direction(const MotorDirection dir) const { (*md)(dir); }
91  const void forward() const { direction(fwd); }
98  const void forward(const unsigned char s) const { forward(); speed(s); }
103  const void reverse() const { direction(rev); }
110  const void reverse(const unsigned char s) const { reverse(); speed(s); }
116  const void brake() const { direction(::brake); }
123  const void brake(int duration) const { brake(); delay(duration); }
129  const void off() const { direction(::off); }
130 
131 private:
132  void (*ms)(unsigned char speed);
133  void (*md)(const MotorDirection dir);
134 };
135 
136 #else // CONF_DMOTOR
137 #warning Enable CONF_DMOTOR to use Motor.H
138 #endif // CONF_DMOTOR
139 #endif // _Motor_H_
const void forward(const unsigned char s) const
set motor direction to forward at speed {s}
Definition: Motor.H:98
const void direction(const MotorDirection dir) const
set the motor direction
Definition: Motor.H:86
const void brake(int duration) const
set the motor to brake and delay return
Definition: Motor.H:123
const void forward() const
set motor direction to forward
Definition: Motor.H:91
void motor_a_speed(unsigned char speed)
set motor A speed
Definition: dmotor.h:143
void motor_b_dir(MotorDirection dir)
set motor B direction to dir
Interface: console input / output.
Limits
motor speed range
Definition: Motor.H:52
Motor(const Port port)
construct a motor connected to {port}
Definition: Motor.H:61
void(* ms)(unsigned char speed)
current velocity setting for this motor instance
Definition: Motor.H:132
~Motor()
destroy our motor instance
Definition: Motor.H:73
kernel configuration file
const void speed(const unsigned char speed) const
set the motor speed
Definition: Motor.H:80
const void reverse(const unsigned char s) const
set the motor direction to reverse at speed {s}
Definition: Motor.H:110
void motor_c_dir(MotorDirection dir)
set motor C direction to dir
Minimum velocity (0)
Definition: Motor.H:53
void(* md)(const MotorDirection dir)
current direction setting for this motor instance
Definition: Motor.H:133
const void off() const
turn the motor off this disables power and the motor coasts to a stop
Definition: Motor.H:129
reverse
Definition: dmotor.h:47
void delay(unsigned ms)
uncalibrated delay loop
Definition: conio.c:204
Maximum velocity (255)
Definition: Motor.H:54
RCX output pad A.
Definition: Motor.H:44
RCX output pad C.
Definition: Motor.H:46
void motor_a_dir(MotorDirection dir)
set motor A direction to dir
const void brake() const
set the motor to brake
Definition: Motor.H:116
Motor control interface.
Definition: Motor.H:38
const void reverse() const
set the motor direction to reverse
Definition: Motor.H:103
Port
valid port designators
Definition: Motor.H:43
MotorDirection
the motor directions
Definition: dmotor.h:44
RCX output pad B.
Definition: Motor.H:45
void motor_b_speed(unsigned char speed)
set motor B speed
Definition: dmotor.h:151
forward
Definition: dmotor.h:46
void motor_c_speed(unsigned char speed)
set motor C speed
Definition: dmotor.h:159

brickOS is released under the Mozilla Public License.
Original code copyright 1998-2005 by the authors.

Generated for brickOS Kernel Developer by doxygen 1.8.12