HiJack In the Box – Part 3 – iPhone HiJack Tutorial
August 21, 2011
Now that our setup is done let us get on with the code!
From the HiJack readme we would do just a couple of things and “The simplest way is to open the project, and make it. Then, add the libHiJack.a and HiJackMgr.h to your iPhone application“ to easily integrate the HiJack lib to our app. But from Part 2 of our tutorial, I think I had experienced otherwise.
For those of you who would like to view the code and follow through, you can download it from here:
https://github.com/wendellinfinity/HiJackInTheBox
Anyway, to start, let us go to the HiJackInTheBoxAppDelegate.h file and then add in the delegate and the instance variable:

Then going to our HiJackInTheBoxAppDelegate.mm file, we will have to initialize the hiJackMgr instance variable:

Of course, it won’t be complete withouth deallocating it, so here we go:

Then let us implement the (int) receive delegate still on our HiJackInTheBoxAppDelegate.mm file:

Let us test our code and build it, I hope you’d get the a success:

We can now move on to our view! But first, how can we pass our sensor’s value to the view? We can create a new “(float) sensorValue” property on our HiJackInTheBoxViewController.h file:

and synthesize it on our implementation file HiJackInTheBoxViewController.m:

Going back to our HiJackInTheBoxViewController.m code, we can look for the code that makes the gradient box bounce:

and replace it with our sensorValue property:

Hmmm.. now we have a clue that the transY variable is the one that calls the shots! So let us find more of it and replace it with our sensorValue variable:

to this:

Now let us run the app! Be sure to plug in your HiJack to your iOS device and when you do, turn up the volume too. Hmmm.. but wait, if you are running it you now would notice that there is no movement from the box. It is because that our implementation before for the (int) receive delegate is not yet complete. Here it is now going back to the HiJackInTheBoxAppDelegate.mm source:

Why divide? Since the value of UInt8 may range from 0 to 255, we can get a “percentage” value instead and pass this to the viewController.
So try it now and run it!
Here is a video showing the action:
I hope you can now control the box, up and down a little. But there is a bit of a snag with it because it does not respond quickly. I will contact the HiJack lib developers about this and keep you posted.
Here is the link to the code again:
https://github.com/wendellinfinity/HiJackInTheBox
I hope you have enjoyed this 3 part tutorial! Comments are very welcome.
;
Great work, It is hard to guess all those special settings!
did you ever try to get the speed up ?
Thanks Madz. For the speed up, I made the lib as a class and not a delegate. However I am still trying out to execute the code on another thread. I have not yet committed the change in git though.
hi,
I meant if you have accomplished the 8.82 Khz mentioned instead of the very low bit rate used by the original software ?
br,
Mads
Ow I am not into it because I am also having a bit of a challenge with the TinyOS firmware loaded on the HiJack to add more ports (and also means to alter the ‘protocol’ used in the libhijack code).
That was very helpful! Thanks!
welcome Pedro
hey great writeup. im thinking of using a sensor with this great device though the sensor uses 100ma(max120) to start up. can this circuitry run such current?
Hello Tom,
Thanks for that. From the project site, it tells that it “… can supply 7.4 mW to a load …” and from that we can compute for the load of your sensor given it is: max 120ma; assuming it will operate on the max voltage of the hijack module which is 3.3V; we can get that the load of your sensor is 0.396 watt (Volts x Current) or 396mW. We can say that it is beyond the supply of the hijack module. However it may depend on the voltage requirement of your sensor, if it is < 1V then we can try it anyway. In my opinion, maybe we can only use resistive sensors like the temp or light for now.
Hope it helps,
Wendell;
hmmm. thanks for the reply. what about using this circuit for charging a super capacitor say a “Super capacitor 5.5V 0.22F” what would be the charge times etc??
oh and here are the specs of the sensor i would like to use
heater voltage = 0.9v +or- 0.1v
loop voltage <6vdc
heater current 120ma
sensor consumption <10mw
hello there Tom,
My apologies for the late reply.
@capacitor: I’m afraid I cannot give you accurate answers but from the looks of it, when working with that low output, you might need maybe a super capacitor charging IC as it will be easier, short of making an energy harvester thing (here: http://www.sparkfun.com/products/9946)
@sensor: Hmm this kinda looks like an smoke sensor or alcohol sensor, if I may guess but I’m sorry if I did. Since I may fail you with my comment above about the cap, then I may help you on how to power the sensor, though I failed in trying, maybe you can have a better implementation of hooking up a coin cell battery instead to power up the big sensor. You can use analog optoisolators between the sensor circuit and the main board too.
I hope it makes sense;
not a problem you have been a great help. its a methane sensor. using a supercap might be the way to go. i really do appreciate your help, i will let you know my progress
Thanks
Godspeed on your project Tom!
I already have my HiJack Pack and I test part 1 of this tutorial. The problem is when i want to download the firmware. I follow the step one but in the second steep says “Insert the programmer into the USB port of PC. The red LED D1 should be lit. Then change the USB serial Port to COM1″ in my devices doesn’t apper something like that :S Another question is if I can modify the program in the mainboard. Thank you
how does I change the usb port to com1 :S
Hello Pedro, we can change the usb port to COM1 through the device manager then look for the device on the COM ports list and go to properties then you can see a “port settings” tab there. You can select from a list of COM ports there. However, if there is an existing device already connected to COM1 port, then you will have to look for that device and then set it on another port.
Yeah, My problem is that in my PC, it said it said it is FT232R USB UART, but the driver for this could not be found. I tried each USB port in my PC, all are the same. Where I could find the driver for this?
May I know what your OS is? But generally, the drivers are found here for Windows OS: http://www.ftdichip.com/Drivers/D2XX.htm
It is windowsXP, it is solved now with installing the driver you advised. Thanks.
Hello Pedro, yes you can modify the program on the mainboard. You will have to take a look at TinyOS to do so.
Hi Wendell – fantastic tutorial! I was just wondering if you ever managed to get the speed up to scratch in your code. I looked at some of the code in the HijackMgr.mm which seems to have some commented out sections that might make the transmission faster.
In particular it mentions a sampling rate as well as the number of stop bits to transmit. Also, I was wondering if you’d tried (I’m sure you have) altering the hardware sampling rate using AudioSessionSetProperty. I haven’t got my Hijack device yet so I’m unable to test whether this works – was just wondering what you thought of those ideas.
Hello J, my apologies for the late reply. I have not tried to alter the sampling rate, but from the top of my head, changing this will have an impact to the behavior of the Manchester encoding protocol because it relies on the sampling rate on how to determine the edges when transmitting 1 or 0.
i just want to know what values are coming from potentiometer. i mean asci of numerical values are sent .
Hello iqbal, when it is used, it is numerical, but in reality it is binary so we can translate it into ascii if we want
As I began adding the HiJackMgr.mm instance calls with my AppDelegate.h file I started getting some weird build errors:
Unknown type name ‘class’; did you mean ‘Class’?
http://stackoverflow.com/questions/8588734/unknown-type-name-class-did-you-mean-class
To fix I changed the following build settings under Project->BuildSettings->Language->Compile Sources As to “Objective-C++”
I’m using Xcode4.3.2 with iOS SDK 5.1
From the paper of Hijack, it said the phone’s headset ports as follows:
+++
The right channel pro- vides power to the energy harvesting circuit. The left chan- nel provides data output from the phone to the microcontroller. The microphone provides data input from the microcontroller to the phone.
+++
As for the energy harvesting from right channel always is not with enough power, I would remove those part from circuits. Is there any side effect if I do this?
Also, I would ask the advice which code is for this handling?
– energy harvesting.
What kind of signals is output from left channel? And what kind of output microphone will get if the A6 input is changed?
Thanks,
Fantastic tutorial.
I have a basic question. When HiJack is connected to the phone and is in operation, can I still play regular audio via phone’s speakers? Is there a trick for it?
For now, I am not aware of any approach (albeit not possible) on how to simultaneously play audio on the loudspeaker while the headphone jack is being used. This behavior is controlled by iOS itself. My apologies if I am not able to help you out on this.
Thanks for the reply. Can there be an intermittent solution, so while playing audio on the speaker, HiJack is paused/stopped/disabled ( and/or audio jack is not detected) and then when audio is stopped, HiJack resumes again ?
In the HiJack code, there are interrupts routine for kAudioSessionEndInterruption and kAudioSessionBeginInterruption . Is HiJack interrupted when audio from other App or incoming call is played ?
Yes indeed the HiJack output is stopped as when the Audio Session is initialized (AudioSessionInitialize) the audio interrupt handler is also put in place (the code you were referring to). It will stop the audio from the app and then resume if the interruption ended. You may plug in your code there. For the audio jack event, you may look at the usage of “kAudioSessionProperty_AudioRouteChange”, “kAudioSessionRouteChangeReason_OldDeviceUnavailable” and “kAudioSessionRouteChangeReason_NewDeviceAvailable”. Hope it helps.