2018年11月22日

bpi-bit mycropython記事


安裝picocom

sudo apt-get install picocom
sudo pip install adafruit-ampy --upgrade

確認 esptool安裝完成

sudo apt-get install esptool
sudo pip3 install esptool

燒錄硬體

下載網址:https://github.com/BPI-STEAM/BPI-BIT-MicroPython/releases
 下載:firmware.bin、AutoErase.py、AutoFlash.py三個檔案。
開啟終端機,輸入:
python3 AutoErase.py
python3 AutoFlash.py

連線到bpi-bit

picocom /dev/ttyUSB0 -b 115200

關閉wifi AP

wifi.close()

執行.py檔案

ampy --port /dev/ttyUSB0 run led.py

Ctrl+A在空行上,輸入原始REPL模式
Ctrl+B在空行上,進入正常的REPL模式
Ctrl+C中斷程序
Ctrl+D軟體重啟
Ctrl+E進入複製貼上程式碼模式,做完貼上使用Ctrl+D執行貼上程式碿
按Ctrl+A 再按Ctrl+Q離開終端機
如果要燒錄進去,則把xx.py程式,改成main.py
sudo ampy --port /dev/ttyUSB0 put main.py
刪除板子上的文件
ampy --port /dev/ttyUSB0 rm led.py
下載檔案到電腦中
ampy -p /dev/ttyUSB0 get xx.py
列出文件
ampy -p /dev/ttyUSB0 ls
清除micropyton重新燒入 
sudo esptool.py --port /dev/ttyUSB0 erase_flash
sudo esptool.py --port /dev/ttyUSB0 --baud 460800 write_flash --flash_size=detect 0 esp8266-20180511-v1.9.4.bin

點亮GPIO 18 LED燈

載入machine裡的Pin類別
from machine import Pin
載入時間物件
import time
設定GPIO 18為輸出,GPIO18在bpi-bit正面wf32晶片旁bpi:bit的正方方LED燈
LED = Pin(18, Pin.OUT)
亮燈
LED.value(1)
熄燈
LED.value(0)
 

呼吸燈作法

import machine,time
LED = machine.PWM(machine.Pin(18), freq=1000)
tick = 0 # counter variable
while True:
    if tick < 40:
        # self.tick % 20 gives a number 0 to 19
        # subtracting 9 makes it -9 to 10
        # abs maps it 9 to 0 to 10
        # subtracting from 10 maps it 1 to 10 to 0
        # multiplying by 25 scales it 25 to 250 to 0
        LED.duty((10 - (abs((tick % 20) - 9))) * 25)
        tick = (tick + 1) % 40
        time.sleep(0.1)
注意程式碼階層,python以階層決定程式碼位置。
可以貼在文件編輯器中存成led_pwm.py檔,放終下列執行指令
 ampy --port /dev/ttyUSB0 run led_pwm.py

2018年11月19日

備份mbr

備份
dd if=/dev/sda of=back.mbr bs=512 count=1

sfdisk -d /dev/sda > back.mbr

還原
dd if=/tmp/mbrsda.bak of=/dev/sdb bs=446 count=1
 
sfdisk /dev/sda < /tmp/sda.mbr

 

2018年11月10日

bpi-bit buzzer music

參考midi網址:https://en.m.wikipedia.org/wiki/Scientific_pitch_notation#Table_of_note_frequencies


 #define Buzzer 25   //GPIO 25

// 0 NULL 0.0001
// 5    LG 196.00
// 5.5  #LG 207.65
// 6    LA 220.00
// 6.5  #LA 233.08
// 7    LB 246.94
// 11    C 261.63
// 11.5 #C 277.18
// 12    D 293.66
// 12.5 #D 311.13
// 13    E 329.63
// 14    F 349.23
// 14.5 #F 369.99
// 15    G 392.00
// 15.5  #G 415.30
// 21    A 440.00
// 21.5  #A 466.16
// 22    B 493.88

#define TIME 250       // 1拍時間
#define LEDC_CHANNEL 0    
#define LEDC_TIMER_BIT 13
#define LEDC_BASE_FREQ 5000
double music_star[] = {11,11,15,15,16,16,15,0,14,14,13,13,12,12,11,0,15,15,14,14,13,13,12,0,15,15,14,14,13,13,12,0,11,11,15,15,16,16,15,0,14,14,13,13,12,12,11,0 };
double music_happy[] = {5,5,6,5,11,7,0,5,5,6,5,12,11,0,5,5,15,13,11,7,6,0,0,0,14,13,12,11,0 };
double music_ode[] = {7,7,11,12,12,11,7,6,5,5,6,7,0,6,6,0,7,7,11,12,12,11,7,6,5,5,6,7,6,0,5,5,0};

void setup(){
  ledcSetup(LEDC_CHANNEL, LEDC_BASE_FREQ, LEDC_TIMER_BIT);
  ledcAttachPin(Buzzer, LEDC_CHANNEL);
}

double to_num(double m_n){
double r_n = 0.00;
if (m_n ==5){
  r_n = 196.00;
}else if( m_n ==5.5){
  r_n = 207.65;
}else if ( m_n == 6){
  r_n = 220.00;
}else if ( m_n == 6.5){
  r_n = 233.08;
}else if ( m_n == 7){
  r_n = 246.94;
}else if ( m_n == 11){
  r_n = 261.63;
}else if ( m_n == 11.5){
  r_n = 277.18;
}else if ( m_n == 12){
  r_n = 293.66;
}else if( m_n == 12.5){
  r_n = 311.13;
}else if( m_n == 13){
  r_n = 329.63;
}else if( m_n == 14){
  r_n = 349.23;
}else if( m_n == 14.5){
  r_n = 369.99;
}else if( m_n == 15){
  r_n = 392.00;
}else if( m_n ==15.5){
  r_n = 415.30;
}else if( m_n == 16){
  r_n = 440.00;
}else if( m_n ==16.5){
  r_n = 466.16;
}else if( m_n == 17){
  r_n =493.88;
}else if( m_n ==21){
  r_n =523.25;
}
   return r_n ;
}


void loop(){
  for (int i=0;i<sizeof(music_happy)/8;i++)
  {
  
    if (to_num(music_happy[i]) == 0){
          delay(TIME);
      }else{
        ledcWriteTone(LEDC_CHANNEL, to_num(music_happy[i]));
        delay(TIME);
        }
    }
  
  ledcWriteTone(LEDC_CHANNEL, 0);
  delay(1000);
}

2018年11月1日

micropython 連bpi-bit

apt-get install picocom
 pip3 install esptool

github
 https://github.com/BPI-STEAM/BPI-BIT-MicroPython


燒入micropython韌體
檔案下載
https://github.com/BPI-STEAM/BPI-BIT-MicroPython/releases/tag/beta 

python3  AutoErase.py
python3 AutoFlash.py

連上bpi-bit
picocom /dev/ttyUSB0 -b 115200

關掉wifi指令
wifi.close();