\r
HiJack\r is\r a\r hardware\r device\r that\r plugs\r right\r into\r your\r iPhone/iPad\r headphone\r jack.\r The\r current\r HiJack\r firmware\r supports\r an\r 8\r bit\r A-‐D\r converter\r that\r takes\r an\r input\r of\r 0-‐2.75\r volts.\r In\r this\r article,\r we’ll\r look\r at\r how\r to\r hook\r up\r the\r HiJack\r hardware\r and\r write\r a\r simple\r program\r to\r access\r data.\r \r
In\r the\r programming\r world,\r it’s\r been\r tradition\r to\r start\r off\r with\r a\r new\r
computer\r language\r by\r writing\r a\r simple\r program\r that\r prints\r “Hello\r world!”\r This\r started\r with\r Brian\r Kerninghan,\r who\r wrote\r the\r first\r hello\r world\r program\r in\r a\r tutorial\r for\r the\r B\r programming\r language\r in\r 1972.\r With\r nearly\r 40\r years\r of\r tradition\r behind\r us,\r we’ll\r start\r our\r
exploration\r of\r the\r HiJack\r hardware\r with\r a\r simple\r program\r to\r read\r the\r data,\r and\r a\r simple\r hardware\r project\r to\r provide\r that\r data-‐-‐hello\r world,\r hardware\r style.\r \r We’ll\r call\r our\r program\r Hello\r HiJack.\r \r
One\r of\r the\r challenging\r things\r about\r interfacing\r hardware\r to\r a\r computer\r is\r that\r it\r involves\r at\r least\r two\r distinct\r disciplines,\r electrical\r engineering\r and\r programming.\r If\r you\r are\r reading\r this\r article,\r you’re\r probably\r at\r least\r conversant\r with\r one\r of\r these\r fields,\r but\r very\r few\r people\r are\r expert\r in\r both.\r Because\r of\r that,\r I’ll\r provide\r a\r lot\r more\r background\r in\r programming\r than\r the\r programmers\r need,\r and\r more\r background\r in\r electronics\r than\r the\r electrical\r engineers\r need.\r I’m\r sure\r you’re\r good\r at\r skimming\r by\r now,\r so\r feel\r free\r to\r skip\r over\r the\r sections\r you\r already\r know\r well.\r
Building\r the\r Sensor\r
\r
Let’s\r start\r with\r the\r hardware\r itself.\r HiJack\r was\r developed\r at\r the\r University\r of\r Michigan\r for\r creating\r cubic-‐inch\r sensor\r peripherals\r for\r mobile\r phones\r like\r the\r
iPhone.\r It\r works\r just\r as\r well\r from\r the\r iPad.\r The\r whole\r idea\r is\r to\r give\r you\r a\r hardware\r platform\r that\r you\r can\r build\r on\r to.\r You\r can\r buy\r the\r hardware\r from\r Seeed\r Studio,\r which\r sells\r the\r raw\r hardware,\r a\r development\r pack,\r and\r a\r few\r odd\r components.\r I\r got\r the\r development\r pack,\r shown\r on\r the\r next\r page.\r It\r comes\r with\r the\r actual\r HiJack\r device,\r which\r is\r the\r green\r board\r with\r the\r headphone\r connection,\r along\r with\r a\r few\r
other\r goodies.\r \r The\r most\r important\r extra\r is\r the\r board\r with\r the\r USB\r plug;\r this\r is\r used\r to\r download\r new\r
firmware\r as\r it\r becomes\r available.\r Be\r warned,\r though:\r The\r software\r to\r install\r firmware\r on\r the\r HiJack\r board\r only\r runs\r on\r Windows,\r and\r is\r fairly\r temperamental.\r The\r two\r blue\r boards\r are\r prototyping\r boards\r that\r are\r designed\r to\r plug\r snugly\r into\r the\r HiJack\r board.\r The\r various\r wiring\r harnesses\r are\r used\r to\r connect\r the\r prototyping\r boards\r to\r other\r devices,\r
notably\r the\r prebuilt\r sensors\r in\r the\r GROVE\r modular\r toolset.\r The\r GROVE\r components\r include\r a\r collection\r of\r sensors,\r some\r of\r which\r work\r with\r HiJack\r right\r out\r of\r the\r box.\r \r
With\r a\r HiJack\r board\r in\r hand,\r the\r first\r thing\r we\r need\r to\r do\r is\r build\r a\r sensor.\r Ours\r will\r be\r about\r as\r simple\r as\r they\r come:\r We’ll\r use\r a\r potentiometer\r (variable\r resistor)\r to\r provide\r a\r voltage\r we\r can\r change\r by\r varying\r the\r resistance.\r You\r can\r pick\r up\r
potentiometers\r from\r pretty\r much\r anyplace\r that\r sells\r electronics\r components.\r The\r specific\r resistance\r doesn’t\r matter,\r either.\r I\r used\r a\r tiny\r little\r 10K\r (10,000\r ohm)\r trim\r pot\r I\r had\r laying\r around\r in\r a\r parts\r bin.\r \r
The\r idea\r behind\r this\r sensor\r is\r to\r divide\r the\r voltage\r supplied\r by\r the\r HiJack\r device.\r Resistors\r in\r series\r divide\r an\r input\r voltage.\r Here’s\r a\r diagram\r of\r the\r circuit\r we’ll\r be\r building.\r Purists\r will\r notice\r that\r I\r showed\r two\r separate\r resistors\r rather\r than\r a\r potentiometer.\r Electrically\r they\r are\r the\r same\r thing\r as\r long\r as\r the\r potentiometer\r is\r not\r being\r adjusted,\r though,\r and\r it\r makes\r the\r following\r discussion\r a\r little\r easier.\r \r
\r
The\r three\r lines\r forming\r the\r triangle\r at\r the\r left\r is\r the\r ground\r connection,\r abbreviated\r GND\r on\r the\r Seeed\r Studio\r documentation.\r This\r is\r the\r negative\r voltage\r side\r of\r the\r circuit.\r The\r other\r side,\r marked\r +2.75V,\r is\r the\r positive\r power\r supplied\r by\r the\r HiJack\r
\r
device.\r This\r pin\r is\r marked\r VCC\r on\r the\r Seeed\r Studio\r documentation.\r Our\r circuit\r will\r connect\r up\r to\r these\r two\r pins\r to\r draw\r power\r across\r the\r potentiometer.\r \r \r
Now\r a\r potentiometer\r is\r essentially,\r and\r sometimes\r literally,\r a\r resistive\r bar\r that\r uses\r a\r slider\r of\r some\r sort\r that\r slides\r over\r the\r resistor.\r The\r bar\r is\r connected\r to\r a\r third\r wire.\r Sliding\r the\r bar\r changes\r the\r resistance\r to\r either\r side,\r dividing\r the\r resistance\r in\r two\r parts\r that\r always\r add\r up\r to\r the\r total\r resistance\r in\r the\r device.\r The\r voltage\r output\r from\r the\r ground\r connection\r to\r the\r center\r wire\r of\r the\r resistor\r will\r be\r \r
R1Vout=Vin*R1+R2\r
where\r Vin\r is\r the\r 2.75\r volts\r supplied\r by\r HiJack,\r and\r R1\r and\r R2\r vary\r as\r you\r adjust\r the\r potentiometer.\r Anything\r that\r detects\r the\r voltage\r from\r the\r ground\r connection\r to\r Voltage\r Out\r will\r see\r the\r voltage\r vary\r from\r 0\r to\r 2.75\r volts\r as\r the\r potentiometer\r is\r turned.\r That’s\r exactly\r what\r the\r HiJack\r hardware\r will\r measure.\r \r
On\r the\r other\r end\r of\r the\r circuit\r sits\r the\r HiJack\r device\r itself.\r The\r top\r of\r the\r HiJack\r
device\r has\r three\r female\r headers\r that\r provide\r 10\r wiring\r connections\r each.\r Here’s\r the\r pinout,\r from\r a\r larger\r diagram\r available\r at\r the\r Seeed\r Studio\r site:\r \r
\r \r
The\r connections\r we’re\r interested\r in\r are\r on\r the\r right\r bar.\r We’ve\r already\r talked\r about\r GND\r and\r VCC;\r those\r pins\r provide\r the\r 2.75\r volt\r power\r source\r we\r are\r using.\r The\r data\r input\r line\r is\r A6/DAC0.\r That’s\r the\r pin\r we\r need\r to\r connect\r to\r the\r center\r pole\r on\r the\r potentiometer.\r Following\r the\r convention\r of\r black\r wires\r for\r ground\r and\r red\r for\r
\r
positive\r in\r DC\r circuits,\r and\r using\r a\r brown\r wire\r because\r I\r had\r one\r handy\r in\r my\r parts\r box,\r here’s\r a\r closeup\r of\r the\r connections\r on\r the\r HiJack\r device.\r \r
\r
Potentiometers\r have\r three\r pins.\r In\r general,\r the\r center\r pin\r is\r the\r one\r we\r want\r to\r hook\r up\r to\r A6/DAC0,\r and\r GND\r and\r VCC\r can\r be\r connected\r to\r either\r of\r the\r two\r remaining\r pins.\r Here’s\r what\r it\r looks\r like\r in\r my\r circuit,\r with\r a\r second,\r identical\r potentiometer\r upside\r down\r beside\r the\r one\r I\r used\r so\r you\r can\r see\r the\r pins\r coming\r out\r of\r the\r bottom.\r I\r used\r a\r breadboard\r for\r the\r connections,\r but\r that’s\r because\r I\r had\r one\r laying\r around.\r Anything\r that\r will\r securely\r mount\r the\r wires\r to\r the\r potentiometer\r and\r let\r you\r easily\r adjust\r the\r resistance\r will\r work\r just\r fine.\r \r
\r
\r
\r
The\r Hello\r HiJack\r Program\r
\r
With\r all\r of\r that\r hooked\r up,\r it’s\r time\r to\r write\r a\r program\r to\r show\r what’s\r happening.\r Run\r techBASIC\r from\r your\r iPhone\r or\r iPad,\r tap\r the\r New\r button\r to\r create\r a\r new\r
program,\r and\r enter\r Hello\r HiJack\r as\r the\r program\r name.\r Tap\r the\r Source\r screen\r to\r get\r the\r keyboard\r and\r enter\r this\r program.\r The\r letter\r case\r doesn’t\r matter,\r but\r I’ve\r used\r uppercase\r for\r BASIC\r keywords\r and\r for\r the\r first\r letter\r of\r class\r names\r to\r make\r it\r easier\r for\r you\r to\r read\r the\r program.\r \r
WHILE 1
System.clearConsole PRINT HiJack.receive System.wait(0.5) WEND
\r
The\r WHILE\r and\r WEND\r lines\r form\r a\r loop\r that\r will\r loop\r as\r long\r as\r the\r expression\r on\r the\r WHILE\r statement\r is\r non-‐zero.\r 1\r will\r be\r non-‐zero\r for\r a\r really\r long\r time,\r so\r the\r loop\r will\r go\r forever.\r The\r only\r way\r to\r stop\r this\r program\r will\r be\r to\r press\r the\r Stop\r button\r that\r will\r show\r up\r at\r the\r top\r of\r the\r screen\r when\r the\r program\r starts\r running.\r \r
System.clearConsole\r clears\r any\r text\r from\r the\r console,\r where\r text\r input\r and\r output\r appear.\r \r
The\r PRINT\r statement\r then\r prints\r a\r two-‐dimensional\r array\r to\r the\r screen.\r It\r gets\r that\r array\r from\r the\r HiJack\r hardware–HiJack.receive\r is\r a\r function\r that\r returns\r an\r array\r where\r the\r first\r element\r is\r a\r value\r between\r 0\r and\r 255,\r with\r lower\r numbers\r when\r the\r voltage\r is\r low\r and\r higher\r numbers\r when\r it\r is\r high,\r and\r a\r second\r value\r that\r is\r a\r time\r stamp.\r The\r time\r stamp\r may\r not\r seem\r to\r change\r much,\r but\r it’s\r a\r large\r number\r whose\r most\r significant\r digits\r change\r slowly.\r \r
System.wait(0.5)\r pauses\r for\r a\r moment\r so\r we\r get\r a\r chance\r to\r read\r the\r screen\r
without\r too\r much\r flicker.\r The\r parameter\r tells\r the\r call\r how\r long\r to\r pause,\r in\r this\r case\r for\r 1/2\r of\r a\r second.\r \r
After\r typing\r in\r the\r program,\r plug\r the\r HiJack\r hardware\r into\r the\r headphone\r jack\r on\r your\r iPhone.\r Tap\r the\r Programs\r button\r at\r the\r bottom\r of\r the\r screen,\r then\r tap\r the\r name\r of\r the\r program\r (not\r the\r round\r blue\r disclosure\r button\r to\r the\r right,\r the\r actual\r name)\r to\r run\r the\r program.\r techBASIC\r will\r change\r the\r display\r to\r the\r Console,\r where\r you\r should\r see\r a\r number\r that\r occasionally\r changes.\r After\r a\r second\r or\r two\r it\r will\r settle\r down\r to\r a\r single\r number,\r or\r perhaps\r flick\r back\r and\r forth\r between\r two\r values.\r Adjust\r the\r potentiometer,\r and\r you’ll\r see\r the\r number\r change.\r Just\r a\r few\r more\r steps,\r and\r you’ll\r be\r building\r Robo\r Cop!\r \r
When\r Things\r Go\r Wrong\r
\r
OK,\r not\r so\r fast.\r Did\r it\r work?\r If\r not,\r there\r are\r two\r places\r for\r things\r to\r go\r wrong:\r the\r software\r or\r the\r hardware.\r
\r
If\r there\r is\r a\r problem\r with\r the\r software,\r techBASIC\r will\r say\r so.\r Check\r to\r make\r sure\r the\r program\r is\r exactly\r the\r one\r typed\r above.\r Letter\r case\r is\r not\r important,\r but\r spaces\r and\r line\r breaks\r can\r be.\r Make\r sure\r there\r are\r five\r lines,\r and\r that\r there\r are\r spaces\r between\r the\r words\r where\r shown,\r and\r no\r spaces\r inserted.\r clear Console\r is\r not\r the\r same\r as\r clearConsole!\r Also,\r look\r at\r the\r error\r messages\r from\r techBASIC.\r They\r will\r usually\r pinpoint\r the\r problem,\r and\r even\r if\r they\r don’t,\r the\r real\r problem\r will\r be\r close\r by.\r
\r
If\r the\r program\r is\r running\r and\r printing\r numbers,\r but\r the\r numbers\r seems\r to\r jump\r around\r randomly,\r you\r probably\r don’t\r have\r a\r good\r connection.\r Check\r to\r make\r sure\r the\r HiJack\r hardware\r is\r plugged\r all\r the\r way\r into\r the\r headphone\r port.\r The\r metal\r disk\r on\r the\r washer\r does\r not\r fit\r flush\r against\r the\r case,\r so\r don’t\r push\r too\r hard.\r If\r you\r are\r using\r an\r iPod\r Touch,\r you\r may\r also\r need\r to\r twist\r the\r jack\r around\r a\r bit.\r
\r
The\r last\r source\r of\r trouble\r is\r the\r HiJack\r hardware\r or\r the\r circuit.\r Check\r it\r all\r carefully\r to\r make\r sure\r the\r right\r wires\r are\r connected\r to\r the\r right\r places,\r and\r that\r the\r wires\r are\r making\r a\r good\r electrical\r contact.\r
A\r Better\r Program\r
\r
While\r that\r first\r little\r program\r worked,\r it’s\r not\r the\r most\r exiting\r program\r in\r the\r world.\r
Wouldn’t\r it\r be\r nicer\r to\r have\r something\r like\r an\r oscilloscope\r trace,\r like\r you\r get\r from\r the\r
techBASIC\r Magnetometer\r sample?\r It’s\r actually\r not\r that\r hard.\r In\r fact,\r let’s\r do\r that\r now.\r
\r
Our\r goal\r is\r a\r program\r that\r plots\r results\r like\r those\r to\r the\r right.\r We’ll\r plot\r 10\r seconds\r worth\r of\r data,\r collecting\r a\r data\r point\r every\r 0.1\r
seconds,\r for\r a\r total\r of\r 100\r data\r points\r plotted\r at\r any\r one\r time.\r The\r newest\r data\r will\r always\r appear\r at\r the\r right,\r at\r time=0,\r and\r older\r data\r will\r be\r scrolled\r to\r the\r left,\r with\r time\r getting\r more\r and\r more\r negative\r until\r the\r point\r falls\r off\r of\r the\r display.\r
\r
Let’s\r start\r with\r the\r finished\r program\r and\r pick\r it\r apart.\r I’ll\r show\r the\r complete\r program\r first,\r then\r we’ll\r walk\r through\r it\r line\r by\r line.\r The\r
program\r is\r also\r available\r for\r download\r at\r the\r end\r of\r this\r article,\r so\r don’t\r type\r it\r in\r unless\r you\r just\r want\r the\r practice.\r
\r
! Shows a running plot of HiJack ! input for the last 10 seconds ! in 0.1 second intervals. !
! Initialize the display with the ! value set to 0. DIM value(100, 2) FOR t = 1 TO 100
value(t, 1) = (t - 100)/10.0 NEXT
! Initialize the plot and show ! it.
DIM p as Plot, ph as PlotPoint p = Graphics.newPlot
p.setTitle(\"HiJack Raw Data\")
p.setXAxisLabel(\"Time in Seconds\") p.setYAxisLabel(\"Value Read\") p.showGrid(1)
p.setGridColor(0.8, 0.8, 0.8)
ph = p.newPlot(value) ph.setColor(1, 0, 0)
ph.setPointColor(1, 0, 0)
! Set the plot range and ! domain. This must be done ! after adding the first
! PlotPoint, since that also ! sets the range and domain. p.setView(-10, 0, 0, 255, 0)
system.showGraphics
! Loop continuously, collecting ! HiJack data and updating the ! plot.
DIM time AS double
time = System.ticks - 10.0 WHILE 1
! Wait for 0.1 seconds to ! elapse.
WHILE System.ticks < time + 10.1 WEND
time = time + 0.1
! Get and plot one data point. h = HiJack.receive FOR i = 1 TO 99
value(i, 2) = value(i + 1, 2) NEXT
value(100, 2) = h(1) ph.setPoints(value) Graphics.repaint WEND
\r
OK,\r that’s\r not\r too\r long,\r as\r programs\r go.\r It’s\r just\r 55\r lines,\r and\r a\r lot\r of\r them\r are\r comments.\r Let’s\r see\r what\r it\r does.\r Here’s\r the\r first\r chunk.\r \r
! Shows a running plot of HiJack ! input for the last 10 seconds ! in 0.1 second intervals. !
! Initialize the display with the ! value set to 0. DIM value(100, 2) FOR t = 1 TO 100
value(t, 1) = (t - 100)/10.0 NEXT
\r
This\r first\r chunk\r of\r code\r has\r come\r introductory\r comments,\r then\r sets\r up\r an\r array\r to\r hold\r the\r values\r we\r will\r eventually\r read\r from\r the\r HiJack\r hardware.\r techBASIC\r won’t\r care\r if\r you\r leave\r out\r the\r comments\r (the\r lines\r starting\r with\r a\r !\r character),\r but\r
commenting\r your\r code\r is\r a\r good\r habit.\r The\r array\r value\r will\r hold\r up\r to\r 100\r values.\r It’s\r a\r two-‐dimensional\r array\r because\r we\r will\r need\r to\r tell\r techBASIC\r both\r the\r X\r and\r Y\r values\r for\r each\r point\r to\r plot.\r The\r X\r values\r are\r the\r timeline,\r which\r doesn’t\r change,\r so\r we\r use\r a\r FOR\r loop\r to\r fill\r in\r the\r X\r values.\r Our\r intent\r is\r to\r collect\r one\r point\r every\r 0.1\r seconds,\r and\r display\r 10\r seconds\r worth\r of\r data,\r so\r we\r fill\r in\r the\r X\r values\r with\r values\r ranging\r from\r -‐9.9\r to\r 0.\r We\r can\r safely\r leave\r the\r Y\r values\r unchanged,\r since\r BASIC\r initialized\r new\r variables\r to\r 0,\r and\r that\r will\r work\r fine\r for\r our\r purpose.\r \r
! Initialize the plot and show ! it.
DIM p as Plot, ph as PlotPoint
\r
Next\r we\r need\r to\r create\r a\r plot.\r The\r DIM\r statement\r sets\r up\r two\r variables,\r one\r to\r hold\r the\r Plot\r class\r that\r displays\r the\r plot\r itself,\r and\r another\r for\r the\r PlotPoint\r class,\r which\r contains\r the\r actual\r points\r to\r plot.\r Why\r didn’t\r techBASIC\r do\r this\r in\r one\r step?\r Well,\r the\r Magnetometer\r sample\r that\r comes\r with\r techBASIC\r is\r a\r good\r example\r that\r shows\r why\r not.\r It\r plots\r three\r lines\r on\r a\r single\r display,\r one\r each\r for\r the\r X,\r Y\r and\r Z\r directions.\r That\r sample\r still\r just\r has\r one\r Plot\r class,\r since\r we\r want\r all\r of\r the\r lines\r to\r show\r up\r on\r a\r single\r plot,\r but\r there\r are\r three\r PlotPoint\r classes,\r one\r for\r each\r axis.\r You\r can\r add\r as\r many\r lines\r as\r you\r like\r to\r a\r techBASIC\r plot.\r \r
p = Graphics.newPlot
p.setTitle(\"HiJack Raw Data\")
p.setXAxisLabel(\"Time in Seconds\") p.setYAxisLabel(\"Value Read\") p.showGrid(1)
p.setGridColor(0.8, 0.8, 0.8)
\r
The\r first\r line\r creates\r the\r plot\r itself.\r Think\r of\r this\r as\r the\r background,\r including\r the\r titles,\r axis,\r and\r so\r forth.\r The\r setTitle\r method\r sets\r the\r title\r at\r the\r top\r of\r the\r plot,\r while\r the\r next\r two\r lines\r set\r the\r axis\r labels.\r showGrid\r turns\r one\r the\r grid\r lines\r that\r appear\r behind\r the\r plot\r line;\r without\r this\r call,\r the\r background\r is\r blank.\r Finally,\r we\r set\r the\r grid\r color\r to\r a\r light\r gray.\r Like\r almost\r all\r techBASIC\r calls\r that\r take\r a\r color,\r
setGridColor\r takes\r three\r parameters,\r one\r each\r for\r the\r intensity\r of\r the\r red,\r green\r
and\r blue\r colors,\r in\r that\r order.\r The\r valid\r values\r are\r 0.0\r to\r 1.0,\r with\r 0.0\r being\r black,\r and\r 1.0\r being\r the\r full\r intensity\r for\r that\r color.\r A\r few\r calls\r have\r a\r fourth\r number\r for\r the\r alpha\r channel,\r which\r tells\r how\r transparent\r a\r color\r is.\r That\r lets\r you\r draw\r
something\r over\r a\r background\r and,\r to\r the\r degree\r specified\r by\r the\r alpha\r value,\r see\r through\r the\r new\r color\r to\r whatever\r lies\r behind\r it.\r Check\r out\r this\r Wikipedia\r article\r if\r you\r would\r like\r to\r know\r more\r about\r how\r RGB\r color\r works\r on\r a\r computer.\r (http://en.wikipedia.org/wiki/RGB_color_model)\r \r
ph = p.newPlot(value) ph.setColor(1, 0, 0)
ph.setPointColor(1, 0, 0)
\r
The\r next\r step\r is\r to\r set\r up\r the\r PlotPoint\r class\r that\r actually\r draws\r the\r line\r across\r the\r plot.\r The\r newPlot\r method\r creates\r a\r new\r instance\r of\r the\r class\r and\r adds\r it\r to\r the\r Plot\r we\r just\r created.\r We\r then\r set\r the\r color\r for\r both\r the\r line\r and\r the\r points\r where\r the\r data\r is\r actually\r plotted\r to\r red.\r There\r are\r other\r calls\r in\r the\r PlotPoint\r class\r that\r control\r the\r shape\r of\r the\r points\r and\r line;\r you\r can\r use\r those\r to\r customize\r your\r version\r of\r the\r program.\r \r
! Set the plot range and ! domain. This must be done ! after adding the first
! PlotPoint, since that also ! sets the range and domain. p.setView(-10, 0, 0, 255, 0)
\r
HiJack\r always\r returns\r a\r value\r from\r 0\r to\r 255,\r and\r we\r know\r the\r X\r axis\r will\r show\r times\r from\r -‐10\r to\r 0\r seconds,\r so\r we\r set\r the\r axis\r to\r show\r exactly\r those\r values.\r Without\r this\r call,\r techBASIC\r will\r default\r to\r showing\r 0\r to\r 10\r along\r the\r X\r axis\r and\r roughly\r -‐5\r to\r 5\r along\r the\r Y\r axis.\r We\r can\r always\r change\r what\r we\r are\r looking\r at\r with\r some\r swipe\r and\r pinch\r gestures,\r but\r this\r saves\r us\r the\r trouble.\r \r
system.showGraphics
\r
Now\r that\r things\r are\r set\r up,\r we\r tell\r techBASIC\r to\r switch\r to\r the\r Graphics\r display.\r Again,\r we\r could\r have\r done\r this\r with\r a\r tap\r on\r the\r Graphics\r button,\r but\r this\r saves\r us\r the\r trouble.\r \r
! Loop continuously, collecting ! HiJack data and updating the ! plot.
DIM time AS double
time = System.ticks - 10.0
\r
HiJack\r can\r report\r data\r at\r various\r rates\r up\r to\r a\r little\r more\r than\r 100\r points\r per\r
second.\r The\r default\r rate\r in\r techBASIC\r is\r about\r 40\r values\r per\r second,\r which\r is\r more\r than\r we\r need.\r These\r lines\r set\r up\r a\r time\r stamp\r we\r will\r use\r to\r tell\r when\r 0.1\r seconds\r
has\r elapsed.\r Each\r time\r that\r happens,\r we’ll\r grab\r a\r new\r point\r from\r the\r HiJack\r hardware\r and\r add\r it\r to\r our\r plot.\r \r
WHILE 1
\r
Just\r like\r the\r first\r program,\r this\r program\r will\r loop\r until\r you\r manually\r stop\r it\r with\r the\r Stop\r button.\r \r
! Wait for 0.1 seconds to ! elapse.
WHILE System.ticks < time + 10.1 WEND
time = time + 0.1
\r
Here\r is\r where\r we\r wait\r for\r 0.1\r seconds\r to\r elapse.\r The\r WHILE\r loop\r waits\r until\r the\r system\r clock\r reports\r a\r time\r 10.1\r seconds\r past\r the\r original\r time\r we\r recorded\r before\r the\r loop\r started.\r We\r then\r add\r 0.1\r to\r this\r time\r so\r the\r next\r time\r through,\r this\r timer\r loop\r will\r wait\r until\r 10.2\r seconds\r have\r gone\r by,\r and\r so\r forth.\r \r \r \r
! Get and plot one data point. h = HiJack.receive
\r
This\r is\r all\r it\r takes\r to\r actually\r read\r the\r HiJack\r hardware.\r A\r value\r from\r 0\r to\r 255\r is\r stuffed\r into\r the\r variable\r h.\r \r
FOR i = 1 TO 99
value(i, 2) = value(i + 1, 2) NEXT
\r
This\r loop\r shifts\r the\r 99\r most\r recent\r points\r in\r the\r value\r array\r one\r index\r lower,\r which\r will\r cause\r them\r to\r be\r drawn\r one\r point\r to\r the\r left\r on\r the\r plot.\r Remember,\r the\r time\r is\r preset,\r and\r is\r not\r being\r shifted,\r so\r this\r essentially\r makes\r each\r point\r 0.1\r seconds\r older\r on\r the\r plot.\r The\r first\r time\r through,\r this\r is\r just\r copying\r a\r bunch\r of\r zeros,\r but\r after\r 10\r seconds,\r all\r of\r the\r values\r are\r older\r values\r read\r from\r the\r HiJack\r hardware.\r \r
value(100, 2) = h(1)
\r
The\r new\r point\r goes\r in\r the\r last\r spot\r in\r the\r plot.\r We\r pull\r off\r the\r first\r element\r of\r the\r array,\r which\r is\r the\r HiJack\r data,\r and\r ignore\r the\r second,\r which\r contains\r a\r time\r stamp\r we\r don’t\r need\r in\r this\r program.\r \r
ph.setPoints(value) Graphics.repaint
\r
This\r tells\r the\r PlotPoint\r object\r we\r create\r and\r stored\r in\r ph\r to\r use\r a\r new\r set\r of\r points.\r Next\r we\r repaint\r the\r graphics\r screen,\r showing\r the\r new\r information\r on\r the\r plot.\r \r
WEND
\r
Finally,\r we\r go\r back\r to\r the\r WHILE\r statement\r and\r do\r it\r all\r again.\r \r
Try\r out\r the\r finished\r program\r with\r HiJack.\r You\r should\r be\r able\r to\r see\r exactly\r where\r the\r potentiometer\r is\r set,\r and\r watch\r the\r change\r as\r you\r adjust\r it.\r This\r program\r will\r work\r with\r all\r of\r your\r HiJack\r projects,\r although\r you\r will\r probably\r develop\r custom\r programs\r for\r specific\r sensors\r and\r uses.\r Be\r sure\r and\r drop\r me\r a\r line.\r I’d\r love\r to\r hear\r about\r the\r things\r you\r build\r with\r HiJack\r and\r techBASIC!\r
For\r More\r Information\r
\r
Find\r out\r more\r about\r the\r HiJack\r project\r at\r the\r University\r of\r Michigan’s\r HiJack\r page\r (http://eecs.umich.edu/~prabal/projects/hijack/)\r and\r at\r the\r wiki\r on\r the\r Seeed\r Studio\r site.\r (http://www.seeedstudio.com/wiki/index.php?title=Hijack\r )\r The\r wiki\r also\r has\r information\r about\r other\r hardware\r projects\r you\r can\r build,\r downloads\r for\r the\r software\r to\r update\r the\r firmware\r on\r the\r HiJack,\r and\r the\r latest\r version\r of\r the\r firmware.\r \r
You\r can\r buy\r the\r HiJack\r hardware\r from\r Seeed\r Studio.\r
(http://www.seeedstudio.com/depot/\r )\r Here’s\r a\r direct\r link\r to\r the\r development\r bundle\r I\r used.\r (http://www.seeedstudio.com/depot/hijack-‐development-‐pack-‐p-‐865.html?cPath=174)\r \r
You\r can\r find\r out\r more\r about\r techBASIC\r on\r the\r techBASIC\r web\r page.\r This\r page\r also\r has\r downloads\r for\r the\r techBASIC\r reference\r manual.\r
(http://www.byteworks.us/Byte_Works/techBASIC.html)\r \r
techBASIC\r is\r available\r from\r the\r app\r store.\r
(http://itunes.apple.com/us/app/techbasic/id470781862?mt=8)\r \r
\r
And\r finally,\r here\r is\r the\r program\r presented\r in\r this\r article.\r You\r can\r download\r it\r to\r your\r computer\r and\r move\r it\r to\r techBASIC\r using\r iTunes.\r The\r Quick\r Start\r guides\r will\r step\r you\r through\r the\r process\r of\r moving\r files\r between\r your\r computer\r and\r your\r iPhone.\r The\r Quick\r Start\r guides\r are\r available\r at\r the\r bottom\r of\r the\r techBASIC\r page.\r (http://www.byteworks.us/Byte_Works/Blog/Entries/2011/12/7_HiJack_Hello_world!_Project_files/HiJack.bas)\r \r
Copyright\r 2011,\r Byte\r Works,\r Inc.\r All\r Rights\r Reserved.\r
\r
因篇幅问题不能全部显示,请点此查看更多更全内容