summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFlorian Baumann <derflob@derflob.de>2017-04-28 23:11:02 +0200
committerFlorian Baumann <derflob@derflob.de>2017-10-04 20:41:43 +0200
commit0b102009affeda0d7f8d045571f11f906260e8c6 (patch)
tree56ac9575a8d10e2dbff8bda1b6455956090c78ca
downloadBarESP-0b102009affeda0d7f8d045571f11f906260e8c6.tar.gz
BarESP-0b102009affeda0d7f8d045571f11f906260e8c6.tar.bz2
Initial Prototype
-rw-r--r--adxl.py123
-rw-r--r--baresp.py213
-rw-r--r--boot.py7
-rw-r--r--main.py4
4 files changed, 347 insertions, 0 deletions
diff --git a/adxl.py b/adxl.py
new file mode 100644
index 0000000..c5e5f61
--- /dev/null
+++ b/adxl.py
@@ -0,0 +1,123 @@
+import machine
+
+class ADXL():
+ ADXL_ADDR = 0x53
+
+ def __init__(self, SCL, SDA):
+ self.SCL = SCL
+ self.SDA = SDA
+
+ self._i2c = machine.I2C(scl=self.SCL, sda=self.SDA, freq=100000)
+
+ self._last_x = 0.0
+ self._last_y = 0.0
+ self._last_z = 0.0
+ self._last_act_x = 0.0
+ self._last_act_y = 0.0
+ self._last_act_z = 0.0
+
+ self._timer = machine.Timer(-1)
+ self._timer.init(period=100, mode=machine.Timer.PERIODIC, callback=self._timer_cb)
+
+ self._is_active = True
+
+ self._timer_cb(None)
+
+ def setup(self):
+ r = self._i2c.readfrom_mem(self.ADXL_ADDR, 0x2D, 1)
+
+ if (not bool(r[0] & (1 << 3)) or True):
+ # OFFSET_{X,Y,Z}
+ #self._i2c.writeto_mem(self.ADXL_ADDR, 0x1E, bytes([25, 76, 183]))
+ self._i2c.writeto_mem(self.ADXL_ADDR, 0x1E, bytes([25, 75, 131]))
+
+ # BW_RATE not LOW_POWER datarate 12.5Hz 0111
+ self._i2c.writeto_mem(self.ADXL_ADDR, 0x2C, (7).to_bytes(1, 'little'))
+ # DATA_FORMAT: FULL_RES 8, 8g 2
+ self._i2c.writeto_mem(self.ADXL_ADDR, 0x31, (8 + 2).to_bytes(1, 'little'))
+
+ # THRESH_ACT 4 * 62.5 mg = 0.25g
+ # THRESH_INACT 2 * 62.5 mg = 0.125g
+ # TIME_INACT 5s
+ # ACT_INACT_CTL INACT ac-coupled, all axis enabled
+ self._i2c.writeto_mem(self.ADXL_ADDR, 0x24, bytes([10, 2, 5, 255]))
+
+ # INT_MAP Activity to INT everything else INT2
+ self._i2c.writeto_mem(self.ADXL_ADDR, 0x2F, (239).to_bytes(1, 'little'))
+
+ # INT_ENABLE Activity, Inactivity
+ # self._i3c.writeto_mem(self.ADXL_ADDR, 0x2E, (16 + 8).to_bytes(1, 'little'))
+ self.inactivity_intr(True)
+
+ # FIFO_CTL bypass FIFO
+ self._i2c.writeto_mem(self.ADXL_ADDR, 0x38, (0).to_bytes(1, 'little'))
+
+ # PWR_CTL Link mode 32, auto_sleep 16, measure 8 enable
+ self._i2c.writeto_mem(self.ADXL_ADDR, 0x2D, (32 + 16 + 8).to_bytes(1, 'little'))
+
+ def __decode_data(self, data):
+ # micropython doesn't implement signed param and assumes signed=False
+ d = int.from_bytes(data[0:2], 'little')
+ if (d > 32767):
+ d = -(65535 - d + 1)
+
+ return d / 250
+
+ def read_data(self):
+ axis = self._i2c.readfrom_mem(self.ADXL_ADDR, 0x2B, 1)
+
+ d = self._i2c.readfrom_mem(self.ADXL_ADDR, 0x32, 6)
+ x = self.__decode_data(d[0:2])
+ y = self.__decode_data(d[2:4])
+ z = self.__decode_data(d[4:6])
+
+ return (x, y, z)
+
+ def _timer_cb(self, t):
+ int_src = self._i2c.readfrom_mem(self.ADXL_ADDR, 0x30, 1)
+ #print(int_src)
+ #print(self.read_data())
+
+ if (bool(int_src[0] & (1 << 4))):
+ self._is_active = True
+ (self._last_act_x, self._last_act_y, self._last_act_z) = self.read_data()
+
+ if (bool(int_src[0] & (1 << 3))):
+ self._is_active = False
+
+ if (bool(int_src[0] & (1 << 7))):
+ (self._last_x, self._last_y, self._last_z) = self.read_data()
+ if (self._is_active):
+ pass
+
+ def is_active(self):
+ return self._is_active
+
+ def last_x(self):
+ return self._last_x
+
+ def last_y(self):
+ return self._last_y
+
+ def last_z(self):
+ return self._last_z
+
+ def activity_intr(self, enabled):
+ int_en = self._i2c.readfrom_mem(self.ADXL_ADDR, 0x2E, 1)
+
+ if (enabled):
+ int_en = int_en[0] | (1 << 4)
+ else:
+ int_en = int_en[0] & ~(1 << 4)
+
+ self._i2c.writeto_mem(self.ADXL_ADDR, 0x2E, int_en.to_bytes(1, 'little'))
+
+ def inactivity_intr(self, enabled):
+ int_en = self._i2c.readfrom_mem(self.ADXL_ADDR, 0x2E, 1)
+
+ if (enabled):
+ int_en = int_en[0] | (1 << 3)
+ else:
+ int_en = int_en[0] & ~(1 << 3)
+
+ self._i2c.writeto_mem(self.ADXL_ADDR, 0x2E, int_en.to_bytes(1, 'little'))
diff --git a/baresp.py b/baresp.py
new file mode 100644
index 0000000..698e170
--- /dev/null
+++ b/baresp.py
@@ -0,0 +1,213 @@
+import esp
+import machine
+import utime
+import micropython
+import network
+import usocket
+import adxl
+from settings import ssid, password
+
+FLASH_PIN = machine.Pin(0, machine.Pin.IN)
+BCEN_PIN = machine.Pin(13, machine.Pin.OUT)
+SDA_PIN = machine.Pin(14)
+SCL_PIN = machine.Pin(12)
+
+class BarState():
+ OFF = 1
+
+ WAIT_FOR_BARCODE = 2
+ READING_BARCODE = 3
+
+ def __init__(self):
+ super(BarState, self).__init__()
+
+ self.state = self.OFF
+
+
+class ESPState():
+ WAIT_FOR_ACTIVITY = 1
+
+ CONNECTING = 2
+
+ IDLE_ON = 3
+ PENDING_DATA = 4
+
+ def __init__(self):
+ super(ESPState, self).__init__()
+
+ self.state = self.WAIT_FOR_ACTIVITY
+
+
+class Logger():
+ def __init__(self, *args, **kwargs):
+ super(Logger, self).__init__(*args, **kwargs)
+
+ #self._log = usocket.socket(usocket.AF_INET, usocket.SOCK_DGRAM)
+ #self._log.connect(('192.168.19.2', 13578))
+
+ def debug(self, log):
+ print(log)
+ pass
+
+ def info(self, log):
+ print(log)
+ pass
+
+
+class BarESP():
+ bar = BarState()
+ esp = ESPState()
+
+ adxl = adxl.ADXL(SCL_PIN, SDA_PIN)
+ #adxl_timer = machine.Timer(-1)
+
+ wlan = network.WLAN(network.STA_IF)
+
+ uart = machine.UART(0, 115200)
+
+ def __init__(self, *args, **kwargs):
+ super(BarESP, self).__init__()
+
+ self._cache = []
+
+ self._log = Logger()
+
+ self.wlan.active(False)
+
+ if (not machine.reset_cause() == machine.DEEPSLEEP_RESET):
+ self._log.info('non DS reset')
+ self.adxl.setup()
+ else:
+ self._log.info('DS Reset')
+ self.adxl.setup()
+
+ self.adxl.activity_intr(False)
+ self.adxl.inactivity_intr(True)
+
+ def run(self):
+ micropython.kbd_intr(-1)
+ while (FLASH_PIN.value()):
+ self.run_esp()
+ self.run_bar()
+ micropython.kbd_intr(0x03)
+
+ def run_esp(self):
+ if (self.esp.state == ESPState.WAIT_FOR_ACTIVITY):
+
+ if (self.adxl.is_active()):
+ BCEN_PIN.on()
+ self.bar.state = BarState.WAIT_FOR_BARCODE
+
+ self.connect()
+ self.esp.state = ESPState.IDLE_ON
+
+ else:
+ self._log.info('Going to sleep - forever!')
+ self.deepsleep(0)
+
+ elif (self.esp.state == ESPState.IDLE_ON):
+ #self._log.debug(b'IDLE_ON\n')
+
+ if (not self.adxl.is_active()):
+ if (self.bar.state == BarState.WAIT_FOR_BARCODE):
+ BCEN_PIN.off()
+ self.bar.state = BarState.OFF
+ self._log.debug(b'->BAR_OFF\n')
+
+ if (len(self._cache) == 0):
+ if (not self.bar.state == BarState.READING_BARCODE):
+ self.esp.state = ESPState.WAIT_FOR_ACTIVITY
+ self._log.debug(b'->WFA\n')
+
+ if (len(self._cache) > 0):
+ self.esp.state = ESPState.PENDING_DATA
+ self._log.debug(b'->PDb\n')
+
+ elif (self.esp.state == ESPState.PENDING_DATA):
+ if (len(self._cache) == 0):
+ self.esp.state = ESPState.IDLE_ON
+ self.ack_data_sent()
+ self._log.debug(b'->IO\n')
+
+ else:
+ if (not (self.wlan.active() and self.wlan.isconnected())):
+ self._log.debug(b'PD.connect\n')
+ self.connect()
+ else:
+ while (len(self._cache) > 0):
+ bc = self._cache.pop(0)
+ self._log.debug(b'PD.pop')
+ self._log.debug(bc)
+ self._log.debug(b'\n')
+
+ try:
+ s = usocket.socket()
+ s.connect(('192.168.19.1', 9845))
+ s.sendall(bc.encode('ascii'))
+ s.close()
+ utime.sleep_ms(100)
+ except Exception as e:
+ self._log.debug(b'Sending bc failed.\n')
+ self._log.debug(e)
+ self._cache.append(bc)
+ break
+
+
+ if (len(self._cache) > 0):
+ self.esp.state = ESPState.PENDING_DATA
+ else:
+ self.esp.state = ESPState.IDLE_ON
+ self.ack_data_sent()
+
+ def run_bar(self):
+ if (self.bar.state == BarState.WAIT_FOR_BARCODE):
+ bc = self.try_barcode()
+
+ if (bc):
+ self._cache.append(bc)
+
+ def connect(self):
+ if (not self.wlan.active()):
+ self._log.debug(b'Activate wlan')
+ self.wlan.active(True)
+
+ if (not self.wlan.isconnected() and not self.wlan.status() == network.STAT_CONNECTING):
+ self._log.debug(b'connecting wlan')
+ self.wlan.connect(ssid, password)
+
+ def deepsleep(self, msec):
+ self.adxl.activity_intr(True)
+ machine.deepsleep()
+
+ def ack_data_sent(self):
+ self.uart.write(b'$?GGG\r')
+ utime.sleep_ms(50)
+ self.uart.write(b'$?GGG\r')
+
+ def try_barcode(self):
+ code = ""
+ while True:
+ b = self.uart.read(1)
+ if (b is None):
+ # if buffer empty and not currently reading barcode
+ # return without barcode
+ if (not self.bar.state == BarState.READING_BARCODE):
+ return None
+ else:
+ self._log.info('read {} - {}'.format(b, code))
+ if (self.bar.state == BarState.WAIT_FOR_BARCODE):
+ if (b == b'\x02'):
+ code = ""
+ self.bar.state = BarState.READING_BARCODE
+
+ else:
+ if (b == b'\x03'):
+ self.bar.state = BarState.WAIT_FOR_BARCODE
+ return code
+ else:
+ code = code + b.decode('ascii')
+
+ return None
+#uart = machine.UART(0, 115200)
+#uart.write(b'$+AFA1AAA1EEA0103EEB1EDA0102EDB1BCB0BLA06$-\r')
+>>>>>>> bdd37c4... stuff to make it actually work
diff --git a/boot.py b/boot.py
new file mode 100644
index 0000000..a5cf2d9
--- /dev/null
+++ b/boot.py
@@ -0,0 +1,7 @@
+# This file is executed on every boot (including wake-boot from deepsleep)
+import esp
+esp.osdebug(None)
+import gc
+#import webrepl
+#webrepl.start()
+gc.collect()
diff --git a/main.py b/main.py
new file mode 100644
index 0000000..c9fd757
--- /dev/null
+++ b/main.py
@@ -0,0 +1,4 @@
+from baresp import BarESP
+
+b = BarESP()
+b.run()