GuyCarver / MicroPython

All of my MicroPython stuff
136 stars 62 forks source link

How to build and install on esp32c3 #4

Open weiyshay opened 10 months ago

weiyshay commented 10 months ago

Hi, Thank you very much for the repo!

I am new to micropython and trying to play with a st7735 display on esp32c3 and set up micropython with mpremtoe enabled, next I am trying to run some display test but failed with some errors.

Can I import the libs via mpremote? How can I do it?

My test:

Basic:
esp32-dev-micropython#cat hello.py 
print("hello")
esp32-dev-micropython#mpremote run  ./hello.py 
hello

---
dispaly:
esp32-dev-micropython#mpremote run  ./hello.py 
hello
esp32-dev-micropython#cat st7735-test.py 
from machine import Pin, SoftSPI, SPI
from ST7735 import TFT
import time

# 由于ftf屏的颜色有问题,因此需要重写一个函数修复一下
def TFTColor(r,g,b) :
    return ((b & 0xF8) << 8) | ((g & 0xFC) << 3) | (r >> 3)

spi = SoftSPI(baudrate=1000000, polarity=1, phase=0, sck=Pin(2), mosi=Pin(3), miso=Pin(10))
tft=TFT(spi,6,10,7) #DC, Reset, CS
tft.initr()
tft.rgb(True)
tft.rotation(1) #方向调整

# 绘制背景色
tft.fill(TFTColor(0,0,0))

w = 20
h = 20
max = 160
for i in range(0,max*4-1):
    x = i * 5 % max
    y = i * 5 // max * h + 24
    tft.fillrect((x,y),(w,h),TFTColor(255,255,255))
    ++i
    time.sleep(0.04)
    tft.fillrect((x,y),(w,h),TFTColor(0,0,0))
esp32-dev-micropython#mpremote run  ./st7735-test.py 
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
ImportError: no module named 'ST7735'

---
st7735:
esp32-dev-micropython#mpremote run  ST7735.py 
Traceback (most recent call last):
  File "<stdin>", line 10, in <module>
SyntaxError: invalid micropython decorator

esp32-dev-micropython#head -15 ST7735.py 
#Driver for Sainsmart 1.8" tft display ST7735
#Translated by Guy Carver from the ST7735 sample code.
#Display uses SPI interface.

#todo: Use const()

import pyb
from math import sqrt

@micropython.native
def clamp( aValue, aMin, aMax ) :
  return max(aMin, min(aMax, aValue))

@micropython.native
def TFTColor( aR, aG, aB ) :

---
led:
esp32-dev-micropython#cat led.py 
#LED testing.

from pyb import *

dC = 5

def test(  ):
  l = LED(4)
  for i in range(255):
    l.intensity(i)
    delay(dC)
  for i in range(255, 0, -1):
    l.intensity(i)
    delay(dC)
  l.off()
esp32-dev-micropython#mpremote run  led.py 
Traceback (most recent call last):
  File "<stdin>", line 3, in <module>
ImportError: no module named 'pyb'
weiyshay commented 10 months ago

Asked in discord and got some feedback! Working on #2 to resolve the decorator issue.

https://discord.com/channels/574275045187125269/1148070474262249543/1148076745388474388

Hello!

     no module pyb

The pyb module refers to the pyBoard, the very own MicroPython project hardware developped to showcasea many of MicroPython's aspects You would have to replace pyb by esp32 in the code examples you encounter, and sometimes adapt things a bit too

     invalid micropython decorator

I think the @micropython.native decorator refers some method to speed-up a few functions by turning them into binary machine code rather than python bytecode (.mpy). This might be an optional feature, and maybe it is not enabled in the mpconfigport.h used to compile MicroPython, or maybe you just need import micropython.

     no module ST7735

Is it something you are working on? I think you can push it through via mpremote yes

weiyshay commented 10 months ago

I comment out all the native decorators in the file and gave it a run and system crashed.

Diff:

