Use of [stream]


#1

Hi
How do I use the stream function if I want for example to print chars to an attached lcd character display

Thanks RonnySuy


#2

There isn’t a function stream. When you create a stream, with a call such as with-spi, a value representing the stream is bound to a variable you provide. You write to or read from the stream by giving the stream variable as an argument to read, print etc.

If your LCD display has an SPI interface you could connect to it, and write “Hello”, with something like:

(with-spi (str 7) 
  (print "Hello" str))

#3

Thanks

in your example does the number 7 refer to something ?
in with-i2c it seems to refer to the i2c address , is that correct. ?
But spi doesn’t use addresses , it uses a CS pin for selecting the device.

So I can use any spi controlled display to send data with print etc ?

Maybe you have a good reference that I can consult ?

Thanks Ronny


#4

So I can use any spi controlled display to send data with print etc ?

Yes! The number 7 refers to the CS pin. See:

http://www.ulisp.com/show?3L#withspi


#5

Ok , think I got it a bit.

So also I2C can be used , stream is than the i2c address , and even using-serial could be done with str 1 for being the second serial port. Am I correct on this ? guess so)

Fascinating !
I can not find an spi controlled display at this time , but i2c seems to be common use.

Thanks again !


#6

There’s an I2C example here:

Scrolling text display

For that display I had to provide the character definitions, but some displays have them built in.


#7

Oh Yes , seen that example , but I’m talking about a 20*4 lines character lcd :-)
Kind regards,

Ronny Suy


#8

I assume you mean something like this:

Standard LCD 20x4

Those displays are alphanumeric so you can send ASCII codes, but you have to send the data 4 bits at a time and the display isn’t standard I2C or SPI. You’ll need to connect 6 pins to the display (4 data pins plus RS and EN), and write your own Lisp code to send the characters, but it should be pretty straightforward.

If you get stuck let me know, and I’ll have a go; I think I’ve got one of those displays somewhere.


#9

Yes

I have an hd44780 compatible display , and past weekend I’ve bought an i2c backpack for
the display.
See hobbytronics.co.uk

Regards , Ronny


#10

That’s a good solution. Let me know how you get on,
David


#11

I sure will.
I expect the backpack end this week.

Ronny


#12

Hi David

I have some good progression with my i2c controlled lcd display
I send hereby my code sofar and a photo.
I’m gonna build a weather station in lisp :-)
I’m using an mkr zero board , a ds1338 rtc , a bme280 temp,humidity and pressure sensor and a i2c controlled display
So far I’m displaying time and date on the first line of the display.
I still have to write the code for the sensor.
Hope you like it !

Kind regards ,
Ronny Suy

;; scani2c : detect all connected i2c devices on the bus
;;           and print the base address in hex

(defun scani2c ()
  (dotimes (p 127)
    (with-i2c (str p)
      (when (restart-i2c str) (princ (* p 2)) (terpri) ))))

 ;; define some global variables

(defvar seconds)
(defvar minutes)
(defvar hour)
(defvar day)
(defvar date)
(defvar month)
(defvar year)

;; -------------------------------------------------------
;;  pblcd : convert a decimal byte to hex and send to lcd
;; -------------------------------------------------------

(defun gethxlcd (nr)
  (princ (subseq "0123456789ABCDEF" nr (1+ nr)) str))

(defun pblcd (byte)
  (gethxlcd (truncate (/ byte 16)))
  (gethxlcd (truncate (mod byte 16))))

;; ----------------------------
;; RTC functions
;; Read the time from the RTC
;; RTC is a DS1338 chip
;; ----------------------------

(defun rtc ()
  (with-i2c (str #x68)
    (write-byte 0 str)
    (restart-i2c str 7)
    (setq seconds (read-byte str))
    (setq minutes (read-byte str))
    (setq hour (read-byte str))
    (setq day (read-byte str))
    (setq date (read-byte str))
    (setq month (read-byte str))
    (setq year (read-byte str))))
	
;; setclock :
;; Set sec min hr day dow month year
;; Note : use #x input because numbers must be hex !
;; dow : monday = 1 , ...sunday=7

(defun setclock (sec min hr dow day month year)
  (with-i2c (str #x68)
    (write-byte 0 str)
    (write-byte sec str)
    (write-byte min str)
    (write-byte hr str)    
    (write-byte dow str)
    (write-byte day str)
    (write-byte month str)
    (write-byte year str)))

;;	-------------------------------
;;	BME280 Breakout board functions
;;	-------------------------------

(defun initbme ()
  (with-i2c (str #x76)
    (write-byte #xf2 str)
    (write-byte #x01 str))
  (with-i2c (str #x76)
    (write-byte #xf4 str)
    (write-byte #x25 str)))

;;	------------------------------------
;;	I2C controlled lcd display functions
;;	------------------------------------

(defun initlcd ()

  ;set backlight on, half dimmed
  (with-i2c (str #x38)
    (write-byte 7 str)
    (write-byte 128 str))
  (delay 1)

  ;set display to 20x4
  (with-i2c (str #x38)
    (write-byte 5 str)
    (write-byte 4 str)
    (write-byte 20 str))
  (delay 1)

  ;clear the display
  (with-i2c (str #x38)
    (write-byte 4 str))
  (delay 1))


(defun ln1 ()

  ; write time & date to the lcd on line 1	 
  (with-i2c (str #x38)
    (write-byte 2 str)
    (write-byte 0 str)
    (write-byte 0 str))
  (delay 1)
  (rtc)
  (with-i2c (str #x38)
    (write-byte 1 str)
    (pblcd hour)
    (princ ":" str)
    (pblcd minutes)
    (princ "   "  str)
    (princ (subseq "MonThuWedThuFriSatSun" (* 3 (- day 1)) (* 3 (1+ (- day 1)))) str)
    (princ " " str)
    (pblcd date)
    (princ "/" str)
    (pblcd month)
    (princ "/" str)
    (pblcd year))
  (delay 1))

#13

Glad you got it working, and thanks for the listing (I’ve formatted it slightly).

David