---
# System prepended metadata

title: 登月車車

---

# 登月小車
![截圖 2024-03-04 下午3.19.45](https://hackmd.io/_uploads/SJXNIxmp6.png)
![截圖 2024-03-04 下午3.29.40](https://hackmd.io/_uploads/ry79dg76p.png)
心得：因為第一二節課有請假所以完全不知道要做什麼東，是我朋友教我怎麼用才知道，而且一開始根本找不到開關在哪，是一直呼叫老師幫忙才找到，歐然後我覺得這個電腦用起來好不習慣，我到現在上課這麼多次都還不會關機．
```javascript=
try:
  def printExcept(e):
    lcd.clear()
    log = str(e)
    sys.print_exception(e)
    data_len, i, colume = 30, 0, 0
    while i < len(log):
      lcd.draw_string(50, 100 + (colume * 20), (log[i : i + data_len] + '\n'), 0x00F8, 0x0000)
      i = i + data_len
      colume = colume + 1

  from time import sleep
  from webai_blockly import Lcd
  from time import sleep
  
  try:
    from webai import *
  except:
    pass
  
  view = Lcd()
  
  # RUN USB MODE
  _deviceID = '3fd25c'
  try:
    print(_onServiceEvent)
    from time import sleep
    sleep(1)
    webai.mqtt.sub(topic=_deviceID + '/SERVICE', callback=_onServiceEvent)
  except:
    pass
  
  
  while True:
    view.displayImg(img=(webai.res.loadImg('mleft.jpg')))
    sleep(0.5)
    view.displayImg(img=(webai.res.loadImg('mright.jpg')))
    sleep(0.5)
  
    sleep(0.001)
except Exception as e:
  printExcept(e)
```




![截圖 2024-03-04 下午3.33.36](https://hackmd.io/_uploads/BkWgqg7a6.png)
![截圖 2024-03-04 下午3.36.38](https://hackmd.io/_uploads/SkuEqxX6a.png)
心得：竟然可以用這塊小面板拍照，雖然畫質沒有多好，但拍出來有種復古感的感覺，但是看上面的程式碼覺得好難，到底怎麼寫出來的，好好奇喔希望自己以後也可以寫出來這種酷程式，還好可以套模板嘿嘿不然我應該做不出來．
```javascript=
try:
  def printExcept(e):
    lcd.clear()
    log = str(e)
    sys.print_exception(e)
    data_len, i, colume = 30, 0, 0
    while i < len(log):
      lcd.draw_string(50, 100 + (colume * 20), (log[i : i + data_len] + '\n'), 0x00F8, 0x0000)
      i = i + data_len
      colume = colume + 1

  import _thread
  import webai_blockly
  from webai_blockly import Lcd
  from webai_blockly import Camera
  from time import sleep
  from time import sleep
  
  show = 0
  
  try:
    from webai import *
  except:
    pass
  
  view = Lcd()
  
  def _hex_to_rgb(hex):
    h = hex.lstrip('#')
    return tuple(int(h[i:i+2], 16) for i in (0, 2, 4))
  
  def _rgb_to_hex565(rgb):
    (red, green, blue) = rgb
    hex565 = "0x%0.4X" % ((int(red / 255 * 31) << 11) | (int(green / 255 * 63) << 5) | (int(blue / 255 * 31)))
    board_hex = '0x' + hex565[4:] + hex565[2:4]
    return int(board_hex, 16)
  
  camera = Camera()
  
  def SYSTEM_THREAD_BTN_L_press(gpio, checkTime):
  
    lastStatus = 1
    while True:
      if webai_blockly.SYSTEM_ALLOCATE_LOCK.locked():
        webai_blockly.SYSTEM_ALLOCATE_LOCK.acquire()
        webai_blockly.SYSTEM_ALLOCATE_LOCK.release()
      value = gpio.value();
      if value == 0 and lastStatus != 0:
        lastStatus = 0
        view.drawString(x=40, y=50, text=b"{var}".format(var='儲存中...'), color=_rgb_to_hex565(_hex_to_rgb('#ffffff')), scale=2, x_spacing=20, mono_space=False)
        webai.fs.save('photo.jpg', (camera.snapshot()))
        view.drawString(x=220, y=50, text=b"{var}".format(var='完成'), color=_rgb_to_hex565(_hex_to_rgb('#ffffff')), scale=2, x_spacing=20, mono_space=False)
        sleep(1)
      elif value == 1 and lastStatus != 1:
        lastStatus = 1
  
      time.sleep_ms(checkTime)
  
  def _on_webai_btn_click(name, state):
    if name == 'btnL' and state == 1:
      try:
        _on_webai_L_press()
      except:
        pass
    elif name == 'btnL' and state == 2:
      try:
        _on_webai_L_release()
      except:
        pass
    elif name == 'btnL' and state == 3:
      try:
        _on_webai_L_long()
      except:
        pass
    elif name == 'btnR' and state == 1:
      try:
        _on_webai_R_press()
      except:
        pass
    elif name == 'btnR' and state == 2:
      try:
        _on_webai_R_release()
      except:
        pass
    elif name == 'btnR' and state == 3:
      try:
        _on_webai_R_long()
      except:
        pass
    elif state == 4:
      try:
        _on_webai_LR_press()
      except:
        pass
  
  def _on_webai_L_press():
  
    view.drawString(x=40, y=50, text=b"{var}".format(var='儲存中...'), color=_rgb_to_hex565(_hex_to_rgb('#ffffff')), scale=2, x_spacing=20, mono_space=False)
    webai.fs.save('photo.jpg', (camera.snapshot()))
    view.drawString(x=220, y=50, text=b"{var}".format(var='完成'), color=_rgb_to_hex565(_hex_to_rgb('#ffffff')), scale=2, x_spacing=20, mono_space=False)
    sleep(1)
  
  try:
    webai.addIoListener(_on_webai_btn_click)
  except:
    try:
      webai.addBtnListener(_on_webai_btn_click)
    except:
      pass
  
  def SYSTEM_THREAD_BTN_R_press(gpio, checkTime):
    global show
    lastStatus = 1
    while True:
      if webai_blockly.SYSTEM_ALLOCATE_LOCK.locked():
        webai_blockly.SYSTEM_ALLOCATE_LOCK.acquire()
        webai_blockly.SYSTEM_ALLOCATE_LOCK.release()
      value = gpio.value();
      if value == 0 and lastStatus != 0:
        lastStatus = 0
        show = True
        sleep(0.1)
        view.displayImg(img=(webai.res.loadImg('photo.jpg')))
      elif value == 1 and lastStatus != 1:
        lastStatus = 1
  
      time.sleep_ms(checkTime)
  
  def _on_webai_R_press():
    global show
    show = True
    sleep(0.1)
    view.displayImg(img=(webai.res.loadImg('photo.jpg')))
  
  # RUN USB MODE
  _deviceID = '3fd25c'
  try:
    print(_onServiceEvent)
    from time import sleep
    sleep(1)
    webai.mqtt.sub(topic=_deviceID + '/SERVICE', callback=_onServiceEvent)
  except:
    pass
  
  
  show = False
  
  try:
    print(webai.addIoListener)
  except:
    try:
      print(webai.addBtnListener)
    except:
      _thread.start_new_thread(SYSTEM_THREAD_BTN_L_press, (webai_blockly.SYSTEM_BTN_L, 100))
  
  
  try:
    print(webai.addIoListener)
  except:
    try:
      print(webai.addBtnListener)
    except:
      _thread.start_new_thread(SYSTEM_THREAD_BTN_R_press, (webai_blockly.SYSTEM_BTN_R, 100))
  
  while True:
    if not show:
      view.displayImg(img=(camera.snapshot()))
    if (webai_blockly.SYSTEM_BTN_R.value()) == 1:
      show = False
  
    sleep(0.001)
except Exception as e:
  printExcept(e)
```

![截圖 2024-03-04 下午3.40.57](https://hackmd.io/_uploads/BJjzslXap.png)
![截圖 2024-03-04 下午3.41.29](https://hackmd.io/_uploads/SJn4slXap.png)
心得：好酷我都沒試過用一堆程式碼可以拼成一張圖，但是我應該還寫不出來這種這麼難的程式碼就只能套模板，雖然這個向日葵看起來很兒童版，但已經很厲害了！還沒試過上傳圖片，等把心得寫完來玩玩看

```javascript=
try:
  def printExcept(e):
    lcd.clear()
    log = str(e)
    sys.print_exception(e)
    data_len, i, colume = 30, 0, 0
    while i < len(log):
      lcd.draw_string(50, 100 + (colume * 20), (log[i : i + data_len] + '\n'), 0x00F8, 0x0000)
      i = i + data_len
      colume = colume + 1

  from webai_blockly import Lcd
  
  i = 0
  
  try:
    from webai import *
  except:
    pass
  
  view = Lcd()
  
  def _hex_to_rgb(hex):
    h = hex.lstrip('#')
    return tuple(int(h[i:i+2], 16) for i in (0, 2, 4))
  
  def _rgb_to_hex565(rgb):
    (red, green, blue) = rgb
    hex565 = "0x%0.4X" % ((int(red / 255 * 31) << 11) | (int(green / 255 * 63) << 5) | (int(blue / 255 * 31)))
    board_hex = '0x' + hex565[4:] + hex565[2:4]
    return int(board_hex, 16)
  
  # RUN USB MODE
  _deviceID = '3fd25c'
  try:
    print(_onServiceEvent)
    from time import sleep
    sleep(1)
    webai.mqtt.sub(topic=_deviceID + '/SERVICE', callback=_onServiceEvent)
  except:
    pass
  
  
  view.drawLine(x0=160, y0=103, x1=160, y1=240, color=_rgb_to_hex565(_hex_to_rgb('#cc0000')), thickness=10)
  view.drawEllipse(x=190,y=205,rx=15,ry=37,rotation=50,color=_rgb_to_hex565(_hex_to_rgb('#33ff33')),thickness=1,fill=True)
  view.drawEllipse(x=130,y=190,rx=15,ry=37,rotation=130,color=_rgb_to_hex565(_hex_to_rgb('#33ff33')),thickness=1,fill=True)
  for i in range(0, 360 + 1, 30):
    view.drawEllipse(x=160,y=100,rx=20,ry=80,rotation=(i + 15),color=_rgb_to_hex565(_hex_to_rgb('#ffff00')),thickness=1,fill=True)
  
  view.drawCircle(x=160, y=100, radius=55, color=_rgb_to_hex565(_hex_to_rgb('#660000')), thickness=1, fill=True)
  view.drawCircle(x=160, y=100, radius=52, color=_rgb_to_hex565(_hex_to_rgb('#ff9900')), thickness=1, fill=True)
  view.drawLine(x0=160, y0=60, x1=200, y1=100, color=_rgb_to_hex565(_hex_to_rgb('#660000')), thickness=3)
  view.drawLine(x0=145, y0=65, x1=195, y1=115, color=_rgb_to_hex565(_hex_to_rgb('#660000')), thickness=3)
  view.drawLine(x0=130, y0=70, x1=190, y1=130, color=_rgb_to_hex565(_hex_to_rgb('#660000')), thickness=3)
  view.drawLine(x0=125, y0=85, x1=175, y1=135, color=_rgb_to_hex565(_hex_to_rgb('#660000')), thickness=3)
  view.drawLine(x0=120, y0=100, x1=160, y1=140, color=_rgb_to_hex565(_hex_to_rgb('#660000')), thickness=3)
  view.drawLine(x0=145, y0=135, x1=195, y1=85, color=_rgb_to_hex565(_hex_to_rgb('#660000')), thickness=3)
  view.drawLine(x0=175, y0=65, x1=125, y1=115, color=_rgb_to_hex565(_hex_to_rgb('#660000')), thickness=3)
  view.drawLine(x0=190, y0=70, x1=130, y1=130, color=_rgb_to_hex565(_hex_to_rgb('#660000')), thickness=3)
  view.drawLine(x0=120, y0=100, x1=160, y1=60, color=_rgb_to_hex565(_hex_to_rgb('#660000')), thickness=3)
  view.drawLine(x0=160, y0=140, x1=200, y1=100, color=_rgb_to_hex565(_hex_to_rgb('#660000')), thickness=3)
except Exception as e:
  printExcept(e)
```


![截圖 2024-03-18 下午3.27.06](https://hackmd.io/_uploads/S1RXTDS0a.png)
![截圖 2024-03-18 下午3.28.51](https://hackmd.io/_uploads/rJBHaPB0T.png)

心得：一開始超笨我把車子拿去充電以為接上去就好了不用控制板，結果老師衝過來說齁這充電啦，你沒有控制板要怎麼輸入程式吧拉吧拉，而且把控制板接上之後還忘記把開關打開，明明程式碼都上傳好了然後繼續呼叫老師顆顆，超級好笑
```javascript=
try:
  def printExcept(e):
    lcd.clear()
    log = str(e)
    sys.print_exception(e)
    data_len, i, colume = 30, 0, 0
    while i < len(log):
      lcd.draw_string(50, 100 + (colume * 20), (log[i : i + data_len] + '\n'), 0x00F8, 0x0000)
      i = i + data_len
      colume = colume + 1

  from ujson import loads, dumps
  __on_line = {}
  __on_sheet = {}
  __on_monster = {}
  def _onServiceEvent(topic, msg):
    try:
      data = loads(msg)
      payload = data['payload']
      if data['type'] == 'sheet':
        if data['actionType'] == 'responseWrite':
          __on_sheet['write' + payload['url'] + payload['name']](payload)
        elif data['actionType'] == 'responseFeature':
          __on_sheet[payload['url'] + payload['name']](payload)
        elif data['actionType'] == 'responseData':
          _google_sheet_receive_data()
          __on_sheet[payload['cell']] = payload['value']
      elif data['type'] == 'line':
        if data['actionType'] == 'replyToken' or data['actionType'] == 'channelMsg':
          eval('_on_channel_' + payload['channelID'])(payload)
      elif data['type'] == 'monster':
        if data['actionType'] == 'click':
          eval('on_click_' + payload['monster'] + '()')
      elif data['type'] == 'keyboard':
        eval('_' + data['actionType'] + '_' + payload['keycode'])()
      elif data['type'] == 'value_input':
        _on_value_input_cb(payload['value'])
      elif data['type'] == 'speech_to_text':
        receive_speech_to_text(payload['value'])
    except:
      pass
  
  try:
    from webai import *
  except:
    pass
  
  mcar.init()
  def _CAR_FORWARD():
    mcar.forward()
  def _CAR_BACK():
    mcar.backward()
  def _CAR_LEFT_FORWARD():
    mcar.move(0, 100)
  def _CAR_LEFT_BACK():
    mcar.move(0, -100)
  def _CAR_RIGHT_FORWARD():
    mcar.move(100, 0)
  def _CAR_RIGHT_BACK():
    mcar.move(-100, 0)
  def _CAR_TURN_RIGHT():
    mcar.right(100)
  def _CAR_TURN_LEFT():
    mcar.left(100)
  def _CAR_STOP():
    mcar.stop()
  def _CAR_KEEP():
    pass
  
  def _onkeydown_38():
  
    _CAR_FORWARD()
  
  def _onkeydown_40():
  
    _CAR_BACK()
  
  def _onkeydown_37():
  
    _CAR_TURN_LEFT()
  
  def _onkeydown_39():
  
    _CAR_TURN_RIGHT()
  
  def _onkeydown_32():
  
    _CAR_STOP()
  
  # RUN USB MODE
  _deviceID = '459795'
  try:
    print(_onServiceEvent)
    from time import sleep
    sleep(1)
    webai.mqtt.sub(topic=_deviceID + '/SERVICE', callback=_onServiceEvent)
  except:
    pass
  
  
  mcar.l_ratio = 1
  mcar.r_ratio = 1
except Exception as e:
  printExcept(e)
```

![截圖 2024-03-18 下午3.39.19](https://hackmd.io/_uploads/ByRTJdHRT.png)
![截圖 2024-03-18 下午3.39.31](https://hackmd.io/_uploads/SJbAk_BCa.png)
![截圖 2024-03-18 下午3.40.03](https://hackmd.io/_uploads/BJBkedSAT.png)

心得：這個是後來知道一定要做才補的，而且我把這個控制板想得太智能了，我以為他資料庫裡面就有顏色編碼，直接辨識就好，結果還得要幫他調亮度那些的，小麻煩但我覺得這個是好玩榜第二名，第一名是那個遙控車車，雖然那個遙控車車會瘋狂延遲但還是很好玩．


```javascript=
try:
  def printExcept(e):
    lcd.clear()
    log = str(e)
    sys.print_exception(e)
    data_len, i, colume = 30, 0, 0
    while i < len(log):
      lcd.draw_string(50, 100 + (colume * 20), (log[i : i + data_len] + '\n'), 0x00F8, 0x0000)
      i = i + data_len
      colume = colume + 1

  from time import sleep
  from webai_blockly import Camera
  from webai import ColorObject
  from webai_blockly import Lcd
  
  _E7_95_AB_E9_9D_A2 = 0
  _E9_A1_8F_E8_89_B2_E8_B3_87_E8_A8_8A = 0
  
  try:
    from webai import *
  except:
    pass
  
  camera = Camera()
  
  view = Lcd()
  
  def _hex_to_rgb(hex):
    h = hex.lstrip('#')
    return tuple(int(h[i:i+2], 16) for i in (0, 2, 4))
  
  def _rgb_to_hex565(rgb):
    (red, green, blue) = rgb
    hex565 = "0x%0.4X" % ((int(red / 255 * 31) << 11) | (int(green / 255 * 63) << 5) | (int(blue / 255 * 31)))
    board_hex = '0x' + hex565[4:] + hex565[2:4]
    return int(board_hex, 16)
  
  # RUN USB MODE
  _deviceID = '3ee0be'
  try:
    print(_onServiceEvent)
    from time import sleep
    sleep(1)
    webai.mqtt.sub(topic=_deviceID + '/SERVICE', callback=_onServiceEvent)
  except:
    pass
  
  
  while True:
    _E7_95_AB_E9_9D_A2 = camera.snapshot()
    # 可點擊右鍵->小工具，使用顏色小工具選擇顏色
    _E9_A1_8F_E8_89_B2_E8_B3_87_E8_A8_8A = ColorObject.findMax(_E7_95_AB_E9_9D_A2, (0,62,-18,-3,-48,-17))
    if (_E9_A1_8F_E8_89_B2_E8_B3_87_E8_A8_8A.pixels() if _E9_A1_8F_E8_89_B2_E8_B3_87_E8_A8_8A else 0) > 10:
      view.drawString(x=125, y=110, text=b"{var}".format(var='藍色'), color=_rgb_to_hex565(_hex_to_rgb('#ffffff')), scale=2, x_spacing=20, img=_E7_95_AB_E9_9D_A2, display=False)
    view.displayImg(img=_E7_95_AB_E9_9D_A2)
  
    sleep(0.001)
except Exception as e:
  printExcept(e)
```
![截圖 2024-04-22 下午3.54.32](https://hackmd.io/_uploads/B1LWdcmZ0.png)
![截圖 2024-04-29 下午3.43.09](https://hackmd.io/_uploads/H1T9kRnZ0.png)
![截圖 2024-04-29 下午3.43.26](https://hackmd.io/_uploads/H13iJAnW0.png)

心得：
這個是後來補的，差點以為自己把東西都丟了，回去翻東西才發現還好沒有丟，我其實很不喜歡捍接，因為國中的時候有被機器燙傷過所以這個部分是請朋友幫助我完成的．



