Skip to content

Instantly share code, notes, and snippets.

@cwyark
cwyark / sdcard.py
Last active November 4, 2017 04:06
MicroPython SPI sdcard driver in my gist.
from micropython import const
import time
_CMD_TIMEOUT = const(100)
_R1_IDLE_STATE = const(1 << 0)
#R1_ERASE_RESET = const(1 << 1)
_R1_ILLEGAL_COMMAND = const(1 << 2)
#R1_COM_CRC_ERROR = const(1 << 3)
@cwyark
cwyark / sht2x.py
Last active June 7, 2017 09:24
sht2x driver for MicroPython
import utime as time
import umachine as machine
sht20_i2c_addr = 0b1000000 #0b1000000 = 0x40
cmd_reset = 0b11111110 # 0b11111110 = 0xFE
cmd_trigger_temp_measurement_with_hold = 0b11100011
cmd_trigger_humi_measurement_with_hold = 0b11100101
cmd_trigger_temp_measurement_without_hold = 0b11110011
cmd_trigger_humi_measurement_without_hold = 0b11110101
cmd_user_register_read = 0b11100111
@cwyark
cwyark / ds1338.py
Last active March 27, 2017 12:59
ds1338 driver for micropython
import utime as time
import umachine as machine
import ubinascii as binascii
ds1338_address = 0b1101000
def _bcd_to_int(bcd):
out = 0
for d in (bcd >> 4, bcd):
for p in (1, 2, 4 ,8):
from umachine import UART
import utime as time
debug = False
class Driver:
def __init__(self, uart):
if type(uart) is not UART:
raise ValueError("%s is not a valid type" % uart)
self.uart = uart
@cwyark
cwyark / lcd1602.py
Last active October 28, 2017 15:12
#LCD 1602 I2C Driver
import utime as time
from umachine import I2C
LCD_CLEARDISPLAY = 0x01
LCD_RETURNHOME = 0x02
LCD_ENTRYMODESET = 0x04
LCD_DISPLAYCONTROL = 0x08
LCD_CURSORSHIFT = 0x10
from umachine import Pin, UART, WDT
import utime, ameba
relay_ctrl_pin = Pin("PD_4", mode=Pin.OUT)
uart = UART(0, rx="PA_6", tx="PA_7")
uart.init()
wdt = WDT()
utime.sleep(3)
import sys, gc
import umachine as machine
import uterminal
from umachine import SPI, Pin, I2C
sys.path.append("/flash/lib")
#debug_uart = machine.UART(2, tx="PA_4", rx="PA_0", baudrate=115200)
#debug_uart.init()
#uterminal.register(debug_uart)
import usocket as socket
telnet_server_fd = socket.socket()
telnet_server_fd.bind(("0.0.0.0", 23))
telnet_server_fd.listen(2)
telnet_client_fd = None
def setup_telnet(s):
global telnet_client_fd
telnet_client_fd, addr = telnet_server_fd.accept()
uterminal.register(telnet_client_fd)
telnet_client_fd.setblocking(False)
@cwyark
cwyark / gist:423528b937c9560d85f22b1f0ede89a1
Created July 7, 2017 16:02
How to fast copy you ssh public key via ssh?
ssh-copy-id -i ~/.ssh/id_rsa.pub USER@HOST
@cwyark
cwyark / flashbdev.py
Created October 17, 2017 06:00
flashbdev.py for Ameba
try:
import ameba
import time
import uio as io
import array
import uctypes
from utils import fast_copy
except:
raise ImportError("ameba module import failed")