田中くんのモタドラを制御する石本さんのやつ
Diff: T_motor.cpp
- Revision:
- 0:e10d08530490
- Child:
- 1:5bd161b83ce0
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/T_motor.cpp Sun Aug 13 02:08:40 2017 +0000 @@ -0,0 +1,57 @@ +#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) +{ + if(fabs(fval) < 0.1f)free(); + else { + if(fval < 0) run(1,char(-1*fval*255)); + else if(fval > 0) run(0,char(fval*255)); + } + return *this; +} + +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); +} +