Skip to content

Commit 0e49f08

Browse files
committed
same code for esp and pico new way to check file exists for esp
1 parent a590a5c commit 0e49f08

File tree

4 files changed

+76
-88
lines changed

4 files changed

+76
-88
lines changed

board.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
"""
2+
Project Name: board.py
3+
File Name: board.py
4+
Author: GurgleApps.com
5+
Date: 2021-04-01
6+
Description: Detects the board type
7+
"""
8+
import sys
9+
import os
10+
11+
class Board:
12+
class BoardType:
13+
PICO_W = 'Raspberry Pi Pico W'
14+
PICO = 'Raspberry Pi Pico'
15+
ESP8266 = 'ESP8266'
16+
ESP32 = 'ESP32'
17+
UNKNOWN = 'Unknown'
18+
19+
def __init__(self):
20+
self.type = self.detect_board_type()
21+
22+
def detect_board_type(self):
23+
sysname = os.uname().sysname.lower()
24+
machine = os.uname().machine.lower()
25+
26+
if sysname == 'rp2' and 'pico w' in machine:
27+
return self.BoardType.PICO_W
28+
elif sysname == 'rp2' and 'pico' in machine:
29+
return self.BoardType.PICO
30+
elif sysname == 'esp8266':
31+
return self.BoardType.ESP8266
32+
# Add more conditions for other boards here
33+
else:
34+
return self.BoardType.UNKNOWN

gurgleapps_webserver.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
"""
2+
Project: GurgleApps Web Server
3+
File: gurgleapps_webserver.py
4+
Author: GurgleApps.com
5+
Date: Your Date 2023-04-01
6+
Description: GurgleApps Web Server
7+
"""
18
import network
29
import re
310
import time
@@ -137,7 +144,8 @@ async def serve_request(self, reader, writer):
137144
file_path = self.doc_root + url
138145
if self.log_level > 0:
139146
print("file_path: "+str(file_path))
140-
if uos.stat(file_path)[6] > 0:
147+
#if uos.stat(file_path)[6] > 0:
148+
if self.file_exists(file_path):
141149
content_type = self.get_content_type(url)
142150
if self.log_level > 1:
143151
print("content_type: "+str(content_type))
@@ -150,6 +158,18 @@ async def serve_request(self, reader, writer):
150158
except OSError as e:
151159
print(e)
152160

161+
def dir_exists(self, filename):
162+
try:
163+
return (os.stat(filename)[0] & 0x4000) != 0
164+
except OSError:
165+
return False
166+
167+
def file_exists(self, filename):
168+
try:
169+
return (os.stat(filename)[0] & 0x4000) == 0
170+
except OSError:
171+
return False
172+
153173
def get_file(self, filename):
154174
print("getFile: "+filename)
155175
try:

main.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,35 @@
1+
"""
2+
Project: GurgleApps Webserver
3+
File: main.py
4+
Author: GurgleApps.com
5+
Date: 2021-04-01
6+
Description: Demonstrates how to use the GurgleApps Webserver
7+
"""
18
import config
29
from gurgleapps_webserver import GurgleAppsWebserver
310
import utime as time
411
import uasyncio as asyncio
512
from machine import Pin
613
import ujson as json
14+
from board import Board
15+
16+
BOARD_TYPE = Board().type
17+
print("Board type: " + BOARD_TYPE)
18+
19+
if BOARD_TYPE == Board.BoardType.PICO_W:
20+
led = Pin("LED", Pin.OUT)
21+
elif BOARD_TYPE == Board.BoardType.PICO:
22+
led = Pin(25, Pin.OUT)
23+
elif BOARD_TYPE == Board.BoardType.ESP8266:
24+
led = Pin(2, Pin.OUT)
25+
else:
26+
led = Pin(2, Pin.OUT)
27+
728

829
blink_off_time = 0.5
930
blink_on_time = 0.5
1031

1132
status = True
12-
led = Pin("LED", Pin.OUT)
1333

1434
async def example_func(request, response, param1, param2):
1535
print("example_func")

main_esp.py

Lines changed: 0 additions & 86 deletions
This file was deleted.

0 commit comments

Comments
 (0)