--- s   2023-09-05 04:37:51.199898388 +0800
+++ ../../../GuyCarver/MicroPython/lib/ST7735.py    2023-09-03 23:48:04.368648368 +0800
@@ -1,15 +1,17 @@
-import machine as pyb
-from machine import Pin, SoftSPI, SPI
-import time
-import esp32
-import micropython
+#Driver for Sainsmart 1.8" tft display ST7735
+#Translated by Guy Carver from the ST7735 sample code.
+#Display uses SPI interface.
+
+#todo: Use const()
+
+import pyb
 from math import sqrt

-#@micropython.native
+@micropython.native
 def clamp( aValue, aMin, aMax ) :
   return max(aMin, min(aMax, aValue))

-#@micropython.native
+@micropython.native
 def TFTColor( aR, aG, aB ) :
   '''Create a 16 bit rgb value from the given R,G,B from 0-255.
      This assumes rgb 565 layout and will be incorrect for bgr.'''
@@ -100,17 +102,17 @@ class tft(object) :
     '''Create a 565 rgb TFTColor value'''
     return TFTColor(aR, aG, aB)

-  def __init__( self, aLoc, aDC, aReset, aCS ) :
+  def __init__( self, aLoc, aDC, aReset ) :
     '''aLoc SPI pin location is either 1 for 'X' or 2 for 'Y'.
        aDC is the DC pin and aReset is the reset pin.'''
     self._size = tft._SCREENSIZE
     self.rotate = 0                    #Vertical with top toward pins.
     self._rgb = True                   #color order of rgb.
-    self.dc  = pyb.Pin(aDC, pyb.Pin.OUT, pyb.Pin.PULL_DOWN)
-    self.reset = pyb.Pin(aReset, pyb.Pin.OUT, pyb.Pin.PULL_DOWN)
+    self.dc  = pyb.Pin(aDC, pyb.Pin.OUT_PP, pyb.Pin.PULL_DOWN)
+    self.reset = pyb.Pin(aReset, pyb.Pin.OUT_PP, pyb.Pin.PULL_DOWN)
     rate = 200000 #100000000 #Set way high but will be clamped to a maximum in SPI constructor.
     cs = "X5" if aLoc == 1 else "Y5"
-    self.cs = pyb.Pin(cs, pyb.Pin.OUT, pyb.Pin.PULL_DOWN)
+    self.cs = pyb.Pin(cs, pyb.Pin.OUT_PP, pyb.Pin.PULL_DOWN)
     self.cs.high()
     self.spi = pyb.SPI(aLoc, pyb.SPI.MASTER, baudrate = rate, polarity = 1, phase = 0, crc=None)
     self.colorData = bytearray(2)
@@ -144,7 +146,7 @@ class tft(object) :
         self._size =(self._size[1], self._size[0])
       self._setMADCTL()

-  #@micropython.native
+  @micropython.native
   def pixel( self, aPos, aColor ) :
     '''Draw a pixel at the given position'''
     if 0 <= aPos[0] < self._size[0] and 0 <= aPos[1] < self._size[1]:
@@ -353,7 +355,7 @@ class tft(object) :
       self.vline((aPos[0] + x, y0), ln, aColor)
       self.vline((aPos[0] - x, y0), ln, aColor)

-  def fill( self, aColor = _BLACK ) :
+  def fill( self, aColor = BLACK ) :
     '''Fill screen with the given color.'''
     self.fillrect((0, 0), self._size, aColor)

@@ -401,7 +403,7 @@ class tft(object) :

     self._writecommand(_RAMWR)            #Write to RAM.

-  #@micropython.native
+  @micropython.native
   def _writecommand( self, aCommand ) :
     '''Write given command to the device.'''
     self.dc.low()
@@ -409,7 +411,7 @@ class tft(object) :
     self.spi.send(aCommand)
     self.cs.high()

-  #@micropython.native
+  @micropython.native
   def _writedata( self, aData ) :
     '''Write given data to the device.  This may be
        either a single int or a bytearray of values.'''
@@ -418,21 +420,21 @@ class tft(object) :
     self.spi.send(aData)
     self.cs.high()

-  #@micropython.native
+  @micropython.native
   def _pushcolor( self, aColor ) :
     '''Push given color to the device.'''
     self.colorData[0] = aColor >> 8
     self.colorData[1] = aColor
     self._writedata(self.colorData)

-  #@micropython.native
+  @micropython.native
   def _setMADCTL( self ) :
     '''Set screen rotation and RGB/BGR format.'''
     self._writecommand(_MADCTL)
     rgb = _TFTRGB if self._rgb else _TFTBGR
     self._writedata(tft._TFTRotations[self.rotate] | rgb)

-  #@micropython.native
+  @micropython.native
   def _reset( self ) :
     '''Reset the device.'''
     self.dc.low()
@@ -637,7 +639,7 @@ class tft(object) :
 #
 #    self.cs.high()

-  #@micropython.native
+  @micropython.native
   def initg( self ) :
     '''Initialize a green tab version.'''
     self._reset()
@@ -746,27 +748,3 @@ class tft(object) :
 #  t.initg()
 ##  t.fill(0)
 #  return t
-
-# 由于ftf屏的颜色有问题,因此需要重写一个函数修复一下
-def TFTColor(r,g,b) :
-    return ((b & 0xF8) << 8) | ((g & 0xFC) << 3) | (r >> 3)
-
-spi = SoftSPI(baudrate=1000000, polarity=1, phase=0, sck=Pin(2), mosi=Pin(3), miso=Pin(10))
-tft=tft(spi, 11, 7, 7) #DC, Reset, CS
-tft.initr()
-tft.rgb(True)
-tft.rotation(1) #方向调整
-
-# 绘制背景色
-tft.fill(TFTColor(0,0,0))
-
-w = 20
-h = 20
-max = 160
-for i in range(0,max*4-1):
-    x = i * 5 % max
-    y = i * 5 // max * h + 24
-    tft.fillrect((x,y),(w,h),TFTColor(255,255,255))
-    ++i
-    time.sleep(0.04)
-    tft.fillrect((x,y),(w,h),TFTColor(0,0,0))

My diff:

#diff s ../../../GuyCarver/MicroPython/lib/ST7735.py  -up
--- s   2023-09-05 04:37:51.199898388 +0800
+++ ../../../GuyCarver/MicroPython/lib/ST7735.py    2023-09-03 23:48:04.368648368 +0800
@@ -1,15 +1,17 @@
-import machine as pyb
-from machine import Pin, SoftSPI, SPI
-import time
-import esp32
-import micropython
+#Driver for Sainsmart 1.8" tft display ST7735
+#Translated by Guy Carver from the ST7735 sample code.
+#Display uses SPI interface.
+
+#todo: Use const()
+
+import pyb
 from math import sqrt

-#@micropython.native
+@micropython.native
 def clamp( aValue, aMin, aMax ) :
   return max(aMin, min(aMax, aValue))

-#@micropython.native
+@micropython.native
 def TFTColor( aR, aG, aB ) :
   '''Create a 16 bit rgb value from the given R,G,B from 0-255.
      This assumes rgb 565 layout and will be incorrect for bgr.'''
@@ -100,17 +102,17 @@ class tft(object) :
     '''Create a 565 rgb TFTColor value'''
     return TFTColor(aR, aG, aB)

-  def __init__( self, aLoc, aDC, aReset, aCS ) :
+  def __init__( self, aLoc, aDC, aReset ) :
     '''aLoc SPI pin location is either 1 for 'X' or 2 for 'Y'.
        aDC is the DC pin and aReset is the reset pin.'''
     self._size = tft._SCREENSIZE
     self.rotate = 0                    #Vertical with top toward pins.
     self._rgb = True                   #color order of rgb.
-    self.dc  = pyb.Pin(aDC, pyb.Pin.OUT, pyb.Pin.PULL_DOWN)
-    self.reset = pyb.Pin(aReset, pyb.Pin.OUT, pyb.Pin.PULL_DOWN)
+    self.dc  = pyb.Pin(aDC, pyb.Pin.OUT_PP, pyb.Pin.PULL_DOWN)
+    self.reset = pyb.Pin(aReset, pyb.Pin.OUT_PP, pyb.Pin.PULL_DOWN)
     rate = 200000 #100000000 #Set way high but will be clamped to a maximum in SPI constructor.
     cs = "X5" if aLoc == 1 else "Y5"
-    self.cs = pyb.Pin(cs, pyb.Pin.OUT, pyb.Pin.PULL_DOWN)
+    self.cs = pyb.Pin(cs, pyb.Pin.OUT_PP, pyb.Pin.PULL_DOWN)
     self.cs.high()
     self.spi = pyb.SPI(aLoc, pyb.SPI.MASTER, baudrate = rate, polarity = 1, phase = 0, crc=None)
     self.colorData = bytearray(2)
@@ -144,7 +146,7 @@ class tft(object) :
         self._size =(self._size[1], self._size[0])
       self._setMADCTL()

-  #@micropython.native
+  @micropython.native
   def pixel( self, aPos, aColor ) :
     '''Draw a pixel at the given position'''
     if 0 <= aPos[0] < self._size[0] and 0 <= aPos[1] < self._size[1]:
@@ -353,7 +355,7 @@ class tft(object) :
       self.vline((aPos[0] + x, y0), ln, aColor)
       self.vline((aPos[0] - x, y0), ln, aColor)

-  def fill( self, aColor = _BLACK ) :
+  def fill( self, aColor = BLACK ) :
     '''Fill screen with the given color.'''
     self.fillrect((0, 0), self._size, aColor)

@@ -401,7 +403,7 @@ class tft(object) :

     self._writecommand(_RAMWR)            #Write to RAM.

-  #@micropython.native
+  @micropython.native
   def _writecommand( self, aCommand ) :
     '''Write given command to the device.'''
     self.dc.low()
@@ -409,7 +411,7 @@ class tft(object) :
     self.spi.send(aCommand)
     self.cs.high()

-  #@micropython.native
+  @micropython.native
   def _writedata( self, aData ) :
     '''Write given data to the device.  This may be
        either a single int or a bytearray of values.'''
@@ -418,21 +420,21 @@ class tft(object) :
     self.spi.send(aData)
     self.cs.high()

-  #@micropython.native
+  @micropython.native
   def _pushcolor( self, aColor ) :
     '''Push given color to the device.'''
     self.colorData[0] = aColor >> 8
     self.colorData[1] = aColor
     self._writedata(self.colorData)

-  #@micropython.native
+  @micropython.native
   def _setMADCTL( self ) :
     '''Set screen rotation and RGB/BGR format.'''
     self._writecommand(_MADCTL)
     rgb = _TFTRGB if self._rgb else _TFTBGR
     self._writedata(tft._TFTRotations[self.rotate] | rgb)

-  #@micropython.native
+  @micropython.native
   def _reset( self ) :
     '''Reset the device.'''
     self.dc.low()
@@ -637,7 +639,7 @@ class tft(object) :
 #
 #    self.cs.high()

-  #@micropython.native
+  @micropython.native
   def initg( self ) :
     '''Initialize a green tab version.'''
     self._reset()
@@ -746,27 +748,3 @@ class tft(object) :
 #  t.initg()
 ##  t.fill(0)
 #  return t
-
-# 由于ftf屏的颜色有问题,因此需要重写一个函数修复一下
-def TFTColor(r,g,b) :
-    return ((b & 0xF8) << 8) | ((g & 0xFC) << 3) | (r >> 3)
-
-spi = SoftSPI(baudrate=1000000, polarity=1, phase=0, sck=Pin(2), mosi=Pin(3), miso=Pin(10))
-tft=tft(spi, 11, 7, 7) #DC, Reset, CS
-tft.initr()
-tft.rgb(True)
-tft.rotation(1) #方向调整
-
-# 绘制背景色
-tft.fill(TFTColor(0,0,0))
-
-w = 20
-h = 20
-max = 160
-for i in range(0,max*4-1):
-    x = i * 5 % max
-    y = i * 5 // max * h + 24
-    tft.fillrect((x,y),(w,h),TFTColor(255,255,255))
-    ++i
-    time.sleep(0.04)
-    tft.fillrect((x,y),(w,h),TFTColor(0,0,0))