![](/media/cache/group/site_id.gif.50x50_q85.jpg)
トランジスタ技術2015年6月号 mbed ラジコン・カーp169-p184掲載
Dependencies: mbed
Diff: ATP3012.cpp
- Revision:
- 0:6228c3688e54
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ATP3012.cpp Mon Jun 01 02:03:38 2015 +0000 @@ -0,0 +1,52 @@ +#include "ATP3012.h" + +ATP3012::ATP3012(PinName sda, PinName scl, int addr):_i2c(sda, scl) +{ + _addr = addr; + _poll_wait.reset(); + _poll_wait.start(); +} + +bool ATP3012::IsActive(int timeout_ms) +{ + wait_ms(AQTK_STARTUP_WAIT_MS); + Timer t; + t.reset(); + t.start(); + while(t.read_ms() < timeout_ms) { + _poll_wait.reset(); + if (_i2c.write(_addr, NULL, 0) == 0) { + return true; + } + wait_ms(AQTK_POLL_WAIT_MS); + } + return false; +} + +void ATP3012::Synthe(const char* msg) +{ + while(IsBusy()) { + ; + } + Write(msg); + Write("\r"); +} + +void ATP3012::Write(const char *msg) +{ + _i2c.write(_addr, msg, strlen(msg)); + _poll_wait.reset(); +} + +bool ATP3012::IsBusy() +{ + if (AQTK_POLL_WAIT_MS > _poll_wait.read_ms()) { + return true; + } + _poll_wait.reset(); + char c = 0x00; + if (_i2c.read(_addr, &c, 1) != 0) { + return false; + } + return c == '*' || c == 0xff; +}