# C++ API
The C++ API, available via the header ft.hpp, implements classes for all devices defined by the C interface.
| Classes |
| ------------------------------- |
| [Device](#device) |
| [InputDevice](#inputdevice) |
| [Switch](#switch) |
| [Digital](#digital) |
| [Resistor](#resistor) |
| [Voltmeter](#voltmeter) |
| [Ultrasonic](#ultrasonic) |
| [NCTTermistor](#ncttermistor) |
| [ColorSensor](#colorsensor) |
| [TrailFollower](#trailfollower) |
| [Counter](#counter) |
| [Lamp](#lamp) |
| [Servo](#servo) |
| [MotorDevice](#motordevice) |
| [Motor](#motor) |
| [Encoder](#encoder) |
| [TXT](#txt) |
## Device
Base class for all devices.
### Methods
int getConn()
Returns the device's connection number. See [ftobjects_connection](C_fttypes.md#ftobjects_connection)
for details.
## InputDevice
Extends Device
Base class for various sensors.
### Methods
Returns the 'state' for universal input devices. For digital devices, the state is
returned, for analog devices, the analog value is returned and for counters the
current step count is returned.
Sets the inputs detection range. Pass *0* for normal detection range (0 - 5 V) and *1* for extended range (0 - 9 V).
| Parameter | Description |
| --------------- | --------------- |
| mode | input range |
## Switch
Extends InputDevice
Implementation of a generic [switch](devices.md#Switch).
### Methods
Switch(TXT &txt, int input)
Constructor for the Switch class. Takes a reference to a TXT instance and an input pin number as arguments.
For the switch instance to be usable, one has to call the *update_config* method on the TXT.
*Note*: The pin number starts at 1!
| Parameter | Description |
| --------------- | --------------- |
| txt | reference to TXT object |
| input | pin number [*1-8*] |
## Digital
Extends InputDevice
Implementation of a generic [digital sensor](devices.md#digital).
### Methods
Digital(TXT &txt, int input)
Constructor for the Digital class. Takes a reference to a TXT instance and an input pin number as arguments.
For the digital instance to be usable, one has to call the *update_config* method on the TXT.
*Note*: The pin number starts at 1!
| Parameter | Description |
| --------------- | --------------- |
| txt | reference to TXT object |
| input | pin number [*1-8*] |
## Resistor
Extends InputDevice
Implementation of a generic [resistor](devices.md#resistor).
### Methods
Resistor(TXT &txt, int input)
Constructor for the Resistor class. Takes a reference to a TXT instance and an input pin number as arguments.
For the resistor instance to be usable, one has to call the *update_config* method on the TXT.
*Note*: The pin number starts at 1!
| Parameter | Description |
| --------------- | --------------- |
| txt | reference to TXT object |
| input | pin number [*1-8*] |
int getResistance()
Calculates and returns the resistance in *Ω*.
## Voltmeter
Extends InputDevice
Implementation of a generic [voltmeter](devices.md#voltmeter).
### Methods
Voltmeter(TXT &txt, int input)
Constructor for the Voltmeter class. Takes a reference to a TXT instance and an input pin number as arguments.
For the voltmeter instance to be usable, one has to call the *update_config* method on the TXT.
*Note*: The pin number starts at 1!
| Parameter | Description |
| --------------- | --------------- |
| txt | reference to TXT object |
| input | pin number [*1-8*] |
int getVoltage()
Returns the voltage in *mV*.
## Ultrasonic
Extends InputDevice
Implementation of fischertechnik's [ultrasonic distance sensor](devices.md#ultrasonic).
### Methods
Ultrasonic(TXT &txt, int input)
Constructor for the Ultrasonic class. Takes a reference to a TXT instance and an input pin number as arguments.
For the ultrasonic instance to be usable, one has to call the *update_config* method on the TXT.
*Note*: The pin number starts at 1!
| Parameter | Description |
| --------------- | --------------- |
| txt | reference to TXT object |
| input | pin number [*1-8*] |
int getDistance()
Returns the current distance in *cm*.
## NCTTermistor
Extends InputDevice
Implementation of the fischertechnik 1,5 kΩ [NTC](devices.md#ntc-thermistor).
### Methods
NCTTermistor(TXT &txt, int input)
Constructor for the NCTTermistor class. Takes a reference to a TXT instance and an input pin number as arguments.
For the ncttermistor instance to be usable, one has to call the *update_config* method on the TXT.
*Note*: The pin number starts at 1!
| Parameter | Description |
| --------------- | --------------- |
| txt | reference to TXT object |
| input | pin number [*1-8*] |
int getTemperature()
Calculates and returns the temperature in *°C* for the fischertechnik 1,5 *kΩ* NTC using the beta-model.
*Note*: This calculation provides correct results for **THIS** NTC only. For different thermistors,
the user must implement their own function to calculate the temperature from the resistance.
int getResistance()
Calculates and returns the resistance in *Ω*.
## ColorSensor
Extends InputDevice
Implementation of the fischertechnik [color sensor](devices.md#colorsensor).
*Note*: The color sensor does NOT implement a special functions to return the color;
use *getState()* to obtain the analog value in *mV*.
### Methods
ColorSensor(TXT &txt, int input)
Constructor for the ColorSensor class. Takes a reference to a TXT instance and an input pin number as arguments.
For the color sensor instance to be usable, one has to call the *update_config* method on the TXT.
*Note*: The pin number starts at 1!
| Parameter | Description |
| --------------- | --------------- |
| txt | reference to TXT object |
| input | pin number [*1-8*] |
## TrailFollower
Extends InputDevice
Implenentation of fischertechnik's [trail sensor](devices.md#trailfollower).
### Methods
ColorSensor(TXT &txt, int input)
Constructor for the TrailFollower class. Takes a reference to a TXT instance and an input pin number as arguments.
For the trail follower instance to be usable, one has to call the *update_config* method on the TXT.
*Note*: The pin number starts at 1!
| Parameter | Description |
| --------------- | --------------- |
| txt | reference to TXT object |
| input | pin number [*1-8*] |
## Counter
Extends Device
Implementation of a fast [counter](devices.md#counter) device.
### Methods
Counter(TXT &txt, int input)
Constructor for the Counter class. Takes a reference to a TXT instance and an input pin number as arguments.
For the counter instance to be usable, one has to call the *update_config* method on the TXT.
*Note*: The pin number starts at 1!
| Parameter | Description |
| --------------- | --------------- |
| txt | reference to TXT object |
| input | pin number [*1-4*] |
int getState()
Returns the counter's state. Returns *0* if the switch is pressed (cuircit is closed) and *1* if the
switch is released (cuircit is open).
int getDistance()
Returns the distance. The distance is the number of pulses (counts) since the last reset.
int reset()
Resets the count number.
*Note*: This reset is not in effect immediately since this function requires communication with the M4.
## Lamp
Extends Device
Implementation of an [output](devices.md#output-lamp) device.
### Methods
Lamp(TXT &txt, int output)
Constructor for the Lamp class. Takes a reference to a TXT instance and an output pin number as arguments.
For the lamp instance to be usable, one has to call the *update_config* method on the TXT.
*Note*: The pin number starts at 1!
| Parameter | Description |
| --------------- | --------------- |
| txt | reference to TXT object |
| output | pin number [*1-8*] |
void setBrightness(unsigned int brightness)
Sets the PWM value (brightness) for outputs.
| Parameter | Description |
| --------------- | --------------- |
| brightness | new PWM value [*0, 512*] |
## Servo
Extends Device
Implementation of a [servo motor](devices.md#servo).
### Methods
Servo(TXT &txt, int output)
Constructor for the Servo class. Takes a reference to a TXT instance and an output pin number as arguments.
For the servo instance to be usable, one has to call the *update_config* method on the TXT.
*Note*: The pin number starts at 1!
| Parameter | Description |
| --------------- | --------------- |
| txt | reference to TXT object |
| output | pin number [*1-3*] |
void setPwm(int pwm)
Sets the PWM value for servo. PWM = *256* sets the servo to its central position.
| Parameter | Description |
| --------------- | --------------- |
| pwm | new PWM value [*0, 512*] |
## MotorDevice
Extends Device
Base class for motors.
### Methods:
void setSpeed(int speed)
Sets a new speed value for the motor but **DOES NOT** start the motor.
| Parameter | Description |
| --------------- | --------------- |
| speed | new speed value [*-512, 512*] |
void startSpeed(int speed)
Starts the motor immediately at new speed.
| Parameter | Description |
| --------------- | --------------- |
| speed | new speed value [*-512, 512*] |
void stop()
Stops the motor immediately.
int getMotor()
Returns the motor number [*0-3*].
void coast()
Sets the motor in coast mode. This will not stop the motor immediately, but it will be turned off
with the next update cycle of the M4.
## Motor
Extends MotorDevice
Implementation of a regular [motor](devices.md#motor).
### Methods
Motor(TXT &txt, int output)
Constructor for the Motor class. Takes a reference to a TXT instance and an output pin number as arguments.
For the motor instance to be usable, one has to call the *update_config* method on the TXT.
*Note*: The pin number starts at 1!
| Parameter | Description |
| --------------- | --------------- |
| txt | reference to TXT object |
| output | pin number [*1-4*] |
## Encoder
Extends MotorDevice
Implementation of an [encoder motor](devices.md#encoder).
### Methods
Encoder(TXT &txt, int output)
Constructor for the Encoder class. Takes a reference to a TXT instance and an output pin number as arguments.
The encoders counter has to be connected to the counter pin with the same number as the output
pin. For the encoder instance to be usable, one has to call the *update_config* method on the TXT.
*Note*: The pin number starts at 1!
| Parameter | Description |
| --------------- | --------------- |
| txt | reference to TXT object |
| output | pin number [*1-4*] |
void startDistance(int distance, Encoder *e = 0)
Starts the encoder immediately to run for the given distance. A pointer to a second encoder
instance can be passed optionally. If the second encoder is provided, both motors run
sychronously.
The speed at which the motor(s) run must be set in advance by calling the
*setSpeed* method.
| Parameter | Description |
| --------------- | --------------- |
| distance | distance in impulses |
| e | optional pointer for 2nd encoder |
void startDistance(int distance, int mask)
Starts the encoders immediately to run synchronously for the given distance. The
encoders to be controlled are specified by the bit mask. The lowest 4 bits are
used to control the motor. If the n-th bit is 1, the encoder connected at the n-th
motor pin will be controlled. If the n-th bit is 0, the motor is ignored.
The speed at which the motor(s) run must be set in advance by calling the
*setSpeed* method.
| Parameter | Description |
| --------------- | --------------- |
| distance | distance in impulses |
| mask | bit mask of up to 4 encoder motors |
void stop(int mask)
Stops all motors selected by the bit mask immediatly.
| Parameter | Description |
| --------------- | --------------- |
| mask | bit mask of up to 4 encoder motors |
int getCurrentDistance()
Returns the remaining distance in impulses to finish the current action.
bool isRunning()
Returns whether or not the [encoder motor](devices.md#encoder) is currently running.
*Note*: This **DOES NOT** mean the motor's cureent speed is non-zero! It checks
whether or not the last motor command has been excecuted or not.
## TXT
Implementation of the TXT 4.0.
### Methods
TXT(const std::string &host = "auto", int port = 65000)
Constructor for the TXT class. The pararmeters *host* and *port* have default
arguments and don't need to be provided. As of now, they are discarded without
being used!
| Parameter | Description |
| --------------- | --------------- |
| host | optional host string (has no usage) |
| port | optional port number (has no usage) |
TXT(TXT &txt, int slave)
Constructor for the TXT class. Used to construct a slave TXT. Takes a reference to the master TXT and a slave number
as arguments.
*Note*: The slave number is *NOT* the number set in the settings of the controller!
The TXTs are numbered by the order they are connected to the network!
| Parameter | Description |
| --------------- | --------------- |
| txt | reference to master TXT |
| slave | slave number |
std::string getName()
Returns the TXT name.
int getVersion()
Returns the TXT version.
int getFirmware()
Returns the firmware version.
void setMode(int mode)
Sets or changes the TXT's mode. Pass *0* for single mode, *1* for master mode
and *2* for slave mode (see [txt mode](c_fttypes.md#ft_txt_mode) enum).
| Parameter | Description |
| --------------- | --------------- |
| mode | new TXT mode |
void setName(const std::string &name)
Sets new name for the TXT. A name is a 16 character string. The name can shorter.
| Parameter | Description |
| --------------- | --------------- |
| name | new name |
int getSlavesNumber()
Returns the number of slaves attached to this TXT.
uint_32 getSlaveUID(int number)
Returns the uid of the selected slave. Slave with the number *0* is the master TXT.
*Note*: The slave number is *NOT* the number set in the settings of the controller!
The TXTs are numbered by the order they are connected to the network!
| Parameter | Description |
| --------------- | --------------- |
| number | slave number |
std::string getSlaveName(int number)
Returns the name of the selected slave. Slave with the number *0* is the master TXT.
*Note*: The slave number is *NOT* the number set in the settings of the controller!
The TXTs are numbered by the order they are connected to the network!
| Parameter | Description |
| --------------- | --------------- |
| number | slave number |
std::string getSlaveSerialNumber(int number)
Returns the serial number of the selected slave. Slave with the number *0* is the master TXT.
*Note*: The slave number is *NOT* the number set in the settings of the controller!
The TXTs are numbered by the order they are connected to the network!
| Parameter | Description |
| --------------- | --------------- |
| number | slave number |
Lamp *getLamp(int output)
Allocates a new [Lamp](#lamp) instance on the heap connected to the pin specified by *output* and returns the pointer.
*Note*: The instance is *NOT* allocated as a smart pointer! To avoid memory leaks, you have to delete the pointer
manually when it's no longer needed.
| Parameter | Description |
| --------------- | --------------- |
| output | output pin number [*1-8*] |
Motor *getMotor(int output)
Allocates a new [Motor](#motor) instance on the heap connected to the pin specified by *output* and returns the pointer.
*Note*: The instance is *NOT* allocated as a smart pointer! To avoid memory leaks, you have to delete the pointer
manually when it's no longer needed.
| Parameter | Description |
| --------------- | --------------- |
| output | motor pin number [*1-4*] |
Encoder *getEncoder(int output)
Allocates a new [Encoder](#encoder) instance on the heap connected to the pin specified by *output* and returns the pointer.
The counter must be connected to the same pin number as the motor. (e.g. motor connected to M1, counter connected to C1).
*Note*: The instance is *NOT* allocated as a smart pointer! To avoid memory leaks, you have to delete the pointer
manually when it's no longer needed.
| Parameter | Description |
| --------------- | --------------- |
| output | motor & counter pin number [*1-4*] |
Servo *getServo(int output)
Allocates a new [Servo](#servo) instance on the heap connected to the pin specified by *output* and returns the pointer.
*Note*: The instance is *NOT* allocated as a smart pointer! To avoid memory leaks, you have to delete the pointer
manually when it's no longer needed.
| Parameter | Description |
| --------------- | --------------- |
| output | servo pin number [*1-3*] |
Resistor *getResistor(int input)
Allocates a new [Resistor](#resistor) instance on the heap connected to the pin specified by *input* and returns the pointer.
*Note*: The instance is *NOT* allocated as a smart pointer! To avoid memory leaks, you have to delete the pointer
manually when it's no longer needed.
| Parameter | Description |
| --------------- | --------------- |
| input | universal input pin number [*1-8*] |
NCTTermistor *getNCTTermistor(int input)
Allocates a new [NCTTermistor](#ncttermistor) instance on the heap connected to the pin specified
by *input* and returns the pointer.
*Note*: The instance is *NOT* allocated as a smart pointer! To avoid memory
leaks, you have to delete the pointer manually when it's no longer needed.
| Parameter | Description |
| --------------- | --------------- |
| input | universal input pin number [*1-8*] |
Switch *getSwitch(int input)
Allocates a new [Switch](#switch) instance on the heap connected to the pin specified by
*input* and returns the pointer.
*Note*: The instance is *NOT* allocated as a smart pointer! To avoid memory
leaks, you have to delete the pointer manually when it's no longer needed.
| Parameter | Description |
| --------------- | --------------- |
| input | universal input pin number [*1-8*] |
Digital *getDigital(int input)
Allocates a new [Digital](#digital) instance on the heap connected to the pin specified by
*input* and returns the pointer.
*Note*: The instance is *NOT* allocated as a smart pointer! To avoid memory
leaks, you have to delete the pointer manually when it's no longer needed.
| Parameter | Description |
| --------------- | --------------- |
| input | universal input pin number [*1-8*] |
Voltmeter *getVoltmeterint input)
Allocates a new [Voltmeter](#voltmeter) instance on the heap connected to the pin specified
by *input* and returns the pointer.
*Note*: The instance is *NOT* allocated as a smart pointer! To avoid memory
leaks, you have to delete the pointer manually when it's no longer needed.
| Parameter | Description |
| --------------- | --------------- |
| input | universal input pin number [*1-8*] |
ColorSensor *getColorSensor(int input)
Allocates a new [ColorSensor](#colorsensor) instance on the heap connected to the pin specified
by *input* and returns the pointer.
*Note*: The instance is *NOT* allocated as a smart pointer! To avoid memory
leaks, you have to delete the pointer manually when it's no longer needed.
| Parameter | Description |
| --------------- | --------------- |
| input | universal input pin number [*1-8*] |
TrailFollower *getTrailFollower(int input)
Allocates a new [TrailFollower](#trailfollower) instance on the heap connected to the pin
specified by *input* and returns the pointer.
*Note*: The instance is *NOT* allocated as a smart pointer! To avoid memory
leaks, you have to delete the pointer manually when it's no longer needed.
| Parameter | Description |
| --------------- | --------------- |
| input | universal input pin number [*1-8*] |
Ultrasonic *getUltrasonic(int input)
Allocates a new [Ultrasonic](#ultrasonic) instance on the heap connected to the pin specified
by *input* and returns the pointer.
*Note*: The instance is *NOT* allocated as a smart pointer! To avoid memory
leaks, you have to delete the pointer manually when it's no longer needed.
| Parameter | Description |
| --------------- | --------------- |
| input | universal input pin number [*1-8*] |
Counter *getCounter(int input)
Allocates a new [Counter](#counter) instance on the heap connected to the pin specified by
*input* and returns the pointer.
*Note*: The instance is *NOT* allocated as a smart pointer! To avoid memory
leaks, you have to delete the pointer manually when it's no longer needed.
| Parameter | Description |
| --------------- | --------------- |
| input | counter pin number [*1-4*] |
void update_config()
Updates the TXT's input/output configuration. This function must be called
after allocating new sensors and actuators for them to become usable. Since
this functions requires communication with the M4, the update is not in effect
immediatly after the function returns, so a sleep might be used to make sure
the peripheral is usuable. For updating the master TXT, 10 *ms* is sufficient,
updating slave TXT takes up to 70 *ms*.
void setBitrate(int bitrate)
Sets a new CAN bitrate.
| Parameter | Description |
| --------------- | --------------- |
| bitrate | new CAN bitrate (in Hz) |
void setTerminator(int mode)
Toggels whether or not to use a termitator for FDCAN.
mode = *0* disabels the terminator, mode = *1* enabels it.
| Parameter | Description |
| --------------- | --------------- |
| mode | new mode |
void powerDown()
Powers down the M4.
void reset()
Resets the M4. As of now, this method does nothing.