mirror of
https://github.com/m5stack/M5Stack_MicroPython.git
synced 2026-05-20 10:14:44 -07:00
Some missing licensing information added
This commit is contained in:
@@ -0,0 +1,19 @@
|
||||
|
||||
***
|
||||
|
||||
**microWebServer** module is slightly modified source from [MicroWebServer](https://github.com/jczic/MicroWebSrv)
|
||||
|
||||
The MIT License (MIT)
|
||||
Copyright © 2017 Jean-Christophe Bos
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
***
|
||||
|
||||
**pye** module is slightly modified source from [Micropython-Editor](https://github.com/robert-hh/Micropython-Editor)
|
||||
|
||||
***
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -1,300 +0,0 @@
|
||||
|
||||
from sys import exc_info
|
||||
import re
|
||||
|
||||
class MicroWebTemplate :
|
||||
|
||||
# ============================================================================
|
||||
# ===( Constants )============================================================
|
||||
# ============================================================================
|
||||
|
||||
TOKEN_OPEN = '{{'
|
||||
TOKEN_CLOSE = '}}'
|
||||
TOKEN_OPEN_LEN = len(TOKEN_OPEN)
|
||||
TOKEN_CLOSE_LEN = len(TOKEN_CLOSE)
|
||||
|
||||
INSTRUCTION_PYTHON = 'py'
|
||||
INSTRUCTION_IF = 'if'
|
||||
INSTRUCTION_ELIF = 'elif'
|
||||
INSTRUCTION_ELSE = 'else'
|
||||
INSTRUCTION_FOR = 'for'
|
||||
INSTRUCTION_END = 'end'
|
||||
|
||||
# ============================================================================
|
||||
# ===( Constructor )==========================================================
|
||||
# ============================================================================
|
||||
|
||||
def __init__(self, code, escapeStrFunc=None) :
|
||||
self._code = code
|
||||
self._escapeStrFunc = escapeStrFunc
|
||||
self._pos = 0
|
||||
self._endPos = len(code)-1
|
||||
self._line = 1
|
||||
self._reIdentifier = re.compile(r'[a-zA-Z_][a-zA-Z0-9_]*$')
|
||||
self._pyGlobalVars = { }
|
||||
self._pyLocalVars = { }
|
||||
self._rendered = ''
|
||||
self._instructions = {
|
||||
MicroWebTemplate.INSTRUCTION_PYTHON : self._processInstructionPYTHON,
|
||||
MicroWebTemplate.INSTRUCTION_IF : self._processInstructionIF,
|
||||
MicroWebTemplate.INSTRUCTION_ELIF : self._processInstructionELIF,
|
||||
MicroWebTemplate.INSTRUCTION_ELSE : self._processInstructionELSE,
|
||||
MicroWebTemplate.INSTRUCTION_FOR : self._processInstructionFOR,
|
||||
MicroWebTemplate.INSTRUCTION_END : self._processInstructionEND,
|
||||
}
|
||||
|
||||
# ============================================================================
|
||||
# ===( Functions )============================================================
|
||||
# ============================================================================
|
||||
|
||||
def Validate(self) :
|
||||
try :
|
||||
self._parseCode(execute=False)
|
||||
return None
|
||||
except :
|
||||
return exc_info()[1]
|
||||
|
||||
# ----------------------------------------------------------------------------
|
||||
|
||||
def Execute(self) :
|
||||
try :
|
||||
self._parseCode(execute=True)
|
||||
return self._rendered
|
||||
except :
|
||||
raise Exception(exc_info()[1])
|
||||
|
||||
# ============================================================================
|
||||
# ===( Utils )===============================================================
|
||||
# ============================================================================
|
||||
|
||||
def _parseCode(self, execute) :
|
||||
self._pyGlobalVars = { }
|
||||
self._pyLocalVars = { }
|
||||
self._rendered = ''
|
||||
newTokenToProcess = self._parseBloc(execute)
|
||||
if newTokenToProcess is not None :
|
||||
raise Exception( '"%s" instruction is not valid here (line %s)'
|
||||
% (newTokenToProcess, self._line) )
|
||||
|
||||
# ----------------------------------------------------------------------------
|
||||
|
||||
def _parseBloc(self, execute) :
|
||||
while self._pos <= self._endPos :
|
||||
c = self._code[self._pos]
|
||||
if c == MicroWebTemplate.TOKEN_OPEN[0] and \
|
||||
self._code[ self._pos : self._pos + MicroWebTemplate.TOKEN_OPEN_LEN ] == MicroWebTemplate.TOKEN_OPEN :
|
||||
self._pos += MicroWebTemplate.TOKEN_OPEN_LEN
|
||||
tokenContent = ''
|
||||
x = self._pos
|
||||
while True :
|
||||
if x > self._endPos :
|
||||
raise Exception("%s is missing (line %s)" % (MicroWebTemplate.TOKEN_CLOSE, self._line))
|
||||
c = self._code[x]
|
||||
if c == MicroWebTemplate.TOKEN_CLOSE[0] and \
|
||||
self._code[ x : x + MicroWebTemplate.TOKEN_CLOSE_LEN ] == MicroWebTemplate.TOKEN_CLOSE :
|
||||
self._pos = x + MicroWebTemplate.TOKEN_CLOSE_LEN
|
||||
break
|
||||
elif c == '\n' :
|
||||
self._line += 1
|
||||
tokenContent += c
|
||||
x += 1
|
||||
newTokenToProcess = self._processToken(tokenContent, execute)
|
||||
if newTokenToProcess is not None :
|
||||
return newTokenToProcess
|
||||
continue
|
||||
elif c == '\n' :
|
||||
self._line += 1
|
||||
if execute :
|
||||
self._rendered += c
|
||||
self._pos += 1
|
||||
return None
|
||||
|
||||
# ----------------------------------------------------------------------------
|
||||
|
||||
def _processToken(self, tokenContent, execute) :
|
||||
tokenContent = tokenContent.strip()
|
||||
parts = tokenContent.split(' ', 1)
|
||||
instructName = parts[0].strip()
|
||||
instructBody = parts[1].strip() if len(parts) > 1 else None
|
||||
if len(instructName) == 0 :
|
||||
raise Exception( '"%s %s" : instruction is missing (line %s)'
|
||||
% (MicroWebTemplate.TOKEN_OPEN, MicroWebTemplate.TOKEN_CLOSE, self._line) )
|
||||
newTokenToProcess = None
|
||||
if instructName in self._instructions :
|
||||
newTokenToProcess = self._instructions[instructName](instructBody, execute)
|
||||
elif execute :
|
||||
try :
|
||||
s = str( eval( tokenContent,
|
||||
self._pyGlobalVars,
|
||||
self._pyLocalVars ) )
|
||||
if (self._escapeStrFunc is not None) :
|
||||
self._rendered += self._escapeStrFunc(s)
|
||||
else :
|
||||
self._rendered += s
|
||||
except :
|
||||
raise Exception('%s (line %s)' % (exc_info()[1], self._line))
|
||||
return newTokenToProcess
|
||||
|
||||
# ----------------------------------------------------------------------------
|
||||
|
||||
def _processInstructionPYTHON(self, instructionBody, execute) :
|
||||
if instructionBody is not None :
|
||||
raise Exception( 'Instruction "%s" is invalid (line %s)'
|
||||
% (MicroWebTemplate.INSTRUCTION_PYTHON, self._line) )
|
||||
pyCode = ''
|
||||
while True :
|
||||
if self._pos > self._endPos :
|
||||
raise Exception( '"%s" instruction is missing (line %s)'
|
||||
% (MicroWebTemplate.INSTRUCTION_END, self._line) )
|
||||
c = self._code[self._pos]
|
||||
if c == MicroWebTemplate.TOKEN_OPEN[0] and \
|
||||
self._code[ self._pos : self._pos + MicroWebTemplate.TOKEN_OPEN_LEN ] == MicroWebTemplate.TOKEN_OPEN :
|
||||
self._pos += MicroWebTemplate.TOKEN_OPEN_LEN
|
||||
tokenContent = ''
|
||||
x = self._pos
|
||||
while True :
|
||||
if x > self._endPos :
|
||||
raise Exception("%s is missing (line %s)" % (MicroWebTemplate.TOKEN_CLOSE, self._line))
|
||||
c = self._code[x]
|
||||
if c == MicroWebTemplate.TOKEN_CLOSE[0] and \
|
||||
self._code[ x : x + MicroWebTemplate.TOKEN_CLOSE_LEN ] == MicroWebTemplate.TOKEN_CLOSE :
|
||||
self._pos = x + MicroWebTemplate.TOKEN_CLOSE_LEN
|
||||
break
|
||||
elif c == '\n' :
|
||||
self._line += 1
|
||||
tokenContent += c
|
||||
x += 1
|
||||
tokenContent = tokenContent.strip()
|
||||
if tokenContent == MicroWebTemplate.INSTRUCTION_END :
|
||||
break
|
||||
raise Exception( '"%s" is a bad instruction in a python bloc (line %s)'
|
||||
% (tokenContent, self._line) )
|
||||
elif c == '\n' :
|
||||
self._line += 1
|
||||
if execute :
|
||||
pyCode += c
|
||||
self._pos += 1
|
||||
if execute :
|
||||
lines = pyCode.split('\n')
|
||||
indent = ''
|
||||
for line in lines :
|
||||
if len(line.strip()) > 0 :
|
||||
for c in line :
|
||||
if c == ' ' or c == '\t' :
|
||||
indent += c
|
||||
else :
|
||||
break
|
||||
break
|
||||
pyCode = ''
|
||||
for line in lines :
|
||||
if line.find(indent) == 0 :
|
||||
line = line[len(indent):]
|
||||
pyCode += line + '\n'
|
||||
try :
|
||||
exec(pyCode, self._pyGlobalVars, self._pyLocalVars)
|
||||
except :
|
||||
raise Exception('%s (line %s)' % (exc_info()[1], self._line))
|
||||
return None
|
||||
|
||||
# ----------------------------------------------------------------------------
|
||||
|
||||
def _processInstructionIF(self, instructionBody, execute) :
|
||||
if instructionBody is not None :
|
||||
if execute :
|
||||
try :
|
||||
result = eval(instructionBody, self._pyGlobalVars, self._pyLocalVars)
|
||||
if not isinstance(result, bool) :
|
||||
raise Exception('"%s" is not a boolean expression (line %s)' % (instructionBody, self._line))
|
||||
except :
|
||||
raise Exception('%s (line %s)' % (exc_info()[1], self._line))
|
||||
else :
|
||||
result = False
|
||||
newTokenToProcess = self._parseBloc(execute and result)
|
||||
if newTokenToProcess is not None :
|
||||
if newTokenToProcess == MicroWebTemplate.INSTRUCTION_END :
|
||||
return None
|
||||
elif newTokenToProcess == MicroWebTemplate.INSTRUCTION_ELSE :
|
||||
newTokenToProcess = self._parseBloc(execute and not result)
|
||||
if newTokenToProcess is not None :
|
||||
if newTokenToProcess == MicroWebTemplate.INSTRUCTION_END :
|
||||
return None
|
||||
raise Exception( '"%s" instruction waited (line %s)'
|
||||
% (MicroWebTemplate.INSTRUCTION_END, self._line) )
|
||||
raise Exception( '"%s" instruction is missing (line %s)'
|
||||
% (MicroWebTemplate.INSTRUCTION_END, self._line) )
|
||||
elif newTokenToProcess == MicroWebTemplate.INSTRUCTION_ELIF :
|
||||
self._processInstructionIF(self._elifInstructionBody, execute and not result)
|
||||
return None
|
||||
raise Exception( '"%s" instruction waited (line %s)'
|
||||
% (MicroWebTemplate.INSTRUCTION_END, self._line) )
|
||||
raise Exception( '"%s" instruction is missing (line %s)'
|
||||
% (MicroWebTemplate.INSTRUCTION_END, self._line) )
|
||||
raise Exception( '"%s" alone is an incomplete syntax (line %s)'
|
||||
% (MicroWebTemplate.INSTRUCTION_IF, self._line) )
|
||||
|
||||
# ----------------------------------------------------------------------------
|
||||
|
||||
def _processInstructionELIF(self, instructionBody, execute) :
|
||||
if instructionBody is None :
|
||||
raise Exception( '"%s" alone is an incomplete syntax (line %s)'
|
||||
% (MicroWebTemplate.INSTRUCTION_ELIF, self._line) )
|
||||
self._elifInstructionBody = instructionBody
|
||||
return MicroWebTemplate.INSTRUCTION_ELIF
|
||||
|
||||
# ----------------------------------------------------------------------------
|
||||
|
||||
def _processInstructionELSE(self, instructionBody, execute) :
|
||||
if instructionBody is not None :
|
||||
raise Exception( 'Instruction "%s" is invalid (line %s)'
|
||||
% (MicroWebTemplate.INSTRUCTION_ELSE, self._line) )
|
||||
return MicroWebTemplate.INSTRUCTION_ELSE
|
||||
|
||||
# ----------------------------------------------------------------------------
|
||||
|
||||
def _processInstructionFOR(self, instructionBody, execute) :
|
||||
if instructionBody is not None :
|
||||
parts = instructionBody.split(' ', 1)
|
||||
identifier = parts[0].strip()
|
||||
if self._reIdentifier.match(identifier) is not None and len(parts) > 1 :
|
||||
parts = parts[1].strip().split(' ', 1)
|
||||
if parts[0] == 'in' and len(parts) > 1 :
|
||||
expression = parts[1].strip()
|
||||
newTokenToProcess = None
|
||||
beforePos = self._pos
|
||||
if execute :
|
||||
try :
|
||||
result = eval(expression, self._pyGlobalVars, self._pyLocalVars)
|
||||
except :
|
||||
raise Exception('%s (line %s)' % (exc_info()[1], self._line))
|
||||
if execute and len(result) > 0 :
|
||||
for x in result :
|
||||
self._pyLocalVars[identifier] = x
|
||||
self._pos = beforePos
|
||||
newTokenToProcess = self._parseBloc(True)
|
||||
if newTokenToProcess != MicroWebTemplate.INSTRUCTION_END :
|
||||
break
|
||||
else :
|
||||
newTokenToProcess = self._parseBloc(False)
|
||||
if newTokenToProcess is not None :
|
||||
if newTokenToProcess == MicroWebTemplate.INSTRUCTION_END :
|
||||
return None
|
||||
raise Exception( '"%s" instruction waited (line %s)'
|
||||
% (MicroWebTemplate.INSTRUCTION_END, self._line) )
|
||||
raise Exception( '"%s" instruction is missing (line %s)'
|
||||
% (MicroWebTemplate.INSTRUCTION_END, self._line) )
|
||||
raise Exception( '"%s %s" is an invalid syntax'
|
||||
% (MicroWebTemplate.INSTRUCTION_FOR, instructionBody) )
|
||||
raise Exception( '"%s" alone is an incomplete syntax (line %s)'
|
||||
% (MicroWebTemplate.INSTRUCTION_FOR, self._line) )
|
||||
|
||||
# ----------------------------------------------------------------------------
|
||||
|
||||
def _processInstructionEND(self, instructionBody, execute) :
|
||||
if instructionBody is not None :
|
||||
raise Exception( 'Instruction "%s" is invalid (line %s)'
|
||||
% (MicroWebTemplate.INSTRUCTION_END, self._line) )
|
||||
return MicroWebTemplate.INSTRUCTION_END
|
||||
|
||||
# ============================================================================
|
||||
# ============================================================================
|
||||
# ============================================================================
|
||||
@@ -204,3 +204,56 @@ class BME280:
|
||||
hd = h * 100 // 1024 - hi * 100
|
||||
return ("{}C".format(t / 100), "{}.{:02d}hPa".format(pi, pd),
|
||||
"{}.{:02d}%".format(hi, hd))
|
||||
|
||||
|
||||
# ==================================================================
|
||||
# BME280 readings can be executed in thread:
|
||||
|
||||
'''
|
||||
import machine, _thread, time
|
||||
import micropython, gc
|
||||
import bme280
|
||||
|
||||
i2c=machine.I2C(scl=machine.Pin(26),sda=machine.Pin(25),speed=400000)
|
||||
bme=bme280.BME280(i2c=i2c)
|
||||
|
||||
def bmevalues():
|
||||
t, p, h = bme.read_compensated_data()
|
||||
|
||||
p = p // 256
|
||||
pi = p // 100
|
||||
pd = p - pi * 100
|
||||
|
||||
hi = h // 1024
|
||||
hd = h * 100 // 1024 - hi * 100
|
||||
#return "[{}] T={0:1g}C ".format(time.strftime("%H:%M:%S",time.localtime()), round(t / 100,1)) + "P={}.{:02d}hPa ".format(pi, pd) + "H={}.{:01d}%".format(hi, hd)
|
||||
return "[{}] T={}C ".format(time.strftime("%H:%M:%S",time.localtime()), t / 100) + "P={}.{:02d}hPa ".format(pi, pd) + "H={}.{:02d}%".format(hi, hd)
|
||||
|
||||
|
||||
def bmerun(interval=60):
|
||||
_thread.allowsuspend(True)
|
||||
sendmsg = True
|
||||
send_time = time.time() + interval
|
||||
while True:
|
||||
while time.time() < send_time:
|
||||
notif = _thread.getnotification()
|
||||
if notif == 10002:
|
||||
_thread.sendmsg(_thread.getReplID(), bmevalues())
|
||||
elif notif == 10004:
|
||||
sendmsg = False
|
||||
elif notif == 10006:
|
||||
sendmsg = True
|
||||
elif (notif <= 3600) and (notif >= 10):
|
||||
interval = notif
|
||||
send_time = time.time() + interval
|
||||
_thread.sendmsg(_thread.getReplID(), "Interval set to {} seconds".format(interval))
|
||||
|
||||
time.sleep_ms(100)
|
||||
send_time = send_time + interval
|
||||
if sendmsg:
|
||||
_thread.sendmsg(_thread.getReplID(), bmevalues())
|
||||
|
||||
_thread.stack_size(3*1024)
|
||||
bmeth=_thread.start_new_thread("BME280", bmerun, (60,))
|
||||
|
||||
'''
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,5 @@
|
||||
### This example code is based on
|
||||
|
||||
[SX127x_driver_for_MicroPython_on_ESP8266](https://github.com/Wei1234c/SX127x_driver_for_MicroPython_on_ESP8266)
|
||||
|
||||
Please read the LICENSE for the terms of including it in you application.
|
||||
@@ -0,0 +1,3 @@
|
||||
### Usage
|
||||
|
||||
Before running the webserver example copy the _www_ directory to the root of the internal flash!
|
||||
@@ -1,2 +0,0 @@
|
||||
|
||||
Before running webserver example copy the www directory to the internal flash!
|
||||
Reference in New Issue
Block a user