summaryrefslogtreecommitdiffstats
path: root/baresp.py
blob: 30d50013654ab2f0cb48c5508b77583d663b2773 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
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')

    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