2021年11月29日月曜日

UART(mic-both, mic2uno,pico2mic,mic2pico)

------------------ micro and micro -------------------------------------------------

micro:bitの間はボー9600でないといかんようだ 参考までに

serial.onDataReceived(serial.delimiters(Delimiters.NewLine), function on_data_received() {
    basic.showString(serial.readUntil(serial.delimiters(Delimiters.NewLine)))
    basic.pause(500)
})
serial.redirect(SerialPin.P0SerialPin.P1BaudRate.BaudRate9600)
basic.forever(function on_forever() {
    serial.writeLine("u")
    basic.pause(1000)
})


--------------pico to micro ------------------------------------
重要なのは、ボーレートを合わせておくこと! 
を参考に以下のコードをマイクロにおく

serial.onDataReceived(serial.delimiters(Delimiters.NewLine), function () {
    basic.showString(serial.readUntil(serial.delimiters(Delimiters.NewLine)))
})
serial.redirect(
SerialPin.P0,
SerialPin.P1,
BaudRate.BaudRate115200
)
basic.forever(function () {
    
})
を参考に以下をラズピコにおく
from machine import UART,Pin
u = UART(1) # pin6 for tx,pin7 for rx
u.write('test\n')
注意:マイクロとラズピコはGNDを結線するのがポイント、
マイクロの0はTxなので、ラズピコの7、
マイクロの1はRxなのでラズピコの6
これで、無事にマイクロにtestと表示がでる
------ from microbit to pico -----------------
9600baudrateで安全をとって通信した
serial.onDataReceived(serial.delimiters(Delimiters.NewLine), function () {
    basic.showString(serial.readUntil(serial.delimiters(Delimiters.NewLine)))
    basic.pause(500)
})
serial.redirect(
SerialPin.P0,
SerialPin.P1,
BaudRate.BaudRate9600
)
basic.pause(2000)
basic.forever(function () {
    basic.pause(500)
    serial.writeLine("hello")
    basic.pause(500)
})
MicroPython的午睡(21) ラズパイPico、M5AtomLiteとUART通信 | デバイスビジネス開拓団 (jhalfmoon.com)
を参考にピコも9600ボーとした
from machine import Pin,UART
import time
u = UART(1, 9600, tx=Pin(4), rx=Pin(5))
time.sleep(2)
while True:
ret = u.readline()
if ret is not None:
print(ret)
time.sleep(0.5)

2021年11月28日日曜日

IRQ/MULTICORE (irq :: arduino/espr2/esp32, dualcore :: raspico,esp32)

Arduinoの割り込み機能を使ってみる | 物を作る者 (novicengineering.com) を

参考にしたが、タクトスイッチでは、一方に5Vをいれ、片方に10kプルダウン抵抗を入れて

その間からPin2への入力を取った 

attachInterruptの使い方 | 物を作る者 (novicengineering.com) によると

割り込みではdelayは利かんのも体験した1

-----------------------------------------------------------------------------

Esp32 GPIO 割り込みについて

https://blog.goo.ne.jp/jh7ubc/e/8c97dc4bfad4f93d44301f8341083a69

INPUT_PULLUPについては http://7ujm.net/micro/arduino_int.html および

https://e-words.jp/w/%E3%83%97%E3%83%AB%E3%82%A2%E3%83%83%E3%83%97%E6%8A%B5%E6%8A%97.html を参考に勉強した 

#define LED_Pin 27
#define SW_Pin 33
volatile int state = LOW;
 
void setup() {
  pinMode(SW_Pin, INPUT_PULLUP);
  pinMode(LED_Pin, OUTPUT);
  attachInterrupt(SW_Pin, LED_blink, FALLING);
}

void loop() {
  digitalWrite(LED_Pin, state);
}

void LED_blink(){
  delay(10);
  state = !state;
}

---------------------------------------------------------------------------------------------------

ここよりマルチコア

-------------------------------------------------------------------------------------------------

Multithreaded on Raspberry Pi Pico (MicroPython) - ElectroSoftCloud とても簡単

import machine
import utime
import _thread
# We configure the pin of the internal led as an output and
# we assign to internal_led
internal_led = machine.Pin(25, machine.Pin.OUT)
# Function that will block the thread with a while loop
# which will simply display a message every second
def second_thread():
while True:
print("Hello, I'm here in the second thread writting every second")
utime.sleep(1)
# Function that initializes execution in the second core
# The second argument is a list or dictionary with the arguments
# that will be passed to the function.
_thread.start_new_thread(second_thread, ())
# Second loop that will block the main thread, and what it will do
# that the internal led blinks every half second
while True:
internal_led.toggle()
utime.sleep(0.25) ---------------------------------------------------using semaphore--------------------
import machine
import utime
import _thread
# We configure the pin of the internal led as an output and
# we assign to internal_led
internal_led = machine.Pin(25, machine.Pin.OUT)
# We create a semaphore (A.K.A lock)
baton = _thread.allocate_lock()
# Function that will block the thread with a while loop
# which will simply display a message every second
def second_thread():
while True:
# We acquire the traffic light lock
baton.acquire()
print("Hello, I'm here in the second thread writting every second")
utime.sleep(1)
# We release the traffic light lock
baton.release()
# Function that initializes execution in the second core
# The second argument is a list or dictionary with the arguments
# that will be passed to the function.
_thread.start_new_thread(second_thread, ())
# Second loop that will block the main thread, and what it will do
# that the internal led blinks every half second
while True:
# We acquire the semaphore lock
baton.acquire()
internal_led.toggle()
utime.sleep(0.25)
# We release the semaphore lock
baton.release()

ーーーーーーーー-dual core usingーーーーーーーーーーーーーーーーーーーーーーーーー

https://programresource.net/2020/02/25/2994.html -> esp32 dual core program

https://robo.mydns.jp/Lecture/?%C5%C5%BB%D2%B9%A9%BA%EE/ESP32#va27dec2 

https://www.mgo-tec.com/blog-entry-arduino-esp32-multi-task-dual-core-01.html

いずれも粗い議論だが、入門には便利 以下のサイトはとても有益だった

 https://www.kerislab.jp/posts/2017-06-24-esp32-dual-core/ このままうごいた

#include "esp32-hal-log.h"
#include "freertos/task.h"

void task0 (void* arg) {
  while (1) {
    log_i("This is Core 0");
    delay(1000);
  }
}

void task1 (void* arg) {
  while (1) {
    log_i("This is Core 1");
    delay(1500);
  }
}

void setup () {
  log_i("Hello, this is ESP32:)");
  xTaskCreatePinnedToCore(task0, "Task0", 4096, NULL, 1, NULL, 0);
  xTaskCreatePinnedToCore(task1, "Task1", 4096, NULL, 1, NULL, 1);
}

void loop () {
  delay(1500);