Anonymous avatar Anonymous committed 9394c7e Draft

bufix : lcd.py is still experimental but not so far

Comments (0)

Files changed (1)

src/platform/stm32f4/lcd.py

 #             If you're looking for detail :
 #             http://code.google.com/p/arduino/source/browse/trunk/libraries/LiquidCrystal/LiquidCrystal.cpp
 #  
+#             WARNING, THIS IS STILL EXPERIMENTAL
+#  
 #  thx :     arduino LiquidCrystal.cpp
 #
 # Author:      Stéphane Bard
 import gpio
 import sys
 
-LCD_CURSOROFF=0x00
-LCD_BLINKON=0x01
-LCD_BLINKOFF=0x00
-LCD_DISPLAYMOVE=0x08
-LCD_CURSORMOVE=0x00
-LCD_MOVERIGHT=0x04
-LCD_MOVELEFT=0x00
-LCD_8BITMODE=0x10
-LCD_4BITMODE=0x00
-LCD_2LINE=0x08
-LCD_1LINE=0x00
-LCD_5x10DOTS=0x04
-LCD_5x8DOTS=0x00
+LCD_CLEARDISPLAY = 0x01
+LCD_RETURNHOME = 0x02
+LCD_ENTRYMODESET = 0x04
+LCD_DISPLAYCONTROL = 0x08
+LCD_CURSORSHIFT = 0x10
+LCD_FUNCTIONSET = 0x20
+LCD_SETCGRAMADDR = 0x40
+LCD_SETDDRAMADDR = 0x80
+LCD_ENTRYRIGHT = 0x00
+LCD_ENTRYLEFT = 0x02
+LCD_ENTRYSHIFTINCREMENT = 0x01
+LCD_ENTRYSHIFTDECREMENT = 0x00
+LCD_DISPLAYON = 0x04
+LCD_DISPLAYOFF = 0x00
+LCD_CURSORON = 0x02
+LCD_CURSOROFF = 0x00
+LCD_BLINKON = 0x01
+LCD_BLINKOFF = 0x00
+LCD_DISPLAYMOVE = 0x08
+LCD_CURSORMOVE = 0x00
+LCD_MOVERIGHT = 0x04
+LCD_MOVELEFT = 0x00
+LCD_8BITMODE = 0x10
+LCD_4BITMODE = 0x00
+LCD_2LINE = 0x08
+LCD_1LINE = 0x00
+LCD_5x10DOTS = 0x04
+LCD_5x8DOTS = 0x00
 
 # TODO: rewrite example for STM32F4
+# TODO: write int method in builtin (this is not possible !!!!)
 # TODO: rewrite easy function to make a digital_write with simple parameter
 
 class LiquidCrystal(object):
 
     def __init__(self, rs, rw, enable, d0, d1, d2, d3, d4=0, d5=0, d6=0, d7=0):
         """
-        each pin should be string like 'E7'
+        each pin should be string like ('E',7)
 
         rw argument is kept in the same position.
 
                 LiquidCrystal(rs, None, enable, d0, d1, d2, d3, d4, d5, d6, d7)
                 LiquidCrystal(rs, rw, enable, d0, d1, d2, d3, d4, d5, d6, d7) 
                 
-                d0, d1, d2, d3 ... should be STM32F4 string composed
-                by letter and a digit like "E7" etc ...
+                d0, d1, d2, d3 ... should be utple composed
+                by letter and an integer like ("E",7) etc ...
 
+                    # RW => E7
+                    # E => E8
+                    # D4 => E9
+                    # D5 => E10
+                    # D6 => E11
+                    # D7 => E12
 
+                    from lcd import LiquidCrystal
+                    screen = LiquidCrystal(("E",7), None, ("E",8), ("E",9), ("E",10), ("E",11), ("E",12))
+                    screen.begin(16,2)
+                    screen.write("2")
         """
         # value authorized for rw are string 'E7' or None !
         if rw is None :
             rw = 255
-
         # four_bit_mode is fixed by non present keywords ...
         four_bit_mode = 0
         if not d4 or not d5 or not d6 or not d7 :
 
         self.set_output(self.enable_pin)
 
-        if fourbitmode :
+        if four_bit_mode :
             self._displayfunction = LCD_4BITMODE | LCD_1LINE | LCD_5x8DOTS
         else :
             self._displayfunction = LCD_8BITMODE | LCD_1LINE | LCD_5x8DOTS
-
         self.begin(16, 1)
 
-    def begin(self, cols, lines, dotsize):
+    def dg_write(self, s_pin, value):
+        """
+        """
+        name_pin_part = s_pin[0]
+        int_pin_part = s_pin[1]
+        gpio.digital_write(name_pin_part, int_pin_part, value)
+
+    def pn_mode(self, s_pin, value):
+        """
+        """
+        name_pin_part = s_pin[0]
+        int_pin_part = s_pin[1]
+        gpio.pin_mode(name_pin_part, int_pin_part, value)
+
+    def begin(self, cols, lines, dotsize=0):
         """
         """
         if lines > 1 :
 
         self._numlines = lines
         self._currline = 0
-
+        
         if dotsize != 0 and lines == 1 :
             self._displayfunction |= LCD_5x10DOTS
 
         sys.wait(50)
 
         # Now we pull both RS and R/W low to begin commands
-        gpio.digital_write(self.rs_pin[0], int(self.rs_pin[1]), gpio.LOW)
-        gpio.digital_write(self.enable_pin[0], int(self.enable_pin[1]), gpio.LOW)
+        self.dg_write(self.rs_pin, gpio.LOW)
+        self.dg_write(self.enable_pin, gpio.LOW)
 
         if self.rw_pin != 255 : 
-              gpio.digital_write(self.rw_pin[0], int(self.rw_pin[1]), gpio.LOW)
+              self.dg_write(self.rw_pin, gpio.LOW)
 
         # //put the LCD into 4 bit or 8 bit mode
         if not (self._displayfunction & LCD_8BITMODE) :
            self.write4bits(0x03)
            # wait min 4.1ms
            sys.wait(4)
-
            # second try
            self.write4bits(0x03)
            # wait min 4.1ms
            sys.wait(4)
-           
            # third go!
            self.write4bits(0x03) 
            sys.wait(2)
            self.command(LCD_FUNCTIONSET | self._displayfunction)
 
         # finally, set # lines, font size, etc.
-        command(LCD_FUNCTIONSET | self._displayfunction)
+        self.command(LCD_FUNCTIONSET | self._displayfunction)
 
         # turn the display on with no cursor or blinking default
         self._displaycontrol = LCD_DISPLAYON | LCD_CURSOROFF | LCD_BLINKOFF
+
         self.display()
 
         # clear it off
 
         # Initialize to default text direction (for romance languages)
         self._displaymode = LCD_ENTRYLEFT | LCD_ENTRYSHIFTDECREMENT
+
         # set the entry mode
         self.command(LCD_ENTRYMODESET | self._displaymode)
     
     def set_output(self, pin):
         """
         """
-        gpio.pin_mode(pin[0], int(pin[1]), gpio.OUTPUT)
+        self.pn_mode(pin, gpio.OUTPUT)
 
-    def home(self):
+    def clear(self):
         # clear display, set cursor position to zero
         self.command(LCD_CLEARDISPLAY)
         # this command takes a long time!
         for i in range(8) :
             self.write(charmap[i])
 
+    def prnt(self, value):
+        """
+        print chars on lcd ...
+        """
+        for c in value :
+            self.write(ord(c))
+
+    def write(self, value):
+        """
+        """
+        self.send(value, gpio.HIGH);
+
     def command(self, value):
         """
         mid level commands, for sending data/cmds 
     def send(self, value, mode) :
         """
         """
-        gpio.digital_write(self.rs_pin[0], int(self.rs_pin[1]), mode)
+        self.dg_write(self.rs_pin, mode)
     
         # if there is a RW pin indicated, set it low to Write
         if (self.rw_pin != 255) :
-           gpio.digital_write(self.rw_pin[0], int(self.rw_pin[1]), gpio.LOW)
+           self.dg_write(self.rw_pin, gpio.LOW)
       
         if (self._displayfunction & LCD_8BITMODE):
            self.write8bits(value)
     def pulseEnable(self) :
         """
         """
-        gpio.digital_write(self.enable_pin[0], int(self.enable_pin[1]), gpio.LOW)
+        self.dg_write(self.enable_pin, gpio.LOW)
         sys.wait(1)
-        gpio.digital_write(self.enable_pin[0], int(self.enable_pin[1]), gpio.HIGH)
+        self.dg_write(self.enable_pin, gpio.HIGH)
         # enable pulse must be >450ns
         sys.wait(1)
-        gpio.digital_write(self.enable_pin[0], int(self.enable_pin[1]), gpio.LOW)
+        self.dg_write(self.enable_pin, gpio.LOW)
         # commands need > 37us to settle
         sys.wait(1)
     
         """
         """
         for i in range(4) :
-            gpio.pin_mode(self.data_pins[i][0], int(self.data_pins[i][1]), gpio.OUTPUT)
-            gpio.digital_write(self.data_pins[i][0], int(self.data_pins[i][1]), (value >> i) & 0x01)
+            self.pn_mode(self.data_pins[i], gpio.OUTPUT)
+            self.dg_write(self.data_pins[i], (value >> i) & 0x01)
         self.pulseEnable()
     
-    def write4bits(self, value) :
+    def write8bits(self, value) :
         """
         """
         for i in range(8) :
-            gpio.pin_mode(self.data_pins[i][0], int(self.data_pins[i][1]), gpio.OUTPUT)
-            gpio.digital_write(self.data_pins[i][0], int(self.data_pins[i][1]), (value >> i) & 0x01)
+            self.pn_mode(self.data_pins[i], gpio.OUTPUT)
+            self.dg_write(self.data_pins[i], (value >> i) & 0x01)
         self.pulseEnable()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.