Escolar Documentos
Profissional Documentos
Cultura Documentos
PRINT
PRINT "This text-based program dumps magnetometer readings from a Texas Instrume
nts SensorTag. A SensorTag is required to use this program. See SensorTag for a
GUI based program that shows all six SensorTag sensors."
PRINT
PRINT "See the O'Reilly book, Building iPhone and iPad Electronics Projects, for
a tutorial showing how to write and use this program."
PRINT
PRINT "Press the Stop button to stop this program."
PRINT
System.showConsole
! Set up variables to hold the peripheral and the characteristics
! for the battery and buzzer.
DIM sensorTag AS BLEPeripheral
! We will look for these services.
DIM servicesHeader AS STRING, services(1) AS STRING
servicesHeader = "-0451-4000-B000-000000000000"
services(1) = "F000AA30" & servicesHeader : ! Magnetometer
mag% = 1
! Start the BLE service and begin scanning for devices.
debug = 0
BLE.startBLE
DIM uuid(0) AS STRING
DIM posCount AS INTEGER
DIM negCount AS INTEGER
DIM sTags(5) AS String
DIM cBuses(5) AS STRING
DIM tagID AS STRING
DIM busNO AS STRING
BLE.startScan(uuid)
setUpGUI
drawImage(direction)
!
!
!
SUB BLEDiscoveredPeripheral (time AS DOUBLE, _
peripheral AS BLEPeripheral, _
services() AS STRING, _
advertisements(,) AS STRING, _
rssi)
IF peripheral.bleName = "TI BLE Sensor Tag" THEN
sensorTag = peripheral
tagID=sensorTag.uuid
FOR I = 1 to noofBuses
IF sTags(I) = tagID THEN
busNO=cBuses(I)
END IF
NEXT I
BLE.connect(sensorTag)
BLE.stopScan
END IF
END SUB
! Called to report information about the connection status of the
! peripheral or to report that services have been discovered.
!
! Parameters:
!
1 - Connection completed
2 - Connection failed
3 - Connection lost
4 - Services discovered
!
SUB BLEPeripheralInfo (time AS DOUBLE, _
peripheral AS BLEPeripheral, _
kind AS INTEGER, _
message AS STRING, _
err AS LONG)
IF kind = 1 THEN
! The connection was established. Look for available services.
IF debug THEN PRINT "Connection made."
peripheral.discoverServices(uuid)
ELSE IF kind = 2 OR kind = 3 THEN
IF debug THEN PRINT "Connection lost: "; kind
BLE.connect(sensorTag)
ELSE IF kind = 4 THEN
! Services were found. If it is one of the ones we are interested
! in, begin discovery of its characteristics.
DIM availableServices(1) AS BLEService
availableServices = peripheral.services
FOR s = 1 to UBOUND(services, 1)
FOR a = 1 TO UBOUND(availableServices, 1)
IF services(s) = availableServices(a).uuid THEN
IF debug THEN PRINT "Discovering characteristics for "; services(s)
peripheral.discoverCharacteristics(uuid, availableServices(a))
END IF
NEXT
NEXT
END IF
END SUB
!
!
1 - Characteristics found
!
SUB BLEServiceInfo (time AS DOUBLE, _
peripheral AS BLEPeripheral, _
service AS BLEService, _
kind AS INTEGER, _
message AS STRING, _
err AS LONG)
IF kind = 1 THEN
! Get the characteristics.
DIM characteristics(1) AS BLECharacteristic
characteristics = service.characteristics
FOR i = 1 TO UBOUND(characteristics, 1)
IF service.uuid = services(mag%) THEN
! Found the magnetometer.
SELECT CASE characteristics(i).uuid
CASE "F000AA31" & servicesHeader
!
!
!
call.
!
SUB BLECharacteristicInfo (time AS DOUBLE, _
peripheral AS BLEPeripheral, _
characteristic AS BLECharacteristic, _
kind AS INTEGER, _
message AS STRING, _
err AS LONG)
IF kind = 2 THEN
DIM value(1) AS INTEGER
value = characteristic.value
SELECT CASE characteristic.uuid
CASE "F000AA31" & servicesHeader
! Update the magnetometer.
c = 65536.0/2000.0
x = ((value(2) << 8) BITOR value(1))/c
y = ((value(4) << 8) BITOR value(3))/c
z = ((value(6) << 8) BITOR value(5))/c
!PRINT time, x, y, z
IF z > -15 THEN posCount=posCount+1
IF posCount=5 THEN
posCount=0
!PRINT "BUS " & busNO & " ENTERED campus at " & System.Date.shortTime & "
on "System.Date.shortDate)
PRINT
PRINT
END IF
IF z < -15 THEN negCount=negCount+1
IF negCount=5 THEN
negCount=0
PRINT "BUS " & busNO & " LEFT campus at " & System.Date.shortTime & " on "
& System.Date.shortDate
PRINT
PRINT
PRINT
status.setText(tagID & " Bus left campus at " & System.Date.shortTime & "
on " & System.Date.shortDate)
drawImage(3)
END IF
CASE ELSE
PRINT "Read from "; characteristic.uuid
END SELECT
ELSE IF kind = 3 AND err <> 0 THEN
PRINT "Error writing "; characteristic.uuid; ": ("; err; ") "; message
END IF
END SUB
! Set up the user interface.
SUB setUpGUI
!Switch to the graphics screen.
System.showGraphics(1)
!System.setAllowedOrientations(1 << (System.orientation - 1))
orientation = System.orientation
DIM title AS Label
title = Graphics.newLabel(20, 20, Graphics.width - 40, 45)
title.setFont("Sans-serif", 40, 0)
title.setText("GRIET Bus Logger")
title.setAlignment(2)
title.setBackgroundColor(0, 0, 0, 0)
title.setColor(200,0,0,1)
status = Graphics.newLabel(50,200,Graphics.width - 40, 45)
status.setFont("Sans-serif", 20, 0)
status.setText(" Bus entry / exit status")
status.setAlignment(2)
status.setBackgroundColor(0,0,0,1)
status.setColor(200,0,0,1)
END SUB
IF direction = 2 THEN
Graphics.setColor(1,0.5,0)
Graphics.fillRect(360,440,40,30)
Graphics.setColor(0,0,0)
Graphics.fillRect(595,440,40,30)
DIM title AS Label
title = Graphics.newLabel(20, 20, Graphics.width - 40, 45)
title.setFont("Sans-serif", 40, 0)
title.setText("GRIET Bus Logger")
title.setAlignment(2)
title.setColor(200,0,0,1)
ELSE IF direction= 3 THEN
Graphics.setColor(1,0.5,0)
Graphics.fillRect(595,440,40,30)
Graphics.setColor(0,0,0)
Graphics.fillRect(360,440,40,30)
END IF
END SUB