From c24598a511c45ffce6f958f62fcf18a7d7a83d54 Mon Sep 17 00:00:00 2001 From: cqc Date: Sat, 3 Jan 2026 11:30:18 +0100 Subject: [PATCH] updater --- boot.py | 4 ++ code.py | 142 ++--------------------------------------------------- minimap.py | 141 ++++++++++++++++++++++++++++++++++++++++++++++++++++ update.py | 26 ++++++++++ 4 files changed, 175 insertions(+), 138 deletions(-) create mode 100644 boot.py create mode 100644 minimap.py create mode 100644 update.py diff --git a/boot.py b/boot.py new file mode 100644 index 0000000..93ff3bb --- /dev/null +++ b/boot.py @@ -0,0 +1,4 @@ +import storage + +# RISKY! +storage.remount('/', readonly=False, disable_concurrent_write_protection=True) diff --git a/code.py b/code.py index 031e793..5814a43 100755 --- a/code.py +++ b/code.py @@ -1,140 +1,6 @@ -print("\nchaos minimap!") -import os -import wifi -import adafruit_connection_manager -import adafruit_requests -import json -import time -import board -import busio -import displayio -import terminalio -from adafruit_display_text import label -# Import the SSD1306 module. -import adafruit_ssd1306 +import update +update.main() -from secrets import hauk_endpoint,hauk_session,hauk_slug,hauk_name - -# Create the I2C interface. -i2c = busio.I2C(board.D7, board.D8) - -# Create the SSD1306 OLED class. -# The first two parameters are the pixel width and pixel height. Change these -# to the right size for your display! -display = adafruit_ssd1306.SSD1306_I2C(128, 64, i2c) - -pool = adafruit_connection_manager.get_radio_socketpool(wifi.radio) -ssl_context = adafruit_connection_manager.get_radio_ssl_context(wifi.radio) -requests = adafruit_requests.Session(pool, ssl_context) - -locavg = [] -def location_avg(): - global locavg - if len(locavg) > 10: - locavg = locavg[1:] - lavg = list(zip(*locavg)) - print(lavg[0], lavg[1]) - lavg = (sum(lavg[0])/len(lavg[0]), sum(lavg[1])/len(lavg[1])) - return lavg - -def update_location(): - global locavg - print('scanning',) - wifi.radio.stop_scanning_networks() - time.sleep(2) - ns = wifi.radio.start_scanning_networks() - wifi_peers = [] - for n in ns: - #print(n.ssid, n.bssid.hex(':'), n.rssi, n.channel) - wifi_peers.append({"bssid": n.bssid.hex(':'), "ssid": n.ssid, "rssi": n.rssi}) - - #print(wifi_peers) - wifi.radio.stop_scanning_networks() - print('loc request') - - data = json.dumps(wifi_peers) - print(data) - lat,lon=0,0 - with requests.post(f"{hauk_endpoint}/location", data=data) as response: - #print(response.text) - j = response.json() - if 'triangulation' in j: - print(j['triangulation']) - lat = j['triangulation']['Position']['Lat'] - lon = j['triangulation']['Position']['Lon'] - else: - print('no location received') - - locavg.append((lat,lon)) - lavg = location_avg() - print(lavg) - form_data={"sid":hauk_session, "lat":lavg[0],"lon":lavg[1], "time":0.0, "pwd":""} - print(form_data) - with requests.post("{hauk_endpoint}/api/post.php", data=form_data) as r: - print('post.php', r.text) - -import math - -def haversine(coord1, coord2): - """ - Calculate the great-circle distance between two points on the Earth - specified in decimal degrees (latitude and longitude). - - Parameters: - coord1 (tuple): (latitude, longitude) for point 1 - coord2 (tuple): (latitude, longitude) for point 2 - - Returns: - float: distance in kilometers between the two points - """ - - # Convert latitude and longitude from degrees to radians - lat1, lon1 = map(math.radians, coord1) - lat2, lon2 = map(math.radians, coord2) - - # Haversine formula - dlon = lon2 - lon1 - dlat = lat2 - lat1 - a = math.sin(dlat / 2)**2 + math.cos(lat1) * math.cos(lat2) * math.sin(dlon / 2)**2 - c = 2 * math.asin(math.sqrt(a)) - - # Radius of Earth in kilometers (mean radius) - r = 6371.0 - - return c * r - -def update_display(): - display.fill(0) - display.text("freeside minimap!", 0, 0, 1) - l = {} - with requests.get(f"{hauk_endpoint}/api/fetch.php?id={hauk_slug}&since=0") as r: - #print('fetch.php', r.text) - l = r.json() - - print(l) - - d = location_avg() - for (name,val), i in zip(l['points'].items(), range(len(l['points']))): - if len(val) > 0: - distance = haversine(d, (val[0][0],val[0][1])) - else: - distance = float('inf') - print(name, "is", distance) - display.text(f"{name}: {distance}", 0, 10*(i+1), 1) - display.show() - -display.fill(0) -display.text("freeside minimap!", 0, 0, 1) -display.show() - -def code_update(): - - -while True: - try: - update_location() - update_display() - except Exception as e: - raise e - +import minimap +minimap.main() diff --git a/minimap.py b/minimap.py new file mode 100644 index 0000000..00657e0 --- /dev/null +++ b/minimap.py @@ -0,0 +1,141 @@ +print("\nchaos minimap!") +import os +import wifi +import adafruit_connection_manager +import adafruit_requests +import json +import time +import board +import busio +import displayio +import terminalio +from adafruit_display_text import label + +# Import the SSD1306 module. +import adafruit_ssd1306 + +from secrets import hauk_endpoint,hauk_session,hauk_slug,hauk_name + +# Create the I2C interface. +i2c = busio.I2C(board.D7, board.D8) + +# Create the SSD1306 OLED class. +# The first two parameters are the pixel width and pixel height. Change these +# to the right size for your display! +display = adafruit_ssd1306.SSD1306_I2C(128, 64, i2c) + +pool = adafruit_connection_manager.get_radio_socketpool(wifi.radio) +ssl_context = adafruit_connection_manager.get_radio_ssl_context(wifi.radio) +requests = adafruit_requests.Session(pool, ssl_context) + +locavg = [] +def location_avg(): + global locavg + if len(locavg) > 10: + locavg = locavg[1:] + lavg = list(zip(*locavg)) + print(lavg[0], lavg[1]) + lavg = (sum(lavg[0])/len(lavg[0]), sum(lavg[1])/len(lavg[1])) + return lavg + +def update_location(): + global locavg + print('scanning',) + wifi.radio.stop_scanning_networks() + time.sleep(2) + ns = wifi.radio.start_scanning_networks() + wifi_peers = [] + for n in ns: + #print(n.ssid, n.bssid.hex(':'), n.rssi, n.channel) + wifi_peers.append({"bssid": n.bssid.hex(':'), "ssid": n.ssid, "rssi": n.rssi}) + + #print(wifi_peers) + wifi.radio.stop_scanning_networks() + print('loc request') + + data = json.dumps(wifi_peers) + print(data) + lat,lon=0,0 + with requests.post(f"{hauk_endpoint}/location", data=data) as response: + #print(response.text) + j = response.json() + if 'triangulation' in j: + print(j['triangulation']) + lat = j['triangulation']['Position']['Lat'] + lon = j['triangulation']['Position']['Lon'] + else: + print('no location received') + + locavg.append((lat,lon)) + lavg = location_avg() + print(lavg) + form_data={"sid":hauk_session, "lat":lavg[0],"lon":lavg[1], "time":0.0, "pwd":""} + print(form_data) + with requests.post("{hauk_endpoint}/api/post.php", data=form_data) as r: + print('post.php', r.text) + +import math + +def haversine(coord1, coord2): + """ + Calculate the great-circle distance between two points on the Earth + specified in decimal degrees (latitude and longitude). + + Parameters: + coord1 (tuple): (latitude, longitude) for point 1 + coord2 (tuple): (latitude, longitude) for point 2 + + Returns: + float: distance in kilometers between the two points + """ + + # Convert latitude and longitude from degrees to radians + lat1, lon1 = map(math.radians, coord1) + lat2, lon2 = map(math.radians, coord2) + + # Haversine formula + dlon = lon2 - lon1 + dlat = lat2 - lat1 + a = math.sin(dlat / 2)**2 + math.cos(lat1) * math.cos(lat2) * math.sin(dlon / 2)**2 + c = 2 * math.asin(math.sqrt(a)) + + # Radius of Earth in kilometers (mean radius) + r = 6371.0 + + return c * r + + +def update_display(): + display.fill(0) + display.text("freeside minimap!", 0, 0, 1) + l = {} + with requests.get(f"{hauk_endpoint}/api/fetch.php?id={hauk_slug}&since=0") as r: + #print('fetch.php', r.text) + l = r.json() + + print(l) + + d = location_avg() + for (name,val), i in zip(l['points'].items(), range(len(l['points']))): + if len(val) > 0: + distance = haversine(d, (val[0][0],val[0][1])) + else: + distance = float('inf') + print(name, "is", distance) + display.text(f"{name}: {distance}", 0, 10*(i+1), 1) + display.show() + + +def main(): + display.fill(0) + display.text("freeside minimap!", 0, 0, 1) + display.show() + + while True: + try: + update_location() + update_display() + except Exception as e: + import traceback + traceback.print_exception(e) + diff --git a/update.py b/update.py new file mode 100644 index 0000000..eea9cfe --- /dev/null +++ b/update.py @@ -0,0 +1,26 @@ +import io +import storage +import wifi +import adafruit_connection_manager +import adafruit_requests + +update_url = "https://gitea.departmentofinter.net/cqc/freeside-minimap/raw/branch/memes/minimap.py" +update_file = 'minimap.py' +pool = adafruit_connection_manager.get_radio_socketpool(wifi.radio) +ssl_context = adafruit_connection_manager.get_radio_ssl_context(wifi.radio) +requests = adafruit_requests.Session(pool, ssl_context) + + +def run_update(): + with io.open(update_file, mode='w') as f: + with requests.get(update_url) as r: + print('\nupdating',update_file) + f.write(r.text) + + +def main(): + try: + run_update() + except Exception as e: + import traceback + traceback.print_exception(e)