トランジスタ技術2015年6月号 mbed ラジコン・カーp169-p184掲載

Dependencies:   mbed

Committer:
YoshihitoShimada
Date:
Mon Jun 01 02:03:38 2015 +0000
Revision:
0:6228c3688e54
mbed test sample code

Who changed what in which revision?

UserRevisionLine numberNew contents of line
YoshihitoShimada 0:6228c3688e54 1 #include "ATP3012.h"
YoshihitoShimada 0:6228c3688e54 2
YoshihitoShimada 0:6228c3688e54 3 ATP3012::ATP3012(PinName sda, PinName scl, int addr):_i2c(sda, scl)
YoshihitoShimada 0:6228c3688e54 4 {
YoshihitoShimada 0:6228c3688e54 5 _addr = addr;
YoshihitoShimada 0:6228c3688e54 6 _poll_wait.reset();
YoshihitoShimada 0:6228c3688e54 7 _poll_wait.start();
YoshihitoShimada 0:6228c3688e54 8 }
YoshihitoShimada 0:6228c3688e54 9
YoshihitoShimada 0:6228c3688e54 10 bool ATP3012::IsActive(int timeout_ms)
YoshihitoShimada 0:6228c3688e54 11 {
YoshihitoShimada 0:6228c3688e54 12 wait_ms(AQTK_STARTUP_WAIT_MS);
YoshihitoShimada 0:6228c3688e54 13 Timer t;
YoshihitoShimada 0:6228c3688e54 14 t.reset();
YoshihitoShimada 0:6228c3688e54 15 t.start();
YoshihitoShimada 0:6228c3688e54 16 while(t.read_ms() < timeout_ms) {
YoshihitoShimada 0:6228c3688e54 17 _poll_wait.reset();
YoshihitoShimada 0:6228c3688e54 18 if (_i2c.write(_addr, NULL, 0) == 0) {
YoshihitoShimada 0:6228c3688e54 19 return true;
YoshihitoShimada 0:6228c3688e54 20 }
YoshihitoShimada 0:6228c3688e54 21 wait_ms(AQTK_POLL_WAIT_MS);
YoshihitoShimada 0:6228c3688e54 22 }
YoshihitoShimada 0:6228c3688e54 23 return false;
YoshihitoShimada 0:6228c3688e54 24 }
YoshihitoShimada 0:6228c3688e54 25
YoshihitoShimada 0:6228c3688e54 26 void ATP3012::Synthe(const char* msg)
YoshihitoShimada 0:6228c3688e54 27 {
YoshihitoShimada 0:6228c3688e54 28 while(IsBusy()) {
YoshihitoShimada 0:6228c3688e54 29 ;
YoshihitoShimada 0:6228c3688e54 30 }
YoshihitoShimada 0:6228c3688e54 31 Write(msg);
YoshihitoShimada 0:6228c3688e54 32 Write("\r");
YoshihitoShimada 0:6228c3688e54 33 }
YoshihitoShimada 0:6228c3688e54 34
YoshihitoShimada 0:6228c3688e54 35 void ATP3012::Write(const char *msg)
YoshihitoShimada 0:6228c3688e54 36 {
YoshihitoShimada 0:6228c3688e54 37 _i2c.write(_addr, msg, strlen(msg));
YoshihitoShimada 0:6228c3688e54 38 _poll_wait.reset();
YoshihitoShimada 0:6228c3688e54 39 }
YoshihitoShimada 0:6228c3688e54 40
YoshihitoShimada 0:6228c3688e54 41 bool ATP3012::IsBusy()
YoshihitoShimada 0:6228c3688e54 42 {
YoshihitoShimada 0:6228c3688e54 43 if (AQTK_POLL_WAIT_MS > _poll_wait.read_ms()) {
YoshihitoShimada 0:6228c3688e54 44 return true;
YoshihitoShimada 0:6228c3688e54 45 }
YoshihitoShimada 0:6228c3688e54 46 _poll_wait.reset();
YoshihitoShimada 0:6228c3688e54 47 char c = 0x00;
YoshihitoShimada 0:6228c3688e54 48 if (_i2c.read(_addr, &c, 1) != 0) {
YoshihitoShimada 0:6228c3688e54 49 return false;
YoshihitoShimada 0:6228c3688e54 50 }
YoshihitoShimada 0:6228c3688e54 51 return c == '*' || c == 0xff;
YoshihitoShimada 0:6228c3688e54 52 }