<commit_before>from machine import *
import time
import esp
import ustruct
from neopixel import NeoPixel
import mpython
from ssd1106 import SSD1106_I2C

class led:
    def __init__(self, pin):
        self.val = 1
        self.pin = pin
    def setbrightness(n,val):
        global rgb
        if n in (0,1,2) and 0 <= val <= 1023:
            if val == 0:
                rgb[n] = (0,0,0)
                rgb.write()
            else:
                val = val // 20 + 1
                rgb[n] = (val,val,val)
                rgb.write()
    def setonoff(n,val):
        global rgb
        if n in (0,1,2):
            if(val == -1):
                if rgb[n] == (0,0,0):
                    rgb[n] = (50,50,50)
                    rgb.write()
                else:
                    rgb[n] = (0,0,0)
                    rgb.write()
            else:
                rgb[n] = (50*val, 50*val, 50*val)
                rgb.write()
    def getonoff(n):
        global rgb
        if n in (0,1,2):
            return int(bool((rgb[n][0]+rgb[n][1]+rgb[n][2])))

class Button:
    def __init__(self, pin):
        from machine import Pin
        self.pin = Pin(pin, Pin.IN)

    def get_presses(self, delay = 1):
        last_time, last_state, presses = time.time(), 0, 0
        while time.time() < last_time + delay:
            time.sleep_ms(50)
            if last_state == 0 and self.pin.value() == 1:
                last_state = 1
            if last_state == 1 and self.pin.value() == 0:
                last_state, presses = 0, presses + 1
        return presses

    def is_pressed(self):
        return self.pin.value() == 0

    def was_pressed(self):
        last_state = self.pin.value()
        if last_state :
            return False
        else :
            while not self.pin.value():
                time.sleep_ms(10)
            return True

    def irq(self, handler, trigger):
        self.pin.irq(handler = handler, trigger = trigger)

class MyPin(Pin):
    def write_digital(self,val):
        self.init(Pin.OUT)
        self.value(val)

    def read_digital(self):
        self.init(Pin.IN)
        return self.value()

    def write_analog(self,val):
        id = int(str(self)[4:-1]) #unsafe!
        self = PWM(Pin(id),duty=val)

    def dac_write(self,val):
        id = int(str(self)[4:-1]) #unsafe!
        self = DAC(Pin(id)).write(val)    

    def read_analog(self):
        id = int(str(self)[4:-1]) #unsafe!
        self = ADC(Pin(id))
        return self.read()

    def set_frequency(self,val):
        id = int(str(self)[4:-1])
        self = PWM(Pin(id),freq=val)

    def is_touched(self):
        id = int(str(self)[4:-1]) #unsafe!
        if id in (0,2,4,12,13,14,15,27,32,33):
            # print(TouchPad(Pin(id)).read())
            return (TouchPad(Pin(id)).read() - 150 < 0)
        else:
            self.init(Pin.IN)
            return self.value() == 1

class ADCSensor:
    def __init__(self,pin):
        self.adc=ADC(Pin(pin))
        self.adc.atten(ADC.ATTN_11DB) 
    def read(self):
        return self.adc.read()

def handbit_get_brightness():
    return ADCSensor(pin = 39).read()

def handbit_get_soundlevel():
    return ADCSensor(pin = 36).read()

class Accelerometer():
    """  """
    def __init__(self):
        self.addr = 38
        self.i2c = i2c
        self.i2c.writeto(self.addr, b'\x0F\x08')    # set resolution = 10bit
        self.i2c.writeto(self.addr, b'\x11\x00')    # set power mode = normal

    def get_a_x(self):
        self.i2c.writeto(self.addr, b'\x02', False)
        buf = self.i2c.readfrom(self.addr, 2)
        x = ustruct.unpack('h', buf)[0]
        return x / 4 / 4096

    def get_a_y(self):
        self.i2c.writeto(self.addr, b'\x04', False)
        buf = self.i2c.readfrom(self.addr, 2)
        y = ustruct.unpack('h', buf)[0]
        return y / 4 / 4096

    def get_a_z(self):
        self.i2c.writeto(self.addr, b'\x06', False)
        buf = self.i2c.readfrom(self.addr, 2)
        z = ustruct.unpack('h', buf)[0]
        return z / 4 / 4096


class Font(object):
    def __init__(self, font_address=0x300000):
        self.font_address = font_address
        buffer = bytearray(18)
        esp.flash_read(self.font_address, buffer)
        self.header, \
            self.height, \
            self.width, \
            self.baseline, \
            self.x_height, \
            self.Y_height, \
            self.first_char,\
            self.last_char = ustruct.unpack('4sHHHHHHH', buffer)
        self.first_char_info_address = self.font_address + 18

    def GetCharacterData(self, c):
        uni = ord(c)
        if uni not in range(self.first_char, self.last_char):
            return None
        char_info_address = self.first_char_info_address + \
            (uni - self.first_char) * 6
        buffer = bytearray(6)
        esp.flash_read(char_info_address, buffer)
        ptr_char_data, len = ustruct.unpack('IH', buffer)   
        if (ptr_char_data) == 0 or (len == 0):
            return None<commit_msg>Update handbit.py<commit_after>from machine import *
import time
import esp
import ustruct
from neopixel import NeoPixel
import mpython
from ssd1106 import SSD1106_I2C

class led:
    def __init__(self, pin):
        self.val = 1
        self.pin = pin
    def setbrightness(n,val):
        global rgb
        if n in (0,1,2) and 0 <= val <= 1023:
            if val == 0:
                rgb[n] = (0,0,0)
                rgb.write()
            else:
                val = val // 20 + 1
                rgb[n] = (val,val,val)
                rgb.write()
    def setonoff(n,val):
        global rgb
        if n in (0,1,2):
            if(val == -1):
                if rgb[n] == (0,0,0):
                    rgb[n] = (50,50,50)
                    rgb.write()
                else:
                    rgb[n] = (0,0,0)
                    rgb.write()
            else:
                rgb[n] = (50*val, 50*val, 50*val)
                rgb.write()
    def getonoff(n):
        global rgb
        if n in (0,1,2):
            return int(bool((rgb[n][0]+rgb[n][1]+rgb[n][2])))

class Button:
    def __init__(self, pin):
        from machine import Pin
        self.pin = Pin(pin, Pin.IN)

    def get_presses(self, delay = 1):
        last_time, last_state, presses = time.time(), 0, 0
        while time.time() < last_time + delay:
            time.sleep_ms(50)
            if last_state == 0 and self.pin.value() == 1:
                last_state = 1
            if last_state == 1 and self.pin.value() == 0:
                last_state, presses = 0, presses + 1
        return presses

    def is_pressed(self):
        return self.pin.value() == 0

    def was_pressed(self):
        last_state = self.pin.value()
        if last_state :
            return False
        else :
            while not self.pin.value():
                time.sleep_ms(10)
            return True

    def irq(self, handler, trigger):
        self.pin.irq(handler = handler, trigger = trigger)

class MyPin(Pin):
    def write_digital(self,val):
        self.init(Pin.OUT)
        self.value(val)

    def read_digital(self):
        self.init(Pin.IN)
        return self.value()

    def write_analog(self,val):
        id = int(str(self)[4:-1]) #unsafe!
        self = PWM(Pin(id),duty=val)

    def dac_write(self,val):
        id = int(str(self)[4:-1]) #unsafe!
        self = DAC(Pin(id)).write(val)    

    def read_analog(self):
        id = int(str(self)[4:-1]) #unsafe!
        self = ADC(Pin(id))
        return self.read()

    def set_frequency(self,val):
        id = int(str(self)[4:-1])
        self = PWM(Pin(id),freq=val)

    def is_touched(self):
        id = int(str(self)[4:-1]) #unsafe!
        if id in (0,2,4,12,13,14,15,27,32,33):
            # print(TouchPad(Pin(id)).read())
            return (TouchPad(Pin(id)).read() - 150 < 0)
        else:
            self.init(Pin.IN)
            return self.value() == 1

class ADCSensor:
    def __init__(self,pin):
        self.adc=ADC(Pin(pin))
        self.adc.atten(ADC.ATTN_11DB) 
    def read(self):
        return self.adc.read()

def handbit_get_brightness():
    return ADCSensor(pin = 39).read()

def handbit_get_soundlevel():
    return ADCSensor(pin = 36).read()

class Accelerometer():
    """  """
    def __init__(self):
        self.addr = 38
        self.i2c = i2c
        self.i2c.writeto(self.addr, b'\x0F\x08')    # set resolution = 10bit
        self.i2c.writeto(self.addr, b'\x11\x00')    # set power mode = normal

    def get_a_x(self):
        self.i2c.writeto(self.addr, b'\x02', False)
        buf = self.i2c.readfrom(self.addr, 2)
        x = ustruct.unpack('h', buf)[0]
        return x / 4 / 4096

    def get_a_y(self):
        self.i2c.writeto(self.addr, b'\x04', False)
        buf = self.i2c.readfrom(self.addr, 2)
        y = ustruct.unpack('h', buf)[0]
        return y / 4 / 4096

    def get_a_z(self):
        self.i2c.writeto(self.addr, b'\x06', False)
        buf = self.i2c.readfrom(self.addr, 2)
        z = ustruct.unpack('h', buf)[0]
        return z / 4 / 4096

    def get_gesture(self,choice):
        if choice == 'face up':
            if self.get_a_z() <= -1:
                return True
            else:
                return False
        if choice == 'face down':
            if self.get_a_z() >= 1:
                return True
            else:
                return False
        if choice == 'shake':
            if abs(self.get_a_x()) >= 1 and abs(self.get_a_y()) >= 1:
                return True
            else:
                return False
        if choice == 'up':
            if self.get_a_x() >= 1:
                return True
            else:
                return False
        if choice == 'down':
            if self.get_a_x() <= -1:
                return True
            else:
                return False
        if choice == 'right':
            if self.get_a_y() <= -1:
                return True
            else:
                return False
        if choice == 'left':
            if self.get_a_y() >= 1:
                return True
            else:
                return False

class Font(object):
    def __init__(self, font_address=0x300000):
        self.font_address = font_address
        buffer = bytearray(18)
        esp.flash_read(self.font_address, buffer)
        self.header, \
            self.height, \
            self.width, \
            self.baseline, \
            self.x_height, \
            self.Y_height, \
            self.first_char,\
            self.last_char = ustruct.unpack('4sHHHHHHH', buffer)
        self.first_char_info_address = self.font_address + 18

    def GetCharacterData(self, c):
        uni = ord(c)
        if uni not in range(self.first_char, self.last_char):
            return None
        char_info_address = self.first_char_info_address + \
            (uni - self.first_char) * 6
        buffer = bytearray(6)
        esp.flash_read(char_info_address, buffer)
        ptr_char_data, len = ustruct.unpack('IH', buffer)   
        if (ptr_char_data) == 0 or (len == 0):
            return None
{'fraction_non_alphanumeric': 0.13070894514007747, 'fraction_numerical': 0.03134852715971534, 'mean_word_length': 1.6401902497027347, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3625845', 'n_tokens_mistral': 3984, 'n_tokens_neox': 3670, 'n_words': 818}