Shop OBEX P1 Docs P2 Docs Learn Events
C learning Print function — Parallax Forums

C learning Print function

SteveW719SteveW719 Posts: 40
edited 2015-05-21 11:00 in Learn with BlocklyProp
I was experimenting with some of the Learn C examples which use print function. I can't get anything with print function to compile if I use printf function it compiles fine. I searched the simple tools reference but no print. Could someone please point me to a reference or explain the difference between print and printf?

I eventually want to print to the terminal from different cores which seems to be another issue.

Thanks
Steve

Comments

  • Hal AlbachHal Albach Posts: 747
    edited 2015-05-06 10:13
    What is the error message when you compile a program with print() function?
  • ElectrodudeElectrodude Posts: 1,660
    edited 2015-05-06 10:22
    You're probably forgetting an #include. Are you including <stdio.h> or <stdlib.h> or both?

    I have a library for multicog print - I don't have access to the computer it's on right now, but I'll try to upload it to github or to the Propeller Object Exchange when I can. It gives each cog its own fake serial device that just writes stuff into a circular buffer, and then a cog constantly goes through all of the buffers and, when it finds one with a complete line, it prints a header (i.e. "[cog 0] ") and then the line. IOW, it lets multiple cogs use the terminal at the same time and makes sure their lines don't get mixed up with each other, at the expense of a cog.
  • edited 2015-05-06 11:48
    There are two approaches to printing information from other cogs. The first is to use one cog as an information conduit, like here:

    http://learn.parallax.com/multicore-approaches/cores-sharing-data

    The other is stop the terminal process in one cog, then restart it in another, like this:

    http://learn.parallax.com/multicore-approaches/print-different-core

    There's an error in the Print from a Different Cor page. It is corrected in the example program in SimpleIDE. cog_run(..., 10) changed to cog_run(...128).
  • edited 2015-05-06 11:52
    P.S. Make sure to Set up SimpleIDE before trying the suggestions in the previous post. If you compile the examples in SimpleIDE, it'll have the libraries, and they will be declared correctly in the code examples.
  • Steph LindsaySteph Lindsay Posts: 767
    edited 2015-05-06 14:38

    http://learn.parallax.com/multicore-approaches/print-different-core

    There's an error in the Print from a Different Core page. It is corrected in the example program in SimpleIDE. cog_run(..., 10) changed to cog_run(...128).

    The page has been updated.
  • SteveW719SteveW719 Posts: 40
    edited 2015-05-07 10:03
    Thanks for the help. I have installed the newest version 0.9.66 on my Mac to continue my study and it won't build now seems I am missing a library files.

    The error is Simpletext.h no such file or directory i. I don't know what happened. I tried to reinstall it a couple times but same result.
  • Steph LindsaySteph Lindsay Posts: 767
    edited 2015-05-07 10:43
    Hi Steve,

    The latest version for Mac is 1.0 RC2, available for download from this page:

    http://learn.parallax.com/propeller-c-set-simpleide/mac

    0.9.66 was linked on this page as well, I have removed it to
  • SteveW719SteveW719 Posts: 40
    edited 2015-05-07 11:49
    I have installed new version

    I still get error missing simple text.h when I try to build.

    I don't know what else to try.

    Thanks,
  • SteveW719SteveW719 Posts: 40
    edited 2015-05-07 11:59
    Screen Shot 2015-05-07 at 2.54.34 PM.png


    I tried to attach a screen shot of the error message.
  • jazzedjazzed Posts: 11,803
    edited 2015-05-07 12:00
    Please post your program and the build error rescue output.

    I don't see any reference to simpletext in your screen shot build info. The build error rescue output is more complete. It's in the help menu.
  • edited 2015-05-07 15:15
    The build error rescue output jazzed refers to is obtained by clicking Help and selecting Build Error Rescue.
  • SteveW719SteveW719 Posts: 40
    edited 2015-05-08 06:50
    Thank you for the reply. I deleted everything all the folders and reinstalled. It seems to be working now. Thanks for the help.
  • ElectrodudeElectrodude Posts: 1,660
    edited 2015-05-19 20:07
    Here's my multi-cog debug library: https://github.com/electrodude/libserialmerge

    I'm not sure I used the standard SimpleIDE naming convention, but libserialmerge.c is a demo, while serialmerge.{c,h} are the actualy library files. SimpleIDE seems to be fine with it.

    It doesn't do input yet, and uses a whole cog besides FullDuplexSerial, but I'm thinking that both of these could be solved by doing serial in the multiplexer cog and then only looking for input when a cog requests it (pausing other cogs' streams until a newline comes). You can only have one terminal per cog, as the output buffer to use is automatically selected by cogid. If anyone's interested, I can properly document it and add the input stuff, but I don't use PropGCC that much, so I probably won't be needing these improvements for myself any time soon.
  • DavidZemonDavidZemon Posts: 2,973
    edited 2015-05-20 19:18
    I'm not sure how we got on the topic of multi-cog serial, but I'll happily jump in too :)

    PropWare has synchronous output as well via the SynchronousPrinter class. Example usage here.
  • ElectrodudeElectrodude Posts: 1,660
    edited 2015-05-20 20:10
    I'm not sure how we got on the topic of multi-cog serial, but I'll happily jump in too :)
    SteveW719 wrote: »
    I eventually want to print to the terminal from different cores which seems to be another issue.

    I should really finish my serial merger library, it will make a lot more sense then... The main benefit of mine is that it has no locks - cogs never have to wait, each cog just throws data in its own circular buffer. The only problem is when buffer overflows happen - right now, it just drops data because losing debug info was better than any delays in the application I wrote it for. Even after I add input, it will still need no locks.
  • DavidZemonDavidZemon Posts: 2,973
    edited 2015-05-20 20:58
    Ah, I didn't catch that last sentence about him wanting to print from multiple cores.
    I should really finish my serial merger library, it will make a lot more sense then... The main benefit of mine is that it has no locks - cogs never have to wait, each cog just throws data in its own circular buffer. The only problem is when buffer overflows happen - right now, it just drops data because losing debug info was better than any delays in the application I wrote it for. Even after I add input, it will still need no locks.

    How do you avoid two cogs writing to the same byte in the buffer? You need to lock your buffer I think, or just accept the risk of sometimes overwriting data. Having buffered UART impls does help a lot with execution time. Thankfully, anything that implements the PrintCapable interface can be used with the SynchronousPrinter (by creating a Printer instance). All you have to do is write (or use an existing) object that has two methods (put_char and puts) and uses a buffered implementation of UART. I've started this, but got side tracked trying to finish the SD implementation in PropWare. For anyone reading this that is interested, feel free to follow the progress of this GitHub issue. Once that issue is complete, buffered, synchronous UART will be available to all :D
  • ElectrodudeElectrodude Posts: 1,660
    edited 2015-05-21 11:00
    How do you avoid two cogs writing to the same byte in the buffer? You need to lock your buffer I think, or just accept the risk of sometimes overwriting data. Having buffered UART impls does help a lot with execution time. Thankfully, anything that implements the PrintCapable interface can be used with the SynchronousPrinter (by creating a Printer instance). All you have to do is write (or use an existing) object that has two methods (put_char and puts) and uses a buffered implementation of UART. I've started this, but got side tracked trying to finish the SD implementation in PropWare. For anyone reading this that is interested, feel free to follow the progress of this GitHub issue. Once that issue is complete, buffered, synchronous UART will be available to all :D

    Every cog gets its own buffer. The serial merger cog looks at each cog's buffer in turn, and for each that has at least one newline somewhere, it prints everything until and including the first newline. If two cogs want to print stuff at the same time, one doesn't have to wait for the other (unless one of their buffers is full, which this doesn't really handle right now...). Both cogs just throw stuff in their buffers, and the serial merger cog eventually gets around to actually printing it. Yes, it uses more ram, but for my application there was plenty of free ram and speed (i.e. no lock contention) was of highest importance.

    For input (which is not implemented yet), the rx function will set a variable in the calling cog's buffer struct to -1 and then busywait until it changes and then return the new value. If the serial merger cog sees -1 in a cog's input field, it will completely flush that cog's buffer, regardless of whether there's a newline at the end of the last line, and then wait for input, servicing no other cogs' buffers in the meantime, and then continue to read input until that cog prints a newline or some other value signifying that it's done.

    To prevent other cogs' buffers from overflowing while one is receiving input, I might do something like have it keep the inputting cog's last line and the part of the line entered so far, printing other cogs' data but then reprinting the inputting cog's stuff at the bottom. This would require full duplex serial (another cog!) and a non-echoing terminal, though (so the inputted characters don't get mixed up in other cogs' output).
Sign In or Register to comment.