From 67ad841ff73892866fba9b5e5a3a13bc71ecebfb Mon Sep 17 00:00:00 2001 From: iabdalkader Date: Mon, 9 Oct 2023 12:49:31 +0200 Subject: [PATCH] scripts/examples: Add Giga H7 examples. --- .../00-Board-Control/adc_read_ext_channel.py | 13 +++ .../00-Board-Control/adc_read_int_channel.py | 11 +++ .../Giga-H7/00-Board-Control/can.py | 37 +++++++ .../00-Board-Control/cpufreq_scaling.py | 32 ++++++ .../Giga-H7/00-Board-Control/i2c_scanner.py | 7 ++ .../Giga-H7/00-Board-Control/led_control.py | 30 ++++++ .../Giga-H7/00-Board-Control/pin_control.py | 13 +++ .../Giga-H7/00-Board-Control/pwm_control.py | 28 ++++++ .../Giga-H7/00-Board-Control/rtc.py | 12 +++ .../Giga-H7/00-Board-Control/spi_control.py | 81 +++++++++++++++ .../Giga-H7/00-Board-Control/timer_control.py | 22 +++++ .../Giga-H7/00-Board-Control/uart_control.py | 13 +++ .../Giga-H7/00-Board-Control/usb_hid.py | 32 ++++++ .../Giga-H7/00-Board-Control/usb_vcp.py | 38 +++++++ .../00-Board-Control/vsync_gpio_output.py | 23 +++++ .../Giga-H7/01-Display/display.py | 62 ++++++++++++ .../Giga-H7/02-Audio/audio_fft.py | 70 +++++++++++++ .../Giga-H7/02-Audio/micro_speech.py | 36 +++++++ .../Giga-H7/03-WiFi/connect.py | 21 ++++ .../10-Arduino-Boards/Giga-H7/03-WiFi/dns.py | 22 +++++ .../Giga-H7/03-WiFi/http_client.py | 42 ++++++++ .../Giga-H7/03-WiFi/http_client_ssl.py | 53 ++++++++++ .../Giga-H7/03-WiFi/mjpeg_streamer.py | 89 +++++++++++++++++ .../Giga-H7/03-WiFi/mqtt_pub.py | 34 +++++++ .../Giga-H7/03-WiFi/mqtt_sub.py | 43 ++++++++ .../10-Arduino-Boards/Giga-H7/03-WiFi/ntp.py | 40 ++++++++ .../10-Arduino-Boards/Giga-H7/03-WiFi/scan.py | 20 ++++ .../Giga-H7/03-WiFi/static_ip.py | 39 ++++++++ .../Giga-H7/04-Bluetooth/ble_blinky.py | 61 ++++++++++++ .../Giga-H7/04-Bluetooth/ble_temperature.py | 98 +++++++++++++++++++ .../04-Bluetooth/temp_sensor_aioble.py | 63 ++++++++++++ .../Giga-H7/05-Low-Power/deep_sleep.py | 27 +++++ .../Giga-H7/05-Low-Power/extint_wakeup.py | 26 +++++ .../Giga-H7/05-Low-Power/stop_mode.py | 19 ++++ 34 files changed, 1257 insertions(+) create mode 100644 scripts/examples/10-Arduino-Boards/Giga-H7/00-Board-Control/adc_read_ext_channel.py create mode 100644 scripts/examples/10-Arduino-Boards/Giga-H7/00-Board-Control/adc_read_int_channel.py create mode 100644 scripts/examples/10-Arduino-Boards/Giga-H7/00-Board-Control/can.py create mode 100644 scripts/examples/10-Arduino-Boards/Giga-H7/00-Board-Control/cpufreq_scaling.py create mode 100644 scripts/examples/10-Arduino-Boards/Giga-H7/00-Board-Control/i2c_scanner.py create mode 100644 scripts/examples/10-Arduino-Boards/Giga-H7/00-Board-Control/led_control.py create mode 100644 scripts/examples/10-Arduino-Boards/Giga-H7/00-Board-Control/pin_control.py create mode 100644 scripts/examples/10-Arduino-Boards/Giga-H7/00-Board-Control/pwm_control.py create mode 100644 scripts/examples/10-Arduino-Boards/Giga-H7/00-Board-Control/rtc.py create mode 100644 scripts/examples/10-Arduino-Boards/Giga-H7/00-Board-Control/spi_control.py create mode 100644 scripts/examples/10-Arduino-Boards/Giga-H7/00-Board-Control/timer_control.py create mode 100644 scripts/examples/10-Arduino-Boards/Giga-H7/00-Board-Control/uart_control.py create mode 100644 scripts/examples/10-Arduino-Boards/Giga-H7/00-Board-Control/usb_hid.py create mode 100644 scripts/examples/10-Arduino-Boards/Giga-H7/00-Board-Control/usb_vcp.py create mode 100644 scripts/examples/10-Arduino-Boards/Giga-H7/00-Board-Control/vsync_gpio_output.py create mode 100644 scripts/examples/10-Arduino-Boards/Giga-H7/01-Display/display.py create mode 100644 scripts/examples/10-Arduino-Boards/Giga-H7/02-Audio/audio_fft.py create mode 100644 scripts/examples/10-Arduino-Boards/Giga-H7/02-Audio/micro_speech.py create mode 100644 scripts/examples/10-Arduino-Boards/Giga-H7/03-WiFi/connect.py create mode 100644 scripts/examples/10-Arduino-Boards/Giga-H7/03-WiFi/dns.py create mode 100644 scripts/examples/10-Arduino-Boards/Giga-H7/03-WiFi/http_client.py create mode 100644 scripts/examples/10-Arduino-Boards/Giga-H7/03-WiFi/http_client_ssl.py create mode 100644 scripts/examples/10-Arduino-Boards/Giga-H7/03-WiFi/mjpeg_streamer.py create mode 100644 scripts/examples/10-Arduino-Boards/Giga-H7/03-WiFi/mqtt_pub.py create mode 100644 scripts/examples/10-Arduino-Boards/Giga-H7/03-WiFi/mqtt_sub.py create mode 100644 scripts/examples/10-Arduino-Boards/Giga-H7/03-WiFi/ntp.py create mode 100644 scripts/examples/10-Arduino-Boards/Giga-H7/03-WiFi/scan.py create mode 100644 scripts/examples/10-Arduino-Boards/Giga-H7/03-WiFi/static_ip.py create mode 100644 scripts/examples/10-Arduino-Boards/Giga-H7/04-Bluetooth/ble_blinky.py create mode 100644 scripts/examples/10-Arduino-Boards/Giga-H7/04-Bluetooth/ble_temperature.py create mode 100644 scripts/examples/10-Arduino-Boards/Giga-H7/04-Bluetooth/temp_sensor_aioble.py create mode 100644 scripts/examples/10-Arduino-Boards/Giga-H7/05-Low-Power/deep_sleep.py create mode 100644 scripts/examples/10-Arduino-Boards/Giga-H7/05-Low-Power/extint_wakeup.py create mode 100644 scripts/examples/10-Arduino-Boards/Giga-H7/05-Low-Power/stop_mode.py diff --git a/scripts/examples/10-Arduino-Boards/Giga-H7/00-Board-Control/adc_read_ext_channel.py b/scripts/examples/10-Arduino-Boards/Giga-H7/00-Board-Control/adc_read_ext_channel.py new file mode 100644 index 000000000..4d49c9472 --- /dev/null +++ b/scripts/examples/10-Arduino-Boards/Giga-H7/00-Board-Control/adc_read_ext_channel.py @@ -0,0 +1,13 @@ +# ADC Read Example. +# +# This example shows how to use the ADC to read an analog pin. + +import time +from pyb import ADC + +adc = ADC("A0") + +while True: + # The ADC has 12-bits of resolution for 4096 values. + print("ADC = %fv" % ((adc.read() * 3.3) / 4095)) + time.sleep_ms(100) diff --git a/scripts/examples/10-Arduino-Boards/Giga-H7/00-Board-Control/adc_read_int_channel.py b/scripts/examples/10-Arduino-Boards/Giga-H7/00-Board-Control/adc_read_int_channel.py new file mode 100644 index 000000000..50fdb4b12 --- /dev/null +++ b/scripts/examples/10-Arduino-Boards/Giga-H7/00-Board-Control/adc_read_int_channel.py @@ -0,0 +1,11 @@ +# ADC Internal Channels Example +# +# This example shows how to read internal ADC channels. + +import pyb + +adc = pyb.ADCAll(12) +print( + "VREF = %.1fv VBAT = %.1fv Temp = %d" + % (adc.read_core_vref(), adc.read_core_vbat(), adc.read_core_temp()) +) diff --git a/scripts/examples/10-Arduino-Boards/Giga-H7/00-Board-Control/can.py b/scripts/examples/10-Arduino-Boards/Giga-H7/00-Board-Control/can.py new file mode 100644 index 000000000..f5ab81a55 --- /dev/null +++ b/scripts/examples/10-Arduino-Boards/Giga-H7/00-Board-Control/can.py @@ -0,0 +1,37 @@ +# CAN Example +# +# This example demonstrates CAN communications between two cameras. +# NOTE: you need two CAN transceiver shields and DB9 cable to run this example. + +import time +import omv +from pyb import CAN + +# NOTE: Set to False on receiving node. +TRANSMITTER = True + +can = CAN(1, CAN.NORMAL, baudrate=125_000, sample_point=75) +# NOTE: uncomment to set bit timing manually, for example: +# can.init(CAN.NORMAL, prescaler=32, sjw=1, bs1=8, bs2=3) +can.restart() + +if TRANSMITTER: + while True: + # Send message with id 1 + can.send("Hello", 1) + time.sleep_ms(1000) + +else: + # Runs on the receiving node. + if omv.board_type() == "H7": # FDCAN + # Set a filter to receive messages with id=1 -> 4 + # Filter index, mode (RANGE, DUAL or MASK), FIFO (0 or 1), params + can.setfilter(0, CAN.RANGE, 0, (1, 4)) + else: + # Set a filter to receive messages with id=1, 2, 3 and 4 + # Filter index, mode (LIST16, etc..), FIFO (0 or 1), params + can.setfilter(0, CAN.LIST16, 0, (1, 2, 3, 4)) + + while True: + # Receive messages on FIFO 0 + print(can.recv(0, timeout=10000)) diff --git a/scripts/examples/10-Arduino-Boards/Giga-H7/00-Board-Control/cpufreq_scaling.py b/scripts/examples/10-Arduino-Boards/Giga-H7/00-Board-Control/cpufreq_scaling.py new file mode 100644 index 000000000..b2a650492 --- /dev/null +++ b/scripts/examples/10-Arduino-Boards/Giga-H7/00-Board-Control/cpufreq_scaling.py @@ -0,0 +1,32 @@ +# CPU frequency scaling example. +# +# This example shows how to use the cpufreq module to change the CPU frequency on the fly. +import sensor +import image +import time +import cpufreq + +sensor.reset() # Reset and initialize the sensor. +sensor.set_pixformat(sensor.GRAYSCALE) # Set pixel format to RGB565 (or GRAYSCALE) +sensor.set_framesize(sensor.QVGA) # Set frame size to QVGA (320x240) +clock = time.clock() # Create a clock object to track the FPS. + + +def test_image_processing(): + for i in range(0, 50): + clock.tick() # Update the FPS clock. + img = sensor.snapshot() # Take a picture and return the image. + img.find_edges(image.EDGE_CANNY, threshold=(50, 80)) + + +print("\nFrequency Scaling Test...") +for f in cpufreq.get_supported_frequencies(): + print("Testing CPU Freq: %dMHz..." % (f)) + cpufreq.set_frequency(f) + clock.reset() + test_image_processing() + freqs = cpufreq.get_current_frequencies() + print( + "CPU Freq:%dMHz HCLK:%dMhz PCLK1:%dMhz PCLK2:%dMhz FPS:%.2f" + % (freqs[0], freqs[1], freqs[2], freqs[3], clock.fps()) + ) diff --git a/scripts/examples/10-Arduino-Boards/Giga-H7/00-Board-Control/i2c_scanner.py b/scripts/examples/10-Arduino-Boards/Giga-H7/00-Board-Control/i2c_scanner.py new file mode 100644 index 000000000..37a8a00ad --- /dev/null +++ b/scripts/examples/10-Arduino-Boards/Giga-H7/00-Board-Control/i2c_scanner.py @@ -0,0 +1,7 @@ +# I2C scanner examples +# +from machine import I2C + +i2c = I2C(1, freq=400_000) +for addr in i2c.scan(): + print("Found device at address %d:0x%x" % (bus, addr)) diff --git a/scripts/examples/10-Arduino-Boards/Giga-H7/00-Board-Control/led_control.py b/scripts/examples/10-Arduino-Boards/Giga-H7/00-Board-Control/led_control.py new file mode 100644 index 000000000..9ed869c7b --- /dev/null +++ b/scripts/examples/10-Arduino-Boards/Giga-H7/00-Board-Control/led_control.py @@ -0,0 +1,30 @@ +# LED Control Example +# +# This example shows how to control the RGB LED. +import time +from machine import LED + +red_led = LED("LED_RED") +green_led = LED("LED_GREEN") +blue_led = LED("LED_BLUE") + + +def led_control(x): + if (x & 1) == 0: + red_led.off() + elif (x & 1) == 1: + red_led.on() + if (x & 2) == 0: + green_led.off() + elif (x & 2) == 2: + green_led.on() + if (x & 4) == 0: + blue_led.off() + elif (x & 4) == 4: + blue_led.on() + + +while True: + for i in range(16): + led_control(i) + time.sleep_ms(500) diff --git a/scripts/examples/10-Arduino-Boards/Giga-H7/00-Board-Control/pin_control.py b/scripts/examples/10-Arduino-Boards/Giga-H7/00-Board-Control/pin_control.py new file mode 100644 index 000000000..67ff07a54 --- /dev/null +++ b/scripts/examples/10-Arduino-Boards/Giga-H7/00-Board-Control/pin_control.py @@ -0,0 +1,13 @@ +# Pin Control Example +# +# This example shows how to use the I/O pins in GPIO mode. + +from machine import Pin + +# Connect a switch to pin 0 that will pull it low when the switch is closed. +# Pin 1 will then light up. +pin0 = Pin("D0", Pin.IN, Pin.PULL_UP) +pin1 = Pin("D1", Pin.OUT, Pin.PULL_NONE) + +while True: + pin1.value(not pin0.value()) diff --git a/scripts/examples/10-Arduino-Boards/Giga-H7/00-Board-Control/pwm_control.py b/scripts/examples/10-Arduino-Boards/Giga-H7/00-Board-Control/pwm_control.py new file mode 100644 index 000000000..baa8b8926 --- /dev/null +++ b/scripts/examples/10-Arduino-Boards/Giga-H7/00-Board-Control/pwm_control.py @@ -0,0 +1,28 @@ +# PWM Control Example +# +# This example shows how to use PWM. + +import time +from pyb import Pin, Timer + + +class PWM: + def __init__(self, pin, tim, ch): + self.pin = pin + self.tim = tim + self.ch = ch + + +pwms = { + "PWM1": PWM("D7", 3, 1), + "PWM2": PWM("D8", 4, 3), + "PWM3": PWM("D9", 4, 4), +} + +# Generate a 1KHz square wave with 50% cycle on the following PWM. +for k, pwm in pwms.items(): + tim = Timer(pwm.tim, freq=1000) # Frequency in Hz + ch = tim.channel(pwm.ch, Timer.PWM, pin=Pin(pwm.pin), pulse_width_percent=50) + +while True: + time.sleep_ms(1000) diff --git a/scripts/examples/10-Arduino-Boards/Giga-H7/00-Board-Control/rtc.py b/scripts/examples/10-Arduino-Boards/Giga-H7/00-Board-Control/rtc.py new file mode 100644 index 000000000..79bc2eba3 --- /dev/null +++ b/scripts/examples/10-Arduino-Boards/Giga-H7/00-Board-Control/rtc.py @@ -0,0 +1,12 @@ +# RTC Example +# +# This example shows how to use the RTC. +import time +from machine import RTC + +rtc = RTC() +rtc.datetime((2013, 7, 9, 2, 0, 0, 0, 0)) + +while True: + print(rtc.datetime()) + time.sleep_ms(1000) diff --git a/scripts/examples/10-Arduino-Boards/Giga-H7/00-Board-Control/spi_control.py b/scripts/examples/10-Arduino-Boards/Giga-H7/00-Board-Control/spi_control.py new file mode 100644 index 000000000..3f543e1ac --- /dev/null +++ b/scripts/examples/10-Arduino-Boards/Giga-H7/00-Board-Control/spi_control.py @@ -0,0 +1,81 @@ +# SPI Control +# +# This example shows how to use the SPI bus to control the +# 1.8" TFT LCD display (JD-T18003-T01) with ST7735R driver. + +import sensor +import time +from machine import Pin +from machine import SPI + +cs = Pin("D2", Pin.OPEN_DRAIN) +rst = Pin("D3", Pin.OUT) +rs = Pin("D4", Pin.OUT) + +# NOTE: The SPI clock frequency will not always be the requested frequency. The hardware only supports +# frequencies that are the bus frequency divided by a prescaler (which can be 2, 4, 8, 16, 32, 64, 128 or 256). +spi = SPI(5, baudrate=int(1000000000 / 66), polarity=0, phase=0) + + +def write_command_byte(c): + cs.low() + rs.low() + spi.send(c) + cs.high() + + +def write_data_byte(c): + cs.low() + rs.high() + spi.send(c) + cs.high() + + +def write_command(c, *data): + write_command_byte(c) + if data: + for d in data: + write_data_byte(d) + + +def write_image(img): + cs.low() + rs.high() + spi.send(img) + cs.high() + + +# Reset the LCD. +rst.low() +time.sleep_ms(100) +rst.high() +time.sleep_ms(100) + +write_command(0x11) # Sleep Exit +time.sleep_ms(120) + +# Memory Data Access Control +# Write 0xC8 for BGR mode. +write_command(0x36, 0xC0) + +# Interface Pixel Format +write_command(0x3A, 0x05) + +# Display On +write_command(0x29) + +sensor.reset() # Initialize the camera sensor. +sensor.set_pixformat(sensor.RGB565) +sensor.set_framesize(sensor.QQVGA2) +sensor.skip_frames(time=2000) # Let new settings take affect. +clock = time.clock() # Tracks FPS. + +while True: + clock.tick() # Track elapsed milliseconds between snapshots(). + img = sensor.snapshot() # Take a picture and return the image. + + write_command(0x2C) # Write image command... + write_image(img) + + print(clock.fps()) # Note: Your OpenMV Cam runs about half as fast while + # connected to your computer. The FPS should increase once disconnected. diff --git a/scripts/examples/10-Arduino-Boards/Giga-H7/00-Board-Control/timer_control.py b/scripts/examples/10-Arduino-Boards/Giga-H7/00-Board-Control/timer_control.py new file mode 100644 index 000000000..5954c2f0f --- /dev/null +++ b/scripts/examples/10-Arduino-Boards/Giga-H7/00-Board-Control/timer_control.py @@ -0,0 +1,22 @@ +# Timer Control Example +# +# This example shows how to use a timer for callbacks. + +import time +from pyb import LED +from pyb import Timer + +blue_led = LED(3) + + +# we will receive the timer object when being called +# Note: functions that allocate memory are Not allowed in callbacks +def tick(timer): + blue_led.toggle() + + +tim = Timer(2, freq=1) # create a timer object using timer 2 - trigger at 1Hz +tim.callback(tick) # set the callback to our tick function + +while True: + time.sleep_ms(1000) diff --git a/scripts/examples/10-Arduino-Boards/Giga-H7/00-Board-Control/uart_control.py b/scripts/examples/10-Arduino-Boards/Giga-H7/00-Board-Control/uart_control.py new file mode 100644 index 000000000..64162349d --- /dev/null +++ b/scripts/examples/10-Arduino-Boards/Giga-H7/00-Board-Control/uart_control.py @@ -0,0 +1,13 @@ +# UART Control +# +# This example shows how to use the serial port on your OpenMV Cam. + +import time +from machine import UART + +# Init UART object. +uart = UART(4, 19200) + +while True: + uart.write("Hello World!\r") + time.sleep_ms(1000) diff --git a/scripts/examples/10-Arduino-Boards/Giga-H7/00-Board-Control/usb_hid.py b/scripts/examples/10-Arduino-Boards/Giga-H7/00-Board-Control/usb_hid.py new file mode 100644 index 000000000..f1b77aba5 --- /dev/null +++ b/scripts/examples/10-Arduino-Boards/Giga-H7/00-Board-Control/usb_hid.py @@ -0,0 +1,32 @@ +# Making OpenMV Camera act as a Mouse using HID. +# +# First we need to create boot.py file to change the default USB mode (VCP+MSC). +# Note: It is recommended to save this file to uSD card not the flash storage. +# This will make it easier to restore the default OpenMV (VCP+MSC) USB mode later +# by just deleting boot.py from uSD using the PC. +# +# Add the following script to boot.py: +# +# import pyb +# pyb.usb_mode('VCP+HID') # serial device + mouse (UNCOMMENT THIS LINE!) +# pyb.usb_mode('VCP+MSC') # serial device + storage device (default) +# pyb.usb_mode('VCP+HID', hid=pyb.hid_keyboard) # serial device + keyboard +# +# Copy boot.py to the root of the uSD card and restart the camera, it should now +# act as a serial device and a mouse. +# +# Connect to the camera using the IDE and run this script, you should see the mouse move. +# +# Note: To restore the default VCP+MSC USB mode, either use the PC to remove boot.py +# from the uSD card, or use the following Python line: import os; os.remove('boot.py') + +import pyb +import time + +hid = pyb.USB_HID() + +while True: + # x, y and scroll + # move 10 pixels to the right + hid.send((0, 10, 0, 0)) + time.sleep_ms(500) diff --git a/scripts/examples/10-Arduino-Boards/Giga-H7/00-Board-Control/usb_vcp.py b/scripts/examples/10-Arduino-Boards/Giga-H7/00-Board-Control/usb_vcp.py new file mode 100644 index 000000000..7c1c4fa53 --- /dev/null +++ b/scripts/examples/10-Arduino-Boards/Giga-H7/00-Board-Control/usb_vcp.py @@ -0,0 +1,38 @@ +# USB VCP example. +# This example shows how to use the USB VCP class to send an image to PC on demand. +# +# WARNING: +# This script should NOT be run from the IDE or command line, it should be saved as main.py +# Note the following commented script shows how to receive the image from the host side. +# +# #!/usr/bin/env python2.7 +# import sys, serial, struct +# port = '/dev/ttyACM0' +# sp = serial.Serial(port, baudrate=115200, bytesize=serial.EIGHTBITS, parity=serial.PARITY_NONE, +# xonxoff=False, rtscts=False, stopbits=serial.STOPBITS_ONE, timeout=None, dsrdtr=True) +# sp.setDTR(True) # dsrdtr is ignored on Windows. +# sp.write("snap") +# sp.flush() +# size = struct.unpack('IIIIIIIIIIII", data)[10] - TIMESTAMP +print("Year:%d Month:%d Day:%d Time: %d:%d:%d" % (time.localtime(t)[0:6])) diff --git a/scripts/examples/10-Arduino-Boards/Giga-H7/03-WiFi/scan.py b/scripts/examples/10-Arduino-Boards/Giga-H7/03-WiFi/scan.py new file mode 100644 index 000000000..a566be764 --- /dev/null +++ b/scripts/examples/10-Arduino-Boards/Giga-H7/03-WiFi/scan.py @@ -0,0 +1,20 @@ +# Scan Example +# +# This example shows how to scan for networks with the WiFi shield. + +import time +import network + +wlan = network.WLAN(network.STA_IF) +wlan.active(True) + +print("Scanning...") +while True: + scan_result = wlan.scan() + for ap in scan_result: + print( + "SSID: %s BSSID: %s Channel: %d RSSI: %d Auth: %d" + % (ap[0], ":".join(["%X" % i for i in ap[1]]), ap[2], ap[3], ap[4]) + ) + print() + time.sleep_ms(1000) diff --git a/scripts/examples/10-Arduino-Boards/Giga-H7/03-WiFi/static_ip.py b/scripts/examples/10-Arduino-Boards/Giga-H7/03-WiFi/static_ip.py new file mode 100644 index 000000000..5ae1bb4d5 --- /dev/null +++ b/scripts/examples/10-Arduino-Boards/Giga-H7/03-WiFi/static_ip.py @@ -0,0 +1,39 @@ +# NTP Example using static IP. +# +# This example shows how to get the current time using NTP with the WiFi shield. + +import network +import socket +import struct +import time + +SSID = "" # Network SSID +KEY = "" # Network key + +TIMESTAMP = 2208988800 + 946684800 + +# Init wlan module and connect to network +wlan = network.WLAN(network.STA_IF) +wlan.active(True) + +# ifconfig must be called before connect() +wlan.ifconfig(("192.168.1.200", "255.255.255.0", "192.168.1.1", "192.168.1.1")) +wlan.connect(SSID, KEY) + +while not wlan.isconnected(): + print('Trying to connect to "{:s}"...'.format(SSID)) + time.sleep_ms(1000) + +# Create new socket +client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + +# Get addr info via DNS +addr = socket.getaddrinfo("pool.ntp.org", 123)[0][4] + +# Send query +client.sendto("\x1b" + 47 * "\0", addr) +data, address = client.recvfrom(1024) + +# Print time +t = struct.unpack(">IIIIIIIIIIII", data)[10] - TIMESTAMP +print("Year:%d Month:%d Day:%d Time: %d:%d:%d" % (time.localtime(t)[0:6])) diff --git a/scripts/examples/10-Arduino-Boards/Giga-H7/04-Bluetooth/ble_blinky.py b/scripts/examples/10-Arduino-Boards/Giga-H7/04-Bluetooth/ble_blinky.py new file mode 100644 index 000000000..a47aa8420 --- /dev/null +++ b/scripts/examples/10-Arduino-Boards/Giga-H7/04-Bluetooth/ble_blinky.py @@ -0,0 +1,61 @@ +# Bluetooth Blinky Example +# +# Use nRFConnect app from the App store, connect to the Nano and write 1/0 to control the LED. + +import bluetooth +import time +from ble_advertising import advertising_payload +from machine import LED +from micropython import const + +_IRQ_CENTRAL_CONNECT = const(1) +_IRQ_CENTRAL_DISCONNECT = const(2) +_IRQ_GATTS_WRITE = const(3) + +_FLAG_READ = const(0x0002) +_FLAG_WRITE = const(0x0008) +_FLAG_NOTIFY = const(0x0010) +_FLAG_INDICATE = const(0x0020) + +_SERVICE_UUID = bluetooth.UUID(0x1523) +_LED_CHAR_UUID = (bluetooth.UUID(0x1525), _FLAG_WRITE) +_LED_SERVICE = ( + _SERVICE_UUID, + (_LED_CHAR_UUID,), +) + + +class BLETemperature: + def __init__(self, ble, name="Giga-H7"): + self._ble = ble + self._ble.active(True) + self._ble.irq(self._irq) + ((self._handle,),) = self._ble.gatts_register_services((_LED_SERVICE,)) + self._connections = set() + self._payload = advertising_payload(name=name, services=[_SERVICE_UUID]) + self._advertise() + self.led = LED("LED_BLUE") + + def _irq(self, event, data): + # Track connections so we can send notifications. + if event == _IRQ_CENTRAL_CONNECT: + conn_handle, _, _ = data + self._connections.add(conn_handle) + elif event == _IRQ_CENTRAL_DISCONNECT: + conn_handle, _, _ = data + self._connections.remove(conn_handle) + # Start advertising again to allow a new connection. + self._advertise() + elif event == _IRQ_GATTS_WRITE: + self.led.value(self._ble.gatts_read(data[-1])[0]) + + def _advertise(self, interval_us=500000): + self._ble.gap_advertise(interval_us, adv_data=self._payload) + + +if __name__ == "__main__": + ble = bluetooth.BLE() + temp = BLETemperature(ble) + + while True: + time.sleep_ms(1000) diff --git a/scripts/examples/10-Arduino-Boards/Giga-H7/04-Bluetooth/ble_temperature.py b/scripts/examples/10-Arduino-Boards/Giga-H7/04-Bluetooth/ble_temperature.py new file mode 100644 index 000000000..19b41adc4 --- /dev/null +++ b/scripts/examples/10-Arduino-Boards/Giga-H7/04-Bluetooth/ble_temperature.py @@ -0,0 +1,98 @@ +# This example demonstrates a simple temperature sensor peripheral. +# +# The sensor's local value updates every second, and it will notify +# any connected central every 10 seconds. + +import bluetooth +import random +import struct +import time +from ble_advertising import advertising_payload +from machine import LED +from micropython import const + +_IRQ_CENTRAL_CONNECT = const(1) +_IRQ_CENTRAL_DISCONNECT = const(2) +_IRQ_GATTS_INDICATE_DONE = const(20) + +_FLAG_READ = const(0x0002) +_FLAG_NOTIFY = const(0x0010) +_FLAG_INDICATE = const(0x0020) + +# org.bluetooth.service.environmental_sensing +_ENV_SENSE_UUID = bluetooth.UUID(0x181A) +# org.bluetooth.characteristic.temperature +_TEMP_CHAR = ( + bluetooth.UUID(0x2A6E), + _FLAG_READ | _FLAG_NOTIFY | _FLAG_INDICATE, +) +_ENV_SENSE_SERVICE = ( + _ENV_SENSE_UUID, + (_TEMP_CHAR,), +) + +# org.bluetooth.characteristic.gap.appearance.xml +_ADV_APPEARANCE_GENERIC_THERMOMETER = const(768) + + +class BLETemperature: + def __init__(self, ble, name="Giga-H7"): + self._ble = ble + self._ble.active(True) + self._ble.irq(self._irq) + ((self._handle,),) = self._ble.gatts_register_services((_ENV_SENSE_SERVICE,)) + self._connections = set() + self._payload = advertising_payload( + name=name, + services=[_ENV_SENSE_UUID], + appearance=_ADV_APPEARANCE_GENERIC_THERMOMETER, + ) + self._advertise() + self.led = LED("LED_BLUE") + + def _irq(self, event, data): + # Track connections so we can send notifications. + if event == _IRQ_CENTRAL_CONNECT: + conn_handle, _, _ = data + self._connections.add(conn_handle) + self.led.on() + elif event == _IRQ_CENTRAL_DISCONNECT: + conn_handle, _, _ = data + self._connections.remove(conn_handle) + # Start advertising again to allow a new connection. + self._advertise() + self.led.off() + elif event == _IRQ_GATTS_INDICATE_DONE: + conn_handle, value_handle, status = data + + def set_temperature(self, temp_deg_c, notify=False, indicate=False): + # Data is sint16 in degrees Celsius with a resolution of 0.01 degrees Celsius. + # Write the local value, ready for a central to read. + self._ble.gatts_write(self._handle, struct.pack("