Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of T_motor by
T_motor.cpp
- Committer:
- fujikenac
- Date:
- 2017-09-21
- Revision:
- 7:a1d54597161d
- Parent:
- 6:dc3db7df2a4b
- Child:
- 8:2759e619969a
File content as of revision 7:a1d54597161d:
#include "mbed.h"
#include "T_motor.h"
void T_motor::setAddr(int addr_)
{
addr = addr_;
}
void T_motor::init(char addr,int freq = 100000)
{
setAddr(addr);
i2c.frequency(freq);
char d = 0;
i2c.write((addr+1) << 1,&d,1);
wait(0.01);
}
T_motor::T_motor(PinName sda,PinName scl,int addr):i2c(sda,scl)
{
init(addr);
}
T_motor::T_motor(I2C& i2c_,int addr):i2c(i2c_)
{
init(addr);
}
T_motor& T_motor::operator=(float fval)// -1 <= fval <= 1
{
if(fabs(fval) < 0.005f)free();
else {
if(fval < 0) run(1,char(-1*fval*255));
else if(fval > 0) run(0,char(fval*255));
}
return *this;
}
void T_motor::control(float fval)//copy from operator
{
if(fabs(fval) < 0.005f)free();
else {
if(fval < 0) run(1,char(-1*fval*255));
else if(fval > 0) run(0,char(fval*255));
}
}
bool T_motor::run(char mode,char speed)
{
bool f = 0;
char d = speed;
if(mode == 0 || mode == 1)f = i2c.write((addr+mode) << 1,&d,1);
wait(0.01);
return f;
}
bool T_motor::stop()
{
return run(1,0);
}
bool T_motor::free()
{
return run(0,0);
}
