TI-Nspire & Arduino、サーボを使う 2、シリアルポートを使って TI-Nspire で角度をモニタリングする

Arduino 側では前回と同じことを実行するが、TI-Nspire で角度がモニタリングできるようにする。


.ino

#include <Servo.h>
#define sigPin 9
Servo myservo;

int pos = 0;

void setup() {
  Serial.begin(115200);
  myservo.attach(sigPin);
}

void loop() {
  while(pos < 180) {
    if(Serial.available() && Serial.read() == 'T') {
      Serial.print(pos);
      myservo.write(pos);
      pos = pos + 1;
      delay(15);
    }
  }
  while(pos > 0) {
    if(Serial.available() && Serial.read() == 'T') {
      Serial.print(pos);
      myservo.write(pos);
      pos = pos - 1;
      delay(15);
    }
  }
}


.lua

require "asi" require "color"
platform.window:setBackgroundColor(color.dodgerblue)

function on.resize() 
   W, H = platform.window:width(), platform.window:height()
   X0, Y0 = W/2, H * 0.9
   UNIT = W/10
   fontSize = W/13.25
end
function polar2xy(r, deg)
   local rad = math.rad(deg)
   return r * math.cos(rad), r * math.sin(rad)
end
function xy2screen(x, y, x0, y0, unit)
   return x0 + x * unit, y0 - y * unit
end
function polar2screen(r, deg, x0, y0, unit)
   local x, y = polar2xy(r, deg)
   return xy2screen(x, y, x0, y0, unit)
end

local PORT
local DATA = 0

-----------------------------------
-- シリアルデータの読み取り要求函数
-----------------------------------
function requestData()
      PORT:write('T')
      PORT:read()
end

--------------------------------------------------------------------------------
-- ASI ステートリスナーを定義する(ASI の準備が整ったらポートスキャンを開始する)
--------------------------------------------------------------------------------
function stateListener(state) 
   if state == asi.ON then 
      asi.startScanning(portScanner)
   end
end

----------------------------------------------------------------
-- ポートスキャナーを定義する(見つかったポートに接続要求を出す)
----------------------------------------------------------------
function portScanner(port) 
   port:connect(portConnector) 
end

-----------------------------------------------------------------------------------------------------
-- ポートコネクターを定義する
-----------------------------------------------------------------------------------------------------
function portConnector(port, event)  
   PORT = port
   if event == asi.CONNECTED then
      asi.stopScanning()
      requestData() 
      port:setReadListener(readListener)
   end 
end

----------------------------------------------------------------------------
-- 読み取りリスナーを定義する(データを取得し、またデータ読み取り要求を出す) 
----------------------------------------------------------------------------
function readListener(port)
   DATA = port:getValue() or 0  
   platform.window:invalidate()
   requestData() 
end

---------------------------------
-- ASI ステートリスナーを登録する
---------------------------------
function on.construction() 
   asi.addStateListener(stateListener)
end

-------------------------------------------------------------------------------
function on.paint(gc)
   gc:setFont("sansserif", "r", fontSize)
   gc:setColorRGB(color.white)
   gc:drawString("  "..DATA.."°", 0, 0)
   gc:setPen("thick")
   gc:drawLine(X0, Y0, polar2screen(4, DATA, X0, Y0, UNIT))   
end