TI-Nspire & SensorTag / ボタンの状態を受信する / シンプル版

The previous script has been rewritten with no decorations.



The process flow is as follows:

1. The ble.addStateListener()     registers a state listener.
2. The bleCentral.startScanning() gets      a scanner                    to scan     peripherals.
3. The connect()                  gets      a connector                  to connect  a found peripheral.
4. The discoverServices()         gets      a services discoverer        to discover services.
5. The discoverCharacteristics()  gets      a characteristics discoverer to discover characteristics.
6. The setValueUpdateListener()   registers a value-update listener.
7. The setNotify()                enebles   the continuous notification mode.
8. The getValue()                 returns   the value of a characteristic.


.lua

require "bleCentral"

local bleState           = ""
local connectorState     = ""
local CONNECTED          = false
local myPeripheral       = nil
local peripheralName     = ""
local keyReadUUID        = "FFE1"
local whichButtonPressed = nil

function on.resize()
   W, H = platform.window:width(), platform.window:height()
   leftMargin = W/100
   fontSize   = W/19
   refreshMenu()
   platform.window:invalidate()
end
function on.construction()
   ble.addStateListener(stateListener)
end
function on.paint(gc)
   gc:setFont("sansserif", "r", fontSize)
   gc:drawString(bleState       or "none", leftMargin, 0.0 * H)
   gc:drawString(peripheralName or "none", leftMargin, 0.1 * H)
   gc:drawString(connectorState or "none", leftMargin, 0.2 * H)
   if CONNECTED then
      if whichButtonPressed and whichButtonPressed ~= 0 then
         gc:drawString(whichButtonPressed            , leftMargin, 0.4 * H)
      else
         gc:drawString("Press the left or right key.", leftMargin, 0.4 * H) 
      end
   end
end

------------------------------
-- Menu, Keyboard and Mouse --
------------------------------
function refreshMenu()
   Menu = {
      {"Controls",
         {"Scan and Connect", function() peripheralOn() end},
         {"Disconnect", function() peripheralOff() end},
      },
   }
   toolpalette.register(Menu)
end

--------------------------
-- Define state listner --
--------------------------
function stateListener(state)
   if state == "on" then
      peripheralOn()
   end 
   bleState = "BLE: "..state
   platform.window:invalidate()
end

----------------------------
-- Start or stop scanning --
----------------------------
function peripheralOn()
   if CONNECTED == false then
      bleCentral.startScanning(scanner)
   end
   platform.window:invalidate()
end
function peripheralOff()
   bleCentral.stopScanning()
   if myPeripheral then
      myPeripheral:disconnect()
   end
   peripheralName = "" 
   platform.window:invalidate()
end

--------------------
-- Define scanner --
--------------------
function scanner(peripheral)
   if peripheral then
      myPeripheral = peripheral
      peripheralName = peripheral:getName()
      peripheral:connect(connector) 
   end
   platform.window:invalidate()
end

----------------------
-- Define connector --
----------------------
function connector(peripheral, event)
   if event == bleCentral.CONNECTED then
      bleCentral.stopScanning()
      connectorState = event
      CONNECTED      = true
      myPeripheral   = peripheral
      peripheralName = peripheral:getName()
      peripheral:discoverServices(servicesDiscoverer)
   elseif event == bleCentral.DISCONNECTED then 
      connectorState = event
      CONNECTED      = false
      myPeripheral   = nil
      peripheralName = ""
   end
   platform.window:invalidate()
end

--------------------------------
-- Define services discoverer --
--------------------------------
function servicesDiscoverer(peripheral)
   if peripheral:getState() == bleCentral.CONNECTED then
      local servicesList = peripheral:getServices()
      for _, v in ipairs(servicesList) do
         v:discoverCharacteristics(characteristicsDiscoverer)
      end
   end
   platform.window:invalidate()
end

---------------------------------------
-- Define characteristics discoverer --
---------------------------------------
function characteristicsDiscoverer(service)
   local characteristicsList = service:getCharacteristics()
   for _, v in ipairs(characteristicsList) do
      if v:getUUID() == keyReadUUID then
         v:setValueUpdateListener(valueUpdateListener)
         v:setNotify(true)
      end
   end
end

----------------------------------
-- Define value-update listener --
----------------------------------
function valueUpdateListener(characteristic)
   if characteristic:getUUID() == keyReadUUID then
      whichButtonPressed = string.unpack("u8", characteristic:getValue())
   end
   platform.window:invalidate()
end