The script below will send a character from TI-Nspire to an Arduino over a serial port.
Then the Arduino will convert the received character to a hex ASCII code, and send the code back to TI-Nspire.
If you want to replicate this experiment, it might be a good idea to use a TI-Innovator Hub instead of a Micro/Leonardo.
.ino
void setup() { Serial.begin(115200); } void loop() { while(Serial.available()) { char c = Serial.read(); Serial.print(c); Serial.print(": "); Serial.print(c, HEX); // 16 進の ASCII コードに変換して返す。 Serial.print("H"); } }
.lua
require "asi" require "color" platform.window:setBackgroundColor(color.dodgerblue) function on.resize() W, H = platform.window:width(), platform.window:height() fontSize = W/13.25 leftMargin = fontSize lineSpace = fontSize * 1.5 end function reset() txChar = nil rxChar = nil platform.window:invalidate() end ------------------------------- -- シリアルデータの書き込み函数 ------------------------------- function writeData() if PORT then PORT:write(txChar) end end ------------------------------- -- シリアルデータの読み取り函数 ------------------------------- function readData() if PORT then PORT:read() end 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() port:setReadListener(readListener) end end ----------------------------- -- 読み取りリスナーを定義する ----------------------------- function readListener(port) local data = port:getValue() rxChar = data or nil platform.window:invalidate() end --------------------------------- -- ASI ステートリスナーを登録する --------------------------------- function on.construction() asi.addStateListener(stateListener) end ------------------------------------------------------------------------------- function on.charIn(char) txChar = char writeData() readData() platform.window:invalidate() end function on.escapeKey() reset() end function on.paint(gc) gc:setFont("sansserif", "r", fontSize) gc:setColorRGB(color.white) gc:drawString(txChar or "---", leftMargin, 0) gc:drawString(rxChar or "---", leftMargin, lineSpace) end