frame

How to Redirect Print() Output

I am trying to port an mbed cpp code ( FreetronicsLCDShield) to Zerynth or write a similar code here.  In the example it says:
 
"The class inherits from stream, therfore writing to the display is as easy as calling printf() to display text or putc() to display a custom character   Example:  <instanceName>.printf("Hello World"); 

See lines 54-59 here: http://https//os.mbed.com/teams/components/code/Freetronics_16x2_LCD/file/0e04b6c4abb8/freetronicsLCDShield.h/

Some more explanations here: http://https//os.mbed.com/forum/helloworld/topic/1472/?page=1#comment-16060

 How can I implement the same in Zerynth with Python+C ? As far as I understand -if I am correct-  mbed Cpp has an advantage of writing byte data while we have to do it bitwise here and therefore It seems to me that redirecting print() of text data to LCD display could be easily implemented like redirecting print() to a memory location or file etc. if we could have byte write possibility. 

I will appreciate your valuable comments and advise on how to redirect stdout to elsewhere in general and in particular to a register of an LCD driver.

Comments

  • Hi ta1db,

    to redirect a print in Zerynth you have to pass as argument the stream in which you want to print; in this example code has been shown how to define serials and how to print on those:

    import streams
    
    streams.serial()
    ser1 = streams.serial(SERIAL1,baud=9600,set_default=False)
    ser2 = streams.serial(SERIAL2,baud=115200,set_default=False)
    
    while True:
        print("default serial with default baud rate (115200)")
        print("serial1 with baud rate set to 9600",stream=ser1)
        print("serial2 with baud rate set to 115200",stream=ser2)
        sleep(1000)
    If the component is manageable by serial communication and accept strings as command/data you can implement your LCD driver following this example passing the related serial connected to the LCD display.

    Keep me posted
    Matteo Cipriani
    Zerynth Support Team
  • Dear Matteo,
    Thank you very much for your valuable explanations. Printing to a serial port with different settings is very clear to me. However I can't imagine printing ascii content to a paralel data port consisting 4 bits (or even 8 bits). Can I apply your above example to SocketStream, FileStream, ResourceStream as well by substituting "serial" with one of these like 

    streams.SocketStream()
    ser1 = streams.SocketStream(...)
    while True:
        print("print to Socket", stream=ser1)

    By the way I found here that read function of ADC module accepts lists or tuples of pins, I tried to do the same with digitalWrite function with hope of writing to byte or nibble but it didn't work.
  • Hi ta1db,

    print builtin function takes as argument any kind of stream subclasses but, because of it is usually used for print on a screen, it treats only strings.

    If you need to send also commands, a sequence of bytes, etc. you can use the write() function that every stream subclass has (here you can find the doc).

    Let me know if this can help you :)
    Matteo Cipriani
    Zerynth Support Team
Sign In or Register to comment.

ZERYNTH Community

@ 2016 Zerynth.com, all rights reserved.