Explorar el Código

Raise exceptions from _send_command

Now the ISPError is raised from the _send_command method, not each
caller.  There will be a few cases where I need that to not be an
exception, but I can just wrap it in a try block then, so there's no
problem.
Clara Hobbs hace 6 años
padre
commit
3e7b54daae
Se han modificado 1 ficheros con 10 adiciones y 22 borrados
  1. 10
    22
      alpaca_isp/__init__.py

+ 10
- 22
alpaca_isp/__init__.py Ver fichero

76
         rr = self._send_command_raw(cmd)
76
         rr = self._send_command_raw(cmd)
77
         lr = [int(n) for n in rr.split()]
77
         lr = [int(n) for n in rr.split()]
78
         lr[0] = ReturnCode(lr[0])
78
         lr[0] = ReturnCode(lr[0])
79
+        if lr[0] != ReturnCode.CMD_SUCCESS:
80
+            raise ISPError(lr)
79
         return lr
81
         return lr
80
 
82
 
81
     def enter_isp(self, delay=0.01):
83
     def enter_isp(self, delay=0.01):
127
 
129
 
128
     def unlock(self, code="23130"):
130
     def unlock(self, code="23130"):
129
         """Unlock the flash write, erase, and go commands"""
131
         """Unlock the flash write, erase, and go commands"""
130
-        r = self._send_command("U {}\r\n".format(code).encode("utf-8"))
131
-        if r[0] != ReturnCode.CMD_SUCCESS:
132
-            raise ISPError(r)
132
+        self._send_command("U {}\r\n".format(code).encode("utf-8"))
133
 
133
 
134
     @property
134
     @property
135
     def baudrate(self):
135
     def baudrate(self):
138
 
138
 
139
     @baudrate.setter
139
     @baudrate.setter
140
     def baudrate(self, br):
140
     def baudrate(self, br):
141
-        r = self._send_command("B {} {}\r\n".format(br,
141
+        self._send_command("B {} {}\r\n".format(br,
142
             self._uart.stopbits).encode("utf-8"))
142
             self._uart.stopbits).encode("utf-8"))
143
         # Update the baud rate for our UART
143
         # Update the baud rate for our UART
144
-        if r[0] != ReturnCode.CMD_SUCCESS:
145
-            raise ISPError(r)
146
         self._uart.baudrate = br
144
         self._uart.baudrate = br
147
 
145
 
148
     @property
146
     @property
152
 
150
 
153
     @stopbits.setter
151
     @stopbits.setter
154
     def stopbits(self, sb):
152
     def stopbits(self, sb):
155
-        r = self._send_command("B {} {}\r\n".format(self._uart.baudrate,
153
+        self._send_command("B {} {}\r\n".format(self._uart.baudrate,
156
             sb).encode("utf-8"))
154
             sb).encode("utf-8"))
157
         # Update the number of stop bits for our UART
155
         # Update the number of stop bits for our UART
158
-        if r[0] != ReturnCode.CMD_SUCCESS:
159
-            raise ISPError(r)
160
         self._uart.stopbits = sb
156
         self._uart.stopbits = sb
161
 
157
 
162
     @property
158
     @property
167
     @echo.setter
163
     @echo.setter
168
     def echo(self, setting):
164
     def echo(self, setting):
169
         setting = bool(setting)
165
         setting = bool(setting)
170
-        r = self._send_command("A {}\r\n".format(int(setting)).encode("utf-8"))
171
-        if r[0] != ReturnCode.CMD_SUCCESS:
172
-            raise ISPError(r)
166
+        self._send_command("A {}\r\n".format(int(setting)).encode("utf-8"))
173
         self._echo = setting
167
         self._echo = setting
174
 
168
 
175
     def write_ram(self, start, data, count=None):
169
     def write_ram(self, start, data, count=None):
182
         if count is None:
176
         if count is None:
183
             count = len(data)
177
             count = len(data)
184
         # Ask to write data
178
         # Ask to write data
185
-        r = self._send_command("W {} {}\r\n".format(start, count).encode(
186
-            "utf-8"))
187
-        if r[0] != ReturnCode.CMD_SUCCESS:
188
-            raise ISPError(r)
189
-        # If the MCU is okay with what we intend to do, send the data
179
+        self._send_command("W {} {}\r\n".format(start, count).encode("utf-8"))
180
+        # Send the data
190
         # NOTE: this is right for LPC8xx chips, not others
181
         # NOTE: this is right for LPC8xx chips, not others
191
-        ok = self._writeline(data[:count], plain=True)
182
+        self._writeline(data[:count], plain=True)
192
         return
183
         return
193
 
184
 
194
     def read_memory(self, start, count):
185
     def read_memory(self, start, count):
196
 
187
 
197
         Start and count must be multiples of four.
188
         Start and count must be multiples of four.
198
         """
189
         """
199
-        r = self._send_command("R {} {}\r\n".format(start, count).encode(
200
-            "utf-8"))
201
-        if r[0] != ReturnCode.CMD_SUCCESS:
202
-            raise ISPError(r)
190
+        self._send_command("R {} {}\r\n".format(start, count).encode("utf-8"))
203
         return self._uart.read(count)
191
         return self._uart.read(count)
204
 
192
 
205
 
193
 

Loading…
Cancelar
Guardar