예제 #1
0
async def sign_tx(ctx, received_msg, keychain):
    state = State(ctx)
    mods = utils.unimport_begin()

    # Splitting ctx.call() to write() and read() helps to reduce memory fragmentation
    # between calls.
    while True:
        if __debug__:
            log.debug(__name__, "#### F: %s, A: %s", gc.mem_free(),
                      gc.mem_alloc())
        gc.collect()
        gc.threshold(gc.mem_free() // 4 + gc.mem_alloc())

        result_msg, accept_msgs = await sign_tx_dispatch(
            state, received_msg, keychain)
        if accept_msgs is None:
            break

        await ctx.write(result_msg)
        del (result_msg, received_msg)
        utils.unimport_end(mods)

        received_msg = await ctx.read(accept_msgs)

    utils.unimport_end(mods)
    return result_msg
예제 #2
0
 async def _garbage_collect(self):
     led = pyb.LED(2)
     while True:
         led.toggle()
         await asyncio.sleep_ms(500)
         gc.collect()
         gc.threshold(gc.mem_free() // 4 + gc.mem_alloc())
예제 #3
0
    async def check_wifi(self):
        while True:
            collect()
            threshold(mem_free() // 4 + mem_alloc())
            print("> Free memory: {}".format(mem_free()))

            # self.clock.play_spinner(SPINNER_RATE, ORANGE)

            self.portal = CaptivePortal(PUBLIC_NAME +
                                        b"-%s" % self.settings.net_id)
            self.portal.start()

            while not self.sta_if.isconnected():
                await sleep_ms(1000)

            ip = self.sta_if.ifconfig()[0]
            self.http.set_ip(ip)

            print("> Connected to {:s} ({:s})".format(self.credentials.essid,
                                                      ip))

            self.portal = None

            collect()
            print("> Free memory: {}".format(mem_free()))

            self.clock.stop_clock()
            self.clock.stop_effect_init = True
            self.clock.display()

            while self.sta_if.isconnected():
                await sleep_ms(1000)
예제 #4
0
파일: __init__.py 프로젝트: sassod/uPyEasy
def main(**params):
    # auto collect garbage
    gc.enable()
    # Max 1/4 heap used: start auto collect
    gc.threshold((gc.mem_free() + gc.mem_alloc()) // 4)

    #Start init
    init_ok = init()

    if init_ok.init():
        # load pages
        from . import pages

        #We have a network!
        core._log.debug("Main: Pre-loading home page")
        # Preload templates to avoid memory fragmentation issues
        gc.collect()
        app._load_template('homepage.html')
        gc.collect()

        #get ip address
        ip_address = core._hal.get_ip_address()
        config = db.configTable.getrow()

        core._log.debug("Main: uPyEasy Main Async Loop")
        app.run(host=ip_address, port=config["port"], debug=True, **params)
        #app.run(host=ip_address, port=config["port"],debug=True, key=ssl.key, cert=ssl.cert, **params)   # SSL version
    else:
        #No network, exit!
        print("Exiting: Network not available, set network values!")
        return
예제 #5
0
    def ai_turn(self):
        print("\nThinking; please wait...\n")

        start_at = time.ticks_ms()

        # If this is the first move, pick the center (score: 3)
        if self.last_play_rc == (None, None):
            col, minimax_score = 3, 3
        else:
            # Do GC collection now; this can be an optimization hack in MicroPy
            gc.collect()
            gc.threshold(gc.mem_free() // 4 + gc.mem_alloc())

            col, minimax_score = minimax(board=self.board,
                                         depth=self.plies,
                                         alpha=_MINUS_INFINITY,
                                         beta=_INFINITY,
                                         max_player=True,
                                         at_top=True)

        if self.debug:
            print("ai: minimax score=", minimax_score, "time=",
                  time.ticks_ms() - start_at)

        return col
예제 #6
0
def init_gc():
    '''
    Eanble GC and run it automatically when the allocated heap memory is
    over the 85% of the total heap memory
    '''
    if not gc.isenabled():
        gc.enable()
    gc.threshold(int((gc.mem_free() + gc.mem_alloc()) * 0.85))
    gc.collect()
예제 #7
0
파일: mpyeasy.py 프로젝트: enesbcs/mpyeasy
def main(**params):
    global timer100ms, timer20ms, timer1s, timer2s, timer30s, init_ok
    try:
        gc.enable()
    except:
        pass
    #Start init
    try:
        tf = open('www/dash.js', "r")
        doinit = False
    except OSError:
        doinit = True
    if doinit:  #first run
        try:
            import inc.initdata as initdata
            initdata.run()
        except:
            pass
    try:
        settings.loadsettings()
        settings.loadadvsettings()
        hardwareInit()
        networkInit()
        PluginInit()
        CPluginInit()
        NotifierInit()
        RulesInit()
        init_ok = True
    except Exception as e:
        print("Init error, failing:", str(e))
        init_ok = False
    timer100ms = utime.ticks_ms()
    timer20ms = timer100ms
    timer1s = timer100ms
    timer2s = timer100ms
    timer30s = timer100ms
    gc.collect()
    try:
        if pglobals.lowram:
            gc.threshold(4096)  #for esp32 wroom
        else:
            gc.threshold(gc.mem_free() // 25)  # for esp32 wrover
    except:
        pass
    if init_ok:
        import webserver
        print("Start mainloop")
        start_new_thread(mainloop, ())
        # Run webserver loop
        print("Starting webserver at", unet.get_ip())
        webserver.WebServer.Start(threaded=False)
        #_thread.start_new_thread(webserver.webstart, ())
        #        mainloop()
        while init_ok:
            pass
    print("Program terminated")
예제 #8
0
def main():
    # auto collect garbage
    gc.enable()
    # Max 1/4 heap used: start auto collect
    gc.threshold((gc.mem_free() + gc.mem_alloc()) // 4)

    # start = Start()
    # s.run()

    while True:
        app.run(host='0.0.0.0', debug=True, log=shared._log)
예제 #9
0
 def _idle_thread(self):
     if self.gc_enable and (self.last_gc == 0 or after(self.last_gc) > GCTIME):
         gc.collect()
         gc.threshold(gc.mem_free() // 4 + gc.mem_alloc())
         self.last_gc = ticks_us()
     if self.heartbeat is not None and (self.last_heartbeat == 0 or after(self.last_heartbeat) > HBTIME):
         if platform == 'pyboard':
             self.heartbeat.toggle()
         elif platform == 'esp8266':
             self.heartbeat(not self.heartbeat())
         self.last_heartbeat = ticks_us()
    async def send_response(self, reader, writer):
        peername = writer.get_extra_info('peername')
        print('\nRequest from:', peername)
        await writer.awrite(b'HTTP/1.0 200 OK\r\n')
        await writer.awrite(b'Content-type: text/html; charset=utf-8\r\n\r\n')

        await writer.awrite(_HTML_PREFIX)

        await writer.awrite(b'Your IP: %s port:%s\n' % peername)

        await writer.awrite(b'\n')

        method, url, querystring, body = await self.parse_request(reader)
        parsed_querystring = request_query2dict(querystring)
        parsed_body = request_query2dict(body)

        await writer.awrite(b'Method: %s\n' % method)
        await writer.awrite(b'URL: %s\n' % url)
        await writer.awrite(b'querystring: %s\n' % querystring)
        await writer.awrite(b'parsed querystring: %s\n' % parsed_querystring)
        await writer.awrite(b'body: %s\n' % body)
        await writer.awrite(b'parsed body: %s\n' % parsed_body)

        threshold = parsed_querystring.get('threshold')
        if threshold is None:
            threshold = parsed_body.get('threshold')
        if threshold is not None:
            threshold = int(threshold)
            await writer.awrite(b'Set gc.threshold() to: %i Bytes\n' % threshold)
            gc.threshold(threshold)

        await writer.awrite(_HTML_SUFFIX.format(
            threshold=gc.threshold()
        ))

        alloc = gc.mem_alloc() / 1024
        free = gc.mem_free() / 1024

        await writer.awrite(
            b'<p>RAM total: {total:.2f} KB, used: {alloc:.2f} KB, free: {free:.2f} KB</p>'.format(
                total=alloc + free,
                alloc=alloc,
                free=free
            )
        )
        await writer.awrite(b'<p>gc.threshold(): %i Bytes</p>' % gc.threshold())
        await writer.awrite(
            b'<p>Render time: %i ms</p>' % utime.ticks_diff(utime.ticks_ms(), self.start_time)
        )

        await writer.awrite(_HTML_FOOTER)
        await writer.aclose()
예제 #11
0
    def run_gc(self):
        """
        Curate the garbage collector.
        https://docs.pycom.io/firmwareapi/micropython/gc.html

        For a "quick fix", issue the following periodically.
        https://community.hiveeyes.org/t/timing-things-on-micropython-for-esp32/2329/9
        """
        import gc
        log.info('Start curating the garbage collector')
        gc.threshold(gc.mem_free() // 4 + gc.mem_alloc())
        gc.collect()
        log.info('Curating the garbage collector finished')
예제 #12
0
def main(**params):
    # auto collect garbage
    gc.enable()
    # Max 1/4 heap used: start auto collect
    gc.threshold((gc.mem_free() + gc.mem_alloc()) // 4)

    #Start init
    init_ok = init()

    if init_ok.init():
        # load pages
        from . import pages

        #We have a network!
        core._log.debug("Main: Pre-loading home page")
        # Preload templates to avoid memory fragmentation issues
        gc.collect()
        app._load_template('homepage.html')
        gc.collect()

        # Run only in STA mode!
        if core.initial_upyeasywifi == "STA":
            #get ip address
            ip_address = core._hal.get_ip_address()
            config = db.configTable.getrow()
            port = config["port"]
            # Schedule plugin/protocol async coro's!
            core._log.debug("Main: Schedule async loops")
            # get loop
            loop = asyncio.get_event_loop()
            # Create async tasks
            loop.create_task(core._plugins.asyncdevices())
            loop.create_task(core._protocols.asynccontrollers())
            loop.create_task(core._scripts.asyncscripts())
        else:
            # WIFI AP mode
            ip_address = "0.0.0.0"
            port = 80

        core._log.debug("Main: uPyEasy Main Async Loop")
        app.run(host=ip_address,
                port=port,
                debug=False,
                log=core._log,
                **params)
        #app.run(host=ip_address, port=config["port"],debug=True, key=ssl.key, cert=ssl.cert, **params)   # SSL version
    else:
        #No network, exit!
        print("Exiting: Network not available, set network values!")
        return
예제 #13
0
def initialize_google_sheets():
    # things to remember:
    # create google service account
    # create key in web interface
    # convert key to rsa
    # enable apis
    # get email address for service account

    # enable garbage collection
    gc.enable()
    print('garbage collection threshold: ' + str(gc.threshold()))

    # load configuration for a file
    config = Config('main.conf', 'key.json')

    # create an instance of ServiceAccount class
    # which then is going to be used to obtain an OAuth2 token
    # for writing data to a sheet
    sa = ServiceAccount()
    # this is not your gmail address - it is a service account address
    sa.email(config.get('google_service_account_email'))
    sa.scope('https://www.googleapis.com/auth/spreadsheets')

    # sh extract_key.sh ../stone-host-164605-297e1653f579.json ../key.json
    # need to convert json key to rsa key with this script
    sa.private_rsa_key(config.private_rsa_key())

    # create an instance of Spreadsheet which is used to write data to a sheet
    spreadsheet = Spreadsheet()
    spreadsheet.set_service_account(sa)
    spreadsheet.set_id(config.get('google_sheet_id'))
    spreadsheet.set_range('A:A')
    return spreadsheet
예제 #14
0
    def do_message_callback(self, b_topic, payload):
        topic = b_topic.decode()  #convert to string

        import gc, micropython
        gc.collect()
        gc.threshold(gc.mem_free() // 4 + gc.mem_alloc())
        micropython.mem_info()

        Util.log(self,
                 "received: topic '{}' payload: '{}'".format(topic, payload))
        if topic == self.exit_topic:
            raise ExitGatewayException()
        for device in self.devices:
            if device.do_message(topic, payload):
                #        Util.log(self,"consumed: topic '{}' payload: '{}' by device {}".format(topic,payload,json.dumps(device.config)))
                #        break
                return
예제 #15
0
async def maintain_memory(interval: int = INTERVAL_MEM):
    "run GC at a 10 minute interval"
    while 1:
        before = gc.mem_free()  #pylint: disable=no-member
        gc.collect()
        gc.threshold(gc.mem_free() // 4 + gc.mem_alloc())  #pylint: disable=no-member
        after = gc.mem_free()  #pylint: disable=no-member
        log.debug("freed: {0:,} - now free: {1:,}".format(
            after - before,
            after).replace(',',
                           '.'))  # EU Style : use . as a thousands seperator
        glb_mqtt_client.publish_one(ROOT_TOPIC + b"/sensor/mem_free",
                                    str(after))
        glb_mqtt_client.publish_one(ROOT_TOPIC + b"/sensor/cpu_temp",
                                    str(cpu_temp()))
        glb_mqtt_client.publish_one(ROOT_TOPIC + b"/sensor/client_id",
                                    NETWORK_ID)
        await asyncio.sleep(interval)
예제 #16
0
 async def poll_async(self):
     while True:
         try:
             gc.collect()  # something about memory
             gc.threshold(gc.mem_free() // 4 + gc.mem_alloc())
             print("Polling for new image...")
             url = "https://letterbox.mayursaxena.com/.netlify/functions/waiting"
             r = self.__requester.request('POST',
                                          url,
                                          json={
                                              "id": self.serial_number,
                                              "content": 1
                                          })
             if r.status_code != 200:
                 print('{0} {1}: {2}'.format(r.status_code, r.reason,
                                             r.text))
             else:
                 json_resp = r.json()
                 if 'content' in json_resp:
                     img_bytes = self.__decrypt_img(
                         loads(
                             a2b_base64(
                                 json_resp["content"]).strip().decode(
                                     'utf-8')))
                     notify_task = uasyncio.create_task(
                         self.__start_notifier())  # start spinning!
                     print('Waiting for lid to open...')
                     await self.__wait_for_lid('open'
                                               )  # "blocks" until lid opens
                     notify_task.cancel()  # stop spinning
                     self.__display_image(
                         img_bytes)  # put the image on the screen
                     print('Waiting for lid to close...')
                     await self.__wait_for_lid(
                         'close')  # "blocks" until lid closes
                     self.__acknowledge_image(
                         json_resp["sha"])  # delete image off the server
                     self.__display.clear(
                     )  # clear the display to avoid burn-in (necessary?)
             r.close()
             await uasyncio.sleep(30)  # wait 30 seconds between each poll
         except Exception as e:
             print("Encountered error in poll: {0}".format(e))
예제 #17
0
 async def from_pyboard(self):
     client = self.client
     while True:
         istr = await self.await_obj(20)  # wait for string (poll interval 20ms)
         s = istr.split(SEP)
         command = s[0]
         if command == PUBLISH:
             await client.publish(s[1], s[2], bool(s[3]), int(s[4]))
             # If qos == 1 only returns once PUBACK received.
             self.send(argformat(STATUS, PUBOK))
         elif command == SUBSCRIBE:
             await client.subscribe(s[1], int(s[2]))
             client.subscriptions[s[1]] = int(s[2])  # re-subscribe after outage
         elif command == MEM:
             gc.collect()
             gc.threshold(gc.mem_free() // 4 + gc.mem_alloc())
             self.send(argformat(MEM, gc.mem_free(), gc.mem_alloc()))
         elif command == TIME:
             t = await client.get_time()
             self.send(argformat(TIME, t))
         else:
             self.send(argformat(STATUS, UNKNOWN, 'Unknown command:', istr))
예제 #18
0
def bed_sensor_loop():
    for sensor in BedSensor.sensors():
        update_sensor(sensor, push=True)

    log('Running infinite sensor loop')

    while True:
        for sensor in BedSensor.sensors():
            update_sensor(sensor)

        if config['settings']['m5stack']:
            update_screen()

        # look for control topic messages
        check_mqtt()

        # garbage collect
        gc.collect()
        gc.threshold(gc.mem_free() // 4 + gc.mem_alloc())

        # take a nap, you worked hard
        utime.sleep(1)
예제 #19
0
def extract_tokyo_data(dataStream):
    # # infection_data_str = str(dataStream)[12:-2]
    # infection_data_str = dataStream.decode()
    # dataStream = ""

    # gc.collect()  # garbage collector
    # # gc.threshold(gc.mem_free() // 4 + gc.mem_alloc())
    # print(gc.mem_free())
    # infection_data = infection_data_str.split('},\n  {')
    # infection_data_str = ""
    # gc.collect()  # garbage collector
    # print(gc.mem_free())
    # if len(infection_data) == 47:   #Tokyo's data is 47th in JSON
    #     infection_data_tokyo = infection_data[12]
    #     infection_data = ""
    #     gc.collect()
    #     print(gc.mem_free())

    #     import ujson
    #     return ujson.loads('{' + infection_data_tokyo + '}')
    #     # return ujson.loads('{' + infection_data[12] + '}')
    # else:
    #     return {'cases':0, 'last_updated': {'cases_date':0}}
    try:
        infection_data_tokyo = dataStream.decode().split('},\n  {')[12]     #Tokyo is 12th in Json data
        # print(infection_data_tokyo)
        dataStream = ""

        gc.collect()  # garbage collector
        gc.threshold(gc.mem_free() // 4 + gc.mem_alloc())
        import ujson
        return ujson.loads('{' + infection_data_tokyo + '}')
    except Exception as e:
        print("Error: {0}\n".format(e))
        debug_log("Error on data extraction: {0}\n".format(e))
        return {'cases':0, 'last_updated': {'cases_date':0}}
예제 #20
0
def check_update():
    display.fill(COLOR565_ACTIVE)
    gc.collect()
    gc.threshold(gc.mem_free() // 4 + gc.mem_alloc())

    if connect_network():
        if set_networktime():
            infection_data = extract_tokyo_data(updatePic_sockets(URL))

            gc.collect()  # garbage collector
            gc.threshold(gc.mem_free() // 4 + gc.mem_alloc())

            # print(type(infection_data))
            # print(infection_data['cases'])
            # print(infection_data['last_updated']['cases_date'])
            dailyInfections = get_daily_infections(
                infection_data['last_updated']['cases_date'], infection_data['cases'])
            
            display.fill(COLOR565_INACTIVE)
            display.text(str(dailyInfections), TEXT_X, TEXT_Y, color = 0x0, background = COLOR565_INACTIVE)
            stepper.moveto(dailyInfections)

    gc.collect()
    gc.threshold(gc.mem_free() // 4 + gc.mem_alloc())
예제 #21
0
 async def _garbage_collect(self):
     while True:
         await asyncio.sleep_ms(100)
         gc.collect()
         gc.threshold(gc.mem_free() // 4 + gc.mem_alloc())
예제 #22
0
async def run_gc():
    gc.collect()
    gc.threshold(gc.mem_free() // 4 + gc.mem_alloc())
    asyncio.sleep(30)
예제 #23
0
        "name": name
    })

print("WIFI : Connecting ....")

## Setup WIFI Networking as a client
if 'wifi-station' in settings:
    nic = network.WLAN(network.STA_IF)
    nic.active(True)
    check_wifi(nic)

print("WIFI : Connected ")

## Clean out allocated memory so far
gc.collect()
gc.threshold(gc.mem_free() // 4 + gc.mem_alloc())

## Create MQTT Connection
c = mqtt.MQTTClient(client_id=settings['device']['name'],
                    server=settings['mqtt']['server'],
                    user=settings['mqtt']['user'],
                    password=settings['mqtt']['key'],
                    ssl=False)

print("MQTT Client : Created")

c.connect()
print("MQTT Client : Connected")

# Publish temperatures to Adafruit IO using MQTT
#
예제 #24
0
파일: _boot.py 프로젝트: ojake/micropython
import gc
gc.threshold((gc.mem_free() + gc.mem_alloc()) // 2)
import uos
from flashbdev import bdev

try:
    if bdev:
        vfs = uos.VfsFat(bdev, "")
except OSError:
    import inisetup
    vfs = inisetup.setup()

gc.collect()
예제 #25
0
파일: gc1.py 프로젝트: BWhitten/micropython
# basic tests for gc module

try:
    import gc
except ImportError:
    print("SKIP")
    import sys
    sys.exit()

print(gc.isenabled())
gc.disable()
print(gc.isenabled())
gc.enable()
print(gc.isenabled())

gc.collect()

if hasattr(gc, 'mem_free'):
    # uPy has these extra functions
    # just test they execute and return an int
    assert type(gc.mem_free()) is int
    assert type(gc.mem_alloc()) is int

if hasattr(gc, 'threshold'):
    # uPy has this extra function
    # check execution and returns
    assert(gc.threshold(1) is None)
    assert(gc.threshold() == 0)
    assert(gc.threshold(-1) is None)
    assert(gc.threshold() == -1)
예제 #26
0
    # initializes a new handler
    def __init__(self, sheet):
        self.sheet = sheet

    # send data to the sheet
    def handle(self, data):
        now = "=TIMESTAMP_TO_DATE(INDIRECT(\"A\" & ROW()))"
        data.append(now)
        print('send the following to the sheet: %s' % data)
        self.sheet.append_values(data)


# enable garbage collection
gc.enable()
print('garbage collection threshold: ' + str(gc.threshold()))

# load configuration for a file
config = Config('main.conf', 'key.json')

# initialize an interface to LEDs
lights = Lights(config.get('wifi_led_pid'), config.get('error_led_pid'),
                config.get('high_co2_led_pin'))
lights.off()

# create an instance of ServiceAccount class
# which then is going to be used to obtain an OAuth2 token
# for writing data to a sheet
sa = ServiceAccount()
sa.email(config.get('google_service_account_email'))
sa.scope('https://www.googleapis.com/auth/spreadsheets')
예제 #27
0
def setGCThreshold(size):
    print('set setGCThreshold: {:d}'.format(size))
    threshold(size)
async def mem_manage():         # Necessary for long term stability
    while True:
        await asyncio.sleep_ms(100)
        gc.collect()
        gc.threshold(gc.mem_free() // 4 + gc.mem_alloc())
예제 #29
0
async def mem_manage():  # Necessary for long term stability
    while True:
        await asyncio.sleep_ms(100)
        gc.collect()
        gc.threshold(gc.mem_free() // 4 + gc.mem_alloc())
예제 #30
0
import gc

gc.threshold((gc.mem_free() + gc.mem_alloc()) // 4)
import uos
from flashbdev import bdev

try:
    if bdev:
        vfs = uos.VfsFat(bdev, "")
except OSError:
    import inisetup
    vfs = inisetup.setup()

gc.collect()
예제 #31
0
from uos import statvfs
from logging import basicConfig, getLogger, DEBUG, debug
from gc import enable, collect, threshold, mem_free, mem_alloc

from common import utils

# Logger config
basicConfig(level=DEBUG)
main_logger = getLogger(None)
main_logger.setLevel(DEBUG)

# Info about start
debug("=== BOOT START ===")
utils.print_reset_wake_state()

# Garbage Collector
enable()
threshold(mem_free() // 4 + mem_alloc())

# Diagnostic info - not needed in production
collect()
debug("Free memory: " + str(mem_free()))
fs_stat = statvfs('//')
debug("Free flash: {} MB".format((fs_stat[0] * fs_stat[3]) / 1048576))
del fs_stat
예제 #32
0
# -*- coding: utf-8 -*-
""" uBLE REPL """
import gc
gc.threshold(4096)
import os
import binascii
import ustruct
import utime
import _thread

import collections
import logging

from bluetooth_low_energy.api.constants import *
from bluetooth_low_energy.api.characteristic import Characteristic
from bluetooth_low_energy.api.descriptor import Descriptor
from bluetooth_low_energy.api.peripheral import Peripheral
from bluetooth_low_energy.api.service import Service
from bluetooth_low_energy.api.uuid import UUID

logging.basicConfig(level=logging.INFO)
log = logging.getLogger("repl")


def main():
    """ main """

    buffer = collections.deque()

    class BleRepl(object):
        """ BleRepl """
예제 #33
0
def set_gc():
    threshold(mem_free() // 4 + mem_alloc())