#defineNEC_HDR_MARK9000#defineNEC_HDR_SPACE4500#defineNEC_BIT_MARK560#defineNEC_ONE_SPACE1600#defineNEC_ZERO_SPACE560#defineNEC_RPT_SPACE2250#defineTOPBIT0x80000000#defineTIMER_PWM_PIN3#defineINPUT0x0#defineOUTPUT0x1#defineSYSCLOCKF_CPUconstunsignedlongSPEAKER_IR_POWER=2155823295L;//Thesetupfunctioniscalledonceatstartupofthesketchvoidsetup(){//Serial.begin(9600);//Addyourinitializationcodehere}voidTIMER_DISABLE_INTR(){TIMSK2=0;}voidenableIROut(intkhz){//EnablesIRoutput.Thekhzvaluecontrolsthemodulationfrequencyinkilohertz.//TheIRoutputwillbeonpin3(OC2B).//Thisroutineisdesignedfor36-40KHz;ifyouuseitforothervalues,it'suptoyou//tomakesureitgivesreasonableresults.(Watchoutforoverflow/underflow/rounding.)//TIMER2isusedinphase-correctPWMmode,withOCR2AcontrollingthefrequencyandOCR2B//controllingthedutycycle.//Thereisnoprescaling,sotheoutputfrequencyis16MHz/(2*OCR2A)//Toturntheoutputonandoff,weleavethePWMrunning,butconnectanddisconnecttheoutputpin.//AfewhoursstaringattheATmegadocumentationandthiswillallmakesense.//SeemySecretsofArduinoPWMathttp://arcfn.com/2009/07/secrets-of-arduino-pwm.htmlfordetails.//DisabletheTimer2Interrupt(whichisusedforreceivingIR)TIMER_DISABLE_INTR();//Timer2OverflowInterruptpinMode(TIMER_PWM_PIN,OUTPUT);digitalWrite(TIMER_PWM_PIN,LOW);//WhennotsendingPWM,wewantitlow//COM2A=00:disconnectOC2A//COM2B=00:disconnectOC2B;tosendsignalsetto10:OC2Bnon-inverted//WGM2=101:phase-correctPWMwithOCRAastop//CS2=000:noprescaling//Thetopvalueforthetimer.ThemodulationfrequencywillbeSYSCLOCK/2/OCR2A.TIMER_CONFIG_KHZ(khz);}voidTIMER_CONFIG_KHZ(intkhz){constuint8_tpwmval=SYSCLOCK/2000/(khz);TCCR2A=_BV(WGM20);TCCR2B=_BV(WGM22)|_BV(CS20);OCR2A=pwmval;OCR2B=pwmval/3;}voidTIMER_ENABLE_PWM(){TCCR2A|=_BV(COM2B1);}voidTIMER_DISABLE_PWM(){TCCR2A&=~(_BV(COM2B1));}voidmark(inttime){//SendsanIRmarkforthespecifiednumberofmicroseconds.//ThemarkoutputismodulatedatthePWMfrequency.TIMER_ENABLE_PWM();//Enablepin3PWMoutputdelayMicroseconds(time);}/* Leave pin off for time (given in microseconds) */voidspace(inttime){//SendsanIRspaceforthespecifiednumberofmicroseconds.//Aspaceisnooutput,sothePWMoutputisdisabled.TIMER_DISABLE_PWM();//Disablepin3PWMoutputdelayMicroseconds(time);}voidsendNEC(unsignedlongdata,intnbits){enableIROut(38);mark(NEC_HDR_MARK);space(NEC_HDR_SPACE);for(inti=0;i<nbits;i++){if(data&TOPBIT){mark(NEC_BIT_MARK);space(NEC_ONE_SPACE);}else{mark(NEC_BIT_MARK);space(NEC_ZERO_SPACE);}data<<=1;}mark(NEC_BIT_MARK);space(0);}//Theloopfunctioniscalledinanendlessloopvoidloop(){delay(2000);sendNEC(SPEAKER_IR_POWER,32);}
It's code - copy-paste from IRremote.cpp. I test it's code on my IR speaker and it work.
Before I rewrite it's code on Java:
This code send all bit's for value SPEAKER_IR_POWER but speaker not receive their. I tried use Arduino.delayMicroseconds instead WProgram.delayMicroseconds but it's not helped.
Do you have any idea what this not worked?
P.S. And how formating code on this forum?!
Last edit: genom2 2015-05-14
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Again, you gave me to little information:
a) Which Config are you useing? If not useing arduinoIDE or duemilanove? I'm confused because you write about Arduino.delayMicroseconds() AND WProgram.delayMicroseconds().
b) Your code uses Java type long. What Config:Mode are you useing: 16/32 or 32/64? Is 16/32 sufficient?
c) Try to debug with more Serial.println() statements.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
a) I use configuration "arduino". I write Arduino.delayMicroseconds() but it's commented code - I tried use configuration "arduinoIDE" and compile and deploy with Arduino IDE but it's not help.
b) I'm use Mode 16/32. As default mode for config "arduino".
c) I tried debug out with Serial.print():
static void sendNEC(long data, int nbits) {
Serial.println("Send " + data + "; nbits " + nbits);
enableIROut(38);
mark(NEC_HDR_MARK);
space(NEC_HDR_SPACE);
for (int i = 0; i < nbits; i++) {
if ((data & TOPBIT) != 0) {
Serial.print("one ");
mark(NEC_BIT_MARK);
space(NEC_ONE_SPACE);
} else {
Serial.print("zero ");
mark(NEC_BIT_MARK);
space(NEC_ZERO_SPACE);
}
data <<= 1;
}
mark(NEC_BIT_MARK);
space(0);
Serial.println();
}
It's printing:
Send -2139144001; nbits 32
one zero zero zero zero zero zero zero zero one one one one one one one zero one zero zero zero zero zero zero one zero one one one one one one
It's correct data.
WProgram.delayMicroseconds() is incorrect delay on microseconds - it's use:
Thread.nap(us / 1000);
Can I use "pure" (real) delay on microseconds on WProgram? Or I must use Arduino.delayMicroseconds() and compile this code with Arduino IDE? Although it's not helped.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hello!
I used oscilloscope for view signal on IR LED. And I found their rules: in C++ code have a uniform signal, but in Java code have a delay without signal. Look like a GC perform. I tried disable GC (used arduino.GC=HAIKU_NoGC in HaikuVM.properties) but it's not helped.
Do you have any idea what it's the delay?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Where "mark : 560" signals a PWM burst of 560 µs and "space: 1600" signals silence for 1600 µs.
What does your oscilloscope show you? Where is the delay you mentioned?
I see three sources of delay:
1) Garbage Collection
If setting "arduino.GC=HAIKU_NoGC" then the GC should be turned off.
#define HAIKU_GC (HAIKU_NoGC)
Please take a look into your project "haikuDefs.h" file. Even if GC is switched on I don't expect a GC delay because in your central for-loop of sendNEC no GC run will be triggered. (It is possible but unlikely.)
2) Threading
Because you are not using Threads, HaikuVM should have detect this and should have set:
#define HAIKU_Threads (0)
Please take a look into your project "haikuDefs.h" file.
in "bytecodeAsJump.h" should be disabled. In consequence Thread-switching/scheduling is turned off and is no source of delay.
3) Timer interrupts
HaikuVM uses timer interrupts to manage time e.g. in System.currentTimeMillis(). See http://haiku-vm.sourceforge.net/#[[Tutorial%20All%20About%20MicroKernels]]
#define HAIKU_TimerInterrupt (TIMER0_OVF_vect)
Please take a look into your project "haikuDefs.h" file to see this definition.
The timer 0 interrupt is set up in ArduinoLib.init(). When calling ArduinoLib.init(), like you do in your main() function, then a timer 0 interrupt is a source of delay.
Last edit: genom2 2015-05-23
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
You wrote about delays. I see a delay in the column you marked "CORRECT". This confuses me!
I think you mixed up "CORRECT" and "INCORRECT"? When looking at your data, I think it is the other way around!? First column should be labeled "INCORRECT".
Please, send me one whole sweep. This is: all 32 Bits of data including start and end mark.
What about:
#define HAIKU_Threads ???
The File "bytecodeAsJump.h" should be in:
C:/haikuVM/myCProject/HaikuVM/aotVariants/
Last edit: genom2 2015-05-23
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
About delay in csv file. It's right file. Look:
First column (correct), row 376, have signal. But in second column (incorrect) not have signal and further not have signal.
Further from row 1005 first row have signal, but incorrect row not have signal.
I don't know about
Please, send me one whole sweep. This is: all 32 Bits of data including start and end mark.
I was send all data of oscilloscope - I do capture all signal. And I don't know what I can capture more signal. :(
Last edit: TimReset 2015-05-24
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Using delayMicroseconds() of HaikuVM is critical. I mapped it to millisecond resolution, because I found no platform independent way to implement it.
1) Use delayMicroseconds() of the Arduino IDE library
So I re-coded your IRremote.java to use delayMicroseconds() of the Arduino IDE. For this I had to:
a) declare delayMicroseconds() as native and annotate it as @NativeCFunction. (See: http://haiku-vm.sourceforge.net/#[[JNI%20using%20functions%20from%20a%20C%20library%20using%20annotations]])
b) to use it I have to compile with the Arduino IDE, so I have to haikufy it with configuration arduinoIDEUpload.
c) as a consequence I have to switch from main() to the setup()-loop()-pair of processing.
d) Finally, because HaikuVM has interpreter overhead, I have to tune your timing (NEC_BIT_MARK, NEC_ONE_SPACE, etc.) I did my very best to tune it, which is hard without using an oscilloscope.
hex and binary representation field is similarly - 0x80000000L and 0b10000000000000000000000000000000;
But when I use Config arduinoIDEUpload it's not work -
D:\arduino\HaikuVM-1.3.1\haikuVM\bin\../bin/haikuupload IrTest..hex
compile and upload using arduino.exe with last port and board settings
/arduino/HaikuVM-1.3.1/haikuVM/bin/../hardware/tools/avr/utils/bin/sh.exe: ./arduino.exe:
No such file or directory
make: *** [upload] Error 127
#############################################################
# error while uploading files
#############################################################
java.lang.Exception: Script 'D:\arduino\HaikuVM-1.3.1\haikuVM\bin\../bin/haikuupload.bat'
exited with errorcode=2
at haikuvm.pc.tools.HaikuVM.executeBlocking(HaikuVM.java:545)
at haikuvm.pc.tools.HaikuVM.call(HaikuVM.java:226)
at haikuvm.pc.tools.HaikuVM.haikuupload0(HaikuVM.java:206)
at haikuvm.pc.tools.HaikuVM.haiku0(HaikuVM.java:151)
at haikuvm.pc.tools.HaikuVM.main(HaikuVM.java:101)
I used Windows 7 and add ArduinoIDE folder to Path.
I deploy to ArduinoIDE manually - copy HaikuVM folder in ArduinoIDE library folder and deploy from ArduinoIDE.
Last edit: TimReset 2015-06-03
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Good day!
I tried rewrite code for IR https://github.com/shirriff/Arduino-IRremote
I write simple worked code on C++:
It's code - copy-paste from IRremote.cpp. I test it's code on my IR speaker and it work.
Before I rewrite it's code on Java:
This code send all bit's for value SPEAKER_IR_POWER but speaker not receive their. I tried use Arduino.delayMicroseconds instead WProgram.delayMicroseconds but it's not helped.
Do you have any idea what this not worked?
P.S. And how formating code on this forum?!
Last edit: genom2 2015-05-14
OT begin
"And how formating code on this forum?!"
Chris asked me the same question last year. My answer is here:
https://sourceforge.net/p/haiku-vm/discussion/general/thread/4ac7ed8a/#02d7/fb7a
OT end
Again, you gave me to little information:
a) Which Config are you useing? If not useing arduinoIDE or duemilanove? I'm confused because you write about Arduino.delayMicroseconds() AND WProgram.delayMicroseconds().
b) Your code uses Java type long. What Config:Mode are you useing: 16/32 or 32/64? Is 16/32 sufficient?
c) Try to debug with more Serial.println() statements.
a) I use configuration "arduino". I write Arduino.delayMicroseconds() but it's commented code - I tried use configuration "arduinoIDE" and compile and deploy with Arduino IDE but it's not help.
b) I'm use Mode 16/32. As default mode for config "arduino".
c) I tried debug out with Serial.print():
It's printing:
It's correct data.
WProgram.delayMicroseconds() is incorrect delay on microseconds - it's use:
Can I use "pure" (real) delay on microseconds on WProgram? Or I must use Arduino.delayMicroseconds() and compile this code with Arduino IDE? Although it's not helped.
Hello!
I used oscilloscope for view signal on IR LED. And I found their rules: in C++ code have a uniform signal, but in Java code have a delay without signal. Look like a GC perform. I tried disable GC (used arduino.GC=HAIKU_NoGC in HaikuVM.properties) but it's not helped.
Do you have any idea what it's the delay?
I'm expecting the following sequence of signals on your oscilloscope when sending SPEAKER_IR_POWER:
Where "mark : 560" signals a PWM burst of 560 µs and "space: 1600" signals silence for 1600 µs.
What does your oscilloscope show you? Where is the delay you mentioned?
I see three sources of delay:
1) Garbage Collection
If setting "arduino.GC=HAIKU_NoGC" then the GC should be turned off.
Please take a look into your project "haikuDefs.h" file. Even if GC is switched on I don't expect a GC delay because in your central for-loop of sendNEC no GC run will be triggered. (It is possible but unlikely.)
2) Threading
Because you are not using Threads, HaikuVM should have detect this and should have set:
Please take a look into your project "haikuDefs.h" file.
Therefore this piece of code:
in "bytecodeAsJump.h" should be disabled. In consequence Thread-switching/scheduling is turned off and is no source of delay.
3) Timer interrupts
HaikuVM uses timer interrupts to manage time e.g. in System.currentTimeMillis(). See http://haiku-vm.sourceforge.net/#[[Tutorial%20All%20About%20MicroKernels]]
Please take a look into your project "haikuDefs.h" file to see this definition.
The timer 0 interrupt is set up in ArduinoLib.init(). When calling ArduinoLib.init(), like you do in your main() function, then a timer 0 interrupt is a source of delay.
Last edit: genom2 2015-05-23
Thanks for reply!
I watched your file:
in haikuDefs.h
and I not found bytecodeAsJump.h file.
I add the attachment with signal. First column - correct signal, second column - incorrect signal. May be it's helped for analyze.
Last edit: TimReset 2015-05-23
You wrote about delays. I see a delay in the column you marked "CORRECT". This confuses me!
I think you mixed up "CORRECT" and "INCORRECT"? When looking at your data, I think it is the other way around!? First column should be labeled "INCORRECT".
Please, send me one whole sweep. This is: all 32 Bits of data including start and end mark.
What about:
The File "bytecodeAsJump.h" should be in:
C:/haikuVM/myCProject/HaikuVM/aotVariants/
Last edit: genom2 2015-05-23
Thanks for quick reply!
I have in haikuDefs.h
About delay in csv file. It's right file. Look:
First column (correct), row 376, have signal. But in second column (incorrect) not have signal and further not have signal.
Further from row 1005 first row have signal, but incorrect row not have signal.
I don't know about
I was send all data of oscilloscope - I do capture all signal. And I don't know what I can capture more signal. :(
Last edit: TimReset 2015-05-24
Using delayMicroseconds() of HaikuVM is critical. I mapped it to millisecond resolution, because I found no platform independent way to implement it.
1) Use delayMicroseconds() of the Arduino IDE library
So I re-coded your IRremote.java to use delayMicroseconds() of the Arduino IDE. For this I had to:
a) declare delayMicroseconds() as native and annotate it as @NativeCFunction. (See: http://haiku-vm.sourceforge.net/#[[JNI%20using%20functions%20from%20a%20C%20library%20using%20annotations]])
b) to use it I have to compile with the Arduino IDE, so I have to haikufy it with configuration arduinoIDEUpload.
c) as a consequence I have to switch from main() to the setup()-loop()-pair of processing.
d) Finally, because HaikuVM has interpreter overhead, I have to tune your timing (NEC_BIT_MARK, NEC_ONE_SPACE, etc.) I did my very best to tune it, which is hard without using an oscilloscope.
I stored the resulting IRremote.java here: https://sourceforge.net/p/haiku-vm/code/HEAD/tree/trunk/gallerie/src/main/java/avr/gallerie/user/TimReset/IRremote.java
2) Data
To see the data I expect, please run (with regular Java not HaikuVM): https://sourceforge.net/p/haiku-vm/code/HEAD/tree/trunk/gallerie/src/main/java/avr/gallerie/user/TimReset/IRremoteExpectedData.java
3) Sometimes good to know
Last edit: genom2 2015-05-25
Good day!
Thanks for answer! Sorry for late response.
I tried your code https://sourceforge.net/p/haiku-vm/code/HEAD/tree/trunk/gallerie/src/main/java/avr/gallerie/user/TimReset/IRremote.java and it's work!
I test it's code - undo the code to may example. And I found what important code - it's:
1) NEC_OFFSET - if I set NEC_OFFSET = 0 and it's not work.
2) @NativeCFunction
private static native void delayMicroseconds(int time); - if I used WProgram.delayMicroseconds and it's not work.
hex and binary representation field is similarly - 0x80000000L and 0b10000000000000000000000000000000;
But when I use Config arduinoIDEUpload it's not work -
I used Windows 7 and add ArduinoIDE folder to Path.
I deploy to ArduinoIDE manually - copy HaikuVM folder in ArduinoIDE library folder and deploy from ArduinoIDE.
Last edit: TimReset 2015-06-03
To summarize, it works if:
1) You let NEC_OFFSET = 204;
2) You use:
3) But, when you use Config arduinoIDEUpload it fails to C-compile and upload.
Please, try this recommended way:
(see http://haiku-vm.sourceforge.net/#[[Arduino%20IDE]])
If, like you did, deploying manually, you have to know exactly what to do. You have to know that arduino.exe is called like this:
(as defined in entry arduinoIDEUpload.Upload in file HaikuVM.properties.)
Therefore, putting arduino.exe into your path has no effect. All depends on "cd C:\arduino-1.0.5\libraries" (or similar).
[OT]
No, you are wrong: 0x80000000L and 0x80000000 are not similar if using with Java long.
To see this, please run (with regular Java or HaikuVM 32/64): https://sourceforge.net/p/haiku-vm/code/HEAD/tree/trunk/gallerie/src/main/java/avr/gallerie/user/TimReset/LongTest.java
Your are only right for the tiny non-standard micro cosmos of HaikuVM 16/32. In general I recommend to write
0b10000000000000000000000000000000L instead of
0b10000000000000000000000000000000.