st7735s: documentation, ready for merge
[cortex-from-scratch] / drivers / st7735s.c
index 9676738..a3c7117 100644 (file)
@@ -5,8 +5,19 @@
  * Initial version 
  * 
  * $DESCRIPTION$
+ * Basic driver for the ST7735s TFT screen. Initializes the screen 
+ * Low-level commands can be called by tft_command. See header (.h)
+ * file for an overview of all commands. tft_command can accepts
+ * an unlimited amount of data or parameter commands.
  * 
  * $USAGE$
+ * I added the following functionality:
+ * a. tft_fill: fills a certain area of the screen
+ * b. tft_setpixel: sets a single pixel
+ * c. tft_putc, tft_put: outputs a char/string to the screen, starts
+ * at the upper top right and tracks the position. If the screen is
+ * full, it 'scrolls' automatically. So you basically have a mini 
+ * sized terminal. You can link standard output to this function!
  *
  * */
 
@@ -23,6 +34,7 @@
 #include <lib/tinyprintf.h>
 #include <lib/fonts/wogfont.h>
 
+#include <drivers/uart.h>
 #include <drivers/st7735s.h>
 
 #define TIMEOUT 500
@@ -34,6 +46,7 @@
 #define YPOS(y) (y * 8)
 #define BUFFER 352
 
+
 static struct {
         uint16_t cpos;
         uint8_t * textmemptr;
@@ -187,6 +200,7 @@ tft_command(TFT_RAMWR, 2, (uint8_t) (color >> 8), (uint8_t) (color & 0xFF));
 }
 
 
+/* Basic puts function that loops over a string */
 int tft_puts(char * str) {
 
        for (int i = 0; i < strlen(str); i++)  {
@@ -196,6 +210,8 @@ int tft_puts(char * str) {
        
 }
 
+/* Used by scroll function to overwrite and clear the last
+ * line on the screen */
 void tft_clrln() {
 
        tft_puts("                     ");
@@ -210,20 +226,28 @@ int tft_scroll() {
 
        /* Scroll the buffer  */
        memcpy(tftscreen.textmemptr, tftscreen.textmemptr + 21, BUFFER - 21);
-       for (int i = 21; i >= 0; i--)
-               tftscreen.buf[BUFFER - 21] = '\0';
+       tftscreen.buf[BUFFER - 21] = '\0';
 
        tftscreen.x = 0;
        tftscreen.y = 0;
        tftscreen.cpos = 0;
-       
+
        tft_puts(tftscreen.buf); // CHECK: ending
-       tft_clrln();
+       tftscreen.y = 14;
+       tftscreen.x = 0;
+       tftscreen.cpos = BUFFER - 21;
 }
 
-void tft_linefill() {
+/* Fills a line with blank characters, returns to 
+ * the next line */
+void tft_nl() {
+
+       uint8_t blanks = 21 - tftscreen.x;
 
-       // TODO
+       // filler with blank spaces
+       for (int i = 0; i < blanks; i++) {
+               tft_putc(0xFFFF, 0x0000, ' ');
+       }
        
 }
 
@@ -231,12 +255,23 @@ void tft_linefill() {
  * Should not be used directly */
 int tft_putc(uint16_t fg, uint16_t bg, int c) {
 
-       
        int totalpixels = 35;
        int column = 0;
        int row = 0;
        uint8_t current;
 
+       if (c == '\n') {
+               if (tftscreen.y == 14) {
+                       tft_nl();
+                       return 1;
+               }
+               else {
+                       tft_nl();
+               //if (tftscreen.y < 15)
+                       return 1;
+               }
+       }
+
        if (tftscreen.y >= 15) {
                tft_scroll();
        }