; Examples for the GIF decode/encode extension v2 ; see http://www.ulisp.com/show?3IUQ ; Display an image from an SD Card (with-sd-card (str "JapanCat.gif") (decode-gif str)) ; Control how an image is displayed (with-sd-card (str "JapanCat.gif") (display-gif str #'(lambda (x y c) (draw-pixel (- 319 x) y c)))) ; Save a screendump to an SD card (defun screendump (filename &optional (colours 256)) (bind (width height) (display-size) (with-sd-card (str filename 2) (encode-gif str width height colours)))) ; Encode an image defined by a function and save it to SD card (defun ellipses (xx yy colours) (bind (width height) (display-size) (let* ((x (- xx (truncate width 2))) (y (- yy (truncate height 2))) (f (truncate (+ (* x (+ x y)) (* y y)) 17))) (mod f colours)))) (defun test2 () (bind (width height) (display-size) (let ((colours 64)) (with-sd-card (str "ellipses.gif" 2) (encode-gif str width height colours ellipses))))) (with-sd-card (str "ellipses.gif") (decode-gif str)) ; Encode with an alternative encoding (defun test2 () (bind (width height) (display-size) (let ((colours 64)) (with-sd-card (str "ellipse2.gif" 2) (encode-gif str width height colours ellipses t))))) ; Display the palette for a given number of colours (defun palette (colours) (bind (xsize ysize) (display-size) (let* ((p (- (integer-length colours) 2)) (xn (ash 1 (truncate (+ p 2) 2))) (yn (ash 1 (truncate (+ p 1) 2))) (h (truncate xsize xn)) (v (truncate ysize yn)) (filename (format nil "P~a.gif" colours))) (with-sd-card (str filename 2) (encode-gif str xsize ysize colours #'(lambda (x y colours) (+ (* (truncate y v) xn) (truncate x h)))))))) (with-sd-card (str "P256.GIF") (decode-gif str)) ; Encode an image with fewer colours (defun test3 () (bind (width height) (display-size) (with-sd-card (str "JapanCat.gif") (decode-gif str)) (with-sd-card (str "Cat2.gif" 2) (encode-gif str width height 64)) (with-sd-card (str "Cat2.gif") (decode-gif str)))) ; Load an image from the web (wifi-connect "Geronimo" "secret99") (defun display-pic () (with-client (s "www.ulisp.com" 80) (format s "GET /pictures/3j/japancat240x135.gif HTTP/1.0~a~%" #\return) (format s "Host: www.ulisp.com~a~%" #\return) (format s "Connection: close~a~%" #\return) (format s "~a~%" #\return) (loop (when (= (length (read-line s)) 1) (return))) ; Skip headers (decode-gif s))) ; Capture a screen display over the web (defun clock () (wifi-server) (format t "Connect your web browser to ~a~%" (wifi-localip)) (bind (xs ys) (display-size) (let ((lastminute -1)) ; Wait for time (loop (when (get-time) (return))) ; Update time (loop (bind (year month day hour minute &rest others) (get-time) (unless (= minute lastminute) (fill-screen) (with-gfx (str) (set-text-size 8) (set-cursor 0 40) (set-text-color #xf81f) (format str "~a:~2,'0d" hour minute)) (setq lastminute minute))) ; Check for web client (serve-gif) (delay 10000))))) (defun serve-gif () (bind (xs ys) (display-size) (with-client (s) (loop (when (= (length (read-line s)) 1) (return))) (format s "HTTP/1.1 200 OK~a~%" #\return) (format s "Content-Type: image/gif~a~%" #\return) (format s "Connection: close~a~%" #\return) (format s "~a~%" #\return) (encode-gif s xs ys 8)))) ; Return a GIF generated from a function over the web (defun plot (x y colours) (truncate (* (1- colours) (/ (+ 2 (cos (/ (- x (/ 320 2)) 20)) (cos (/ (- y (/ 240 2)) 20))) 4)))) (defun 3dplot () (wifi-server) (format t "Connect your web browser to ~a~%" (wifi-localip)) (loop (with-client (s) (loop (when (= (length (read-line s)) 1) (return))) (format s "HTTP/1.1 200 OK~a~%" #\return) (format s "Content-Type: image/gif~a~%" #\return) (format s "Connection: close~a~%" #\return) (format s "~a~%" #\return) (encode-gif s 320 240 64 plot)) (delay 10000)))