calebh / Juniper

https://www.juniper-lang.org/
MIT License
72 stars 8 forks source link

blink example #1

Closed sensor111 closed 4 years ago

sensor111 commented 4 years ago

@calebh I tested blink example on arduino and surprisingly it's blinking only 33 times and led stays off. After reset again: led blinks 33 times and off.

calebh commented 4 years ago

It's possible that there are heap allocations happening which results in fragmented memory and eventually allocation failure. This may occur because closures are heap allocated instead of stack allocated. The solution would be to move the closure type to be stored in the function type, making them stack allocated. Unfortunately I don't have time to update Juniper right now as I've moved on to some new research projects. Ideally the next version of Juniper would include typeclasses as well as the closure function types.

What Arduino are you using?

sensor111 commented 4 years ago

Thanks @calebh for swift answer. The arduino is marked as original arduino board model UNO. I think it was very interesting project and regret you got no time to carry on with it. Do you know any other project about allowing for functional programing on microcontrolers? Especially ESP 8266 or 32

calebh commented 4 years ago

Hmm, I've tested the blink example with the UNO before and it worked for me. It's possible that I did not watch the blinking long enough though. Unfortunately I lost my UNO in a move (along with a bunch of other circuit stuff I had), and the only microcontroller I have right now is a Adafruit Circuit Express.

I am not aware of any other functional languages that are as mature as Juniper that run on systems as small as an Arduino. However there are a couple of other projects that you may be interested in:

I can take a look at the closure memory allocation issue this weekend and see if I can get a quick fix for you.

jprivate50 commented 4 years ago

Hi Caleb, thanks for the interesting links. Have you any success to trace the bug?

pre63 commented 4 years ago

@sensor111 @calebh

I had the same issue with an Arduino Nano 33 BLE. The led stoped blinking after exactly 9 minutes every time I updated the board with juniper code, no matter what the code did, but when using the Arduino's Blink example it was fine, the blinking would go on for hours.

I really wanted to use juniper for programming because I find it so dreadful to write imperative code like c++. Also juniper is quite a nice language with some of my favorite goodies like pattern matching, lambdas, algebraic data types and implicit type inference.

You are already here so you get the point I was really determined not to give up on FP and code a bunch of c++.

Thus, I started removing code form the c++ output in hopes to narrow the code leaking memory and fix it. Since it took 9 minutes testing each hypothesis, I spend a day doing that. But in the end the simplest code without any Juniper generated code had the same problem.

It turns out that by using a scheme like this below with a main function and a while true loop causes the issue.

void main() {
  ...
  while true {
  ...
  }
  ...
}

From what I can tell, the Arduino lib can't determine that you are running code so it puts the device to sleep. To solve this you have to use the setup and loop functions to use the Arduino's features and keep the device on.

In layman terms you must use setup and loop that are called by the Arduino library. Like in every Arduino code example.

void loop() { ... }
void setup() { ... }

Also turn's out that is solves the usb serial being unavailable for the serial monitor or the screen command after upload.

I solved that in PR #3. It's been blinking for over 12 hours. ;)

Cheers!

calebh commented 4 years ago

Absolute legend! @sensor111 @jprivate50 would you be able to test the fix before I close this issue?

sensor111 commented 4 years ago

unfortunately when I tried to compile blink.jun: Unhandled Exception: System.Exception: Unable to find program entry point. Pleas e create a function called main. at Compiler.compileProgram(FSharpList1 program_0, FSharpList1 program_1, FS harpList1 program_2, FSharpList1 program_3, FSharpList1 program_4) in C:\User s\caleb\Documents\Visual Studio 2015\Projects\Juniper\Juniper\Compiler.fs:line 6 80 at Program.main$cont@55(FSharpList1 sourceFiles, String outputFile, Unit uni tVar) in C:\Users\caleb\Documents\Visual Studio 2015\Projects\Juniper\Juniper\Pr ogram.fs:line 89 at Program.main(String[] argv) in C:\Users\caleb\Documents\Visual Studio 2015 \Projects\Juniper\Juniper\Program.fs:line 55

calebh commented 4 years ago

Did you recompile the compiler from the latest source from GitHub? Or are you using the prebuilt compiler from the website? The problem is with the compiler, not the blink.jun example

sensor111 commented 4 years ago

I used the old compiler created by windows installer in April cos the juniper.sln on github was 4 years unchanged.

On Fri, 29 May 2020 at 02:17, Caleb Helbling notifications@github.com wrote:

Did you recompile the compiler from the latest source from GitHub? Or are you using the prebuilt compiler from the website? The problem is with the compiler, not the blink.jun example

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/calebh/Juniper/issues/1#issuecomment-635701367, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACIPKSV7OFAOJP3HH5DQ7C3RT4EMPANCNFSM4MH4HXTA .

pre63 commented 4 years ago

@sensor111 I changed the compiler to require setup and loop functions instead of main. So you need to recompile it with the latest commit on master.

calebh commented 4 years ago

@sensor111 If you aren't able to compile the compiler for some reason I can build it for you. Are you on Windows?

sensor111 commented 4 years ago

Thanks @calebh - I'm trying to swim by myself ;-) But I got another problem after I build the compiler and run it:

C:\Users\augug\source\repos\Juniper\Juniper\bin\Debug>juniper.exe -h Juniper 2.3.0 usage: Juniper.exe -s s1.jun s2.jun ... sn.jun -o main.cpp options: -s, --source: The .jun Juniper source files to compile -o, --output: The file in which the compiled C++ is written -h, --help: View this help message

C:\Users\augug\source\repos\Juniper\Juniper\bin\Debug>juniper.exe -s blink.jun -o blinkjuniper

Unhandled Exception: System.ArgumentException: The input must be non-negative. count = -1 Parameter name: count at Microsoft.FSharp.Core.StringModule.Replicate(Int32 count, String str) in F:\workspace_work\1\s\src\fsharp\FSharp.Core\string.fs:line 79 at Compiler.output(String str) in C:\Users\augug\Source\Repos\Juniper\Juniper\Compiler.fs:line 23 at Compiler.compileProgram(FSharpList1 program_0, FSharpList1 program_1, FSharpList1 program_2, FSharpList1 program_3, FSharpList`1 program_4) in C:\Users\augug\Source\Repos\Juniper\Juniper\Compiler.fs:line 699 at Program.main(String[] argv) in C:\Users\augug\Source\Repos\Juniper\Juniper\Program.fs:line 89

calebh commented 4 years ago

@sensor111 That issue should be fixed on master now

sensor111 commented 4 years ago

33 heartbeats and counting ;-))

Can we have now solution for esp8266, please? :-)

calebh commented 4 years ago

@sensor111 There are some examples of C++ library wrappers in the wrappers directory if you'd like to write your own wrapper for esp8266. https://github.com/calebh/Juniper/tree/master/Juniper/wrappers

calebh commented 4 years ago

Juniper 2.3.0 has been released which fixes this issue.