From 69661f3343bedf86e514337cff63d96cc42f8859 Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 27 Feb 2020 15:36:53 +1100 Subject: all: Reformat C and Python source code with tools/codeformat.py. This is run with uncrustify 0.70.1, and black 19.10b0. --- examples/SDdatalogger/boot.py | 20 +++---- examples/SDdatalogger/datalogger.py | 18 +++--- examples/accel_i2c.py | 4 +- examples/accellog.py | 18 +++--- examples/asmled.py | 14 +++-- examples/asmsum.py | 6 +- examples/bluetooth/ble_advertising.py | 25 +++++--- examples/bluetooth/ble_temperature.py | 26 ++++++--- examples/bluetooth/ble_temperature_central.py | 68 +++++++++++++--------- examples/bluetooth/ble_uart_peripheral.py | 37 ++++++++---- examples/bluetooth/ble_uart_repl.py | 8 ++- examples/conwaylife.py | 45 +++++++------- examples/hwapi/button_reaction.py | 8 ++- examples/hwapi/hwconfig_console.py | 1 - examples/hwapi/hwconfig_pyboard.py | 8 +-- examples/ledangle.py | 1 + examples/mandel.py | 7 ++- examples/micropython.py | 2 + examples/natmod/features2/test.py | 7 ++- examples/network/http_server.py | 1 + examples/network/http_server_simplistic.py | 1 + .../network/http_server_simplistic_commented.py | 1 + examples/network/http_server_ssl.py | 50 ++++++++-------- examples/pins.py | 36 ++++++------ examples/pyb.py | 11 +++- examples/switch.py | 8 ++- examples/unix/ffi_example.py | 2 + examples/unix/machine_bios.py | 2 +- 28 files changed, 258 insertions(+), 177 deletions(-) (limited to 'examples') diff --git a/examples/SDdatalogger/boot.py b/examples/SDdatalogger/boot.py index 4ac94bbaa..bc42cef2e 100644 --- a/examples/SDdatalogger/boot.py +++ b/examples/SDdatalogger/boot.py @@ -8,18 +8,18 @@ import pyb -pyb.LED(3).on() # indicate we are waiting for switch press -pyb.delay(2000) # wait for user to maybe press the switch -switch_value = pyb.Switch()() # sample the switch at end of delay -pyb.LED(3).off() # indicate that we finished waiting for the switch +pyb.LED(3).on() # indicate we are waiting for switch press +pyb.delay(2000) # wait for user to maybe press the switch +switch_value = pyb.Switch()() # sample the switch at end of delay +pyb.LED(3).off() # indicate that we finished waiting for the switch -pyb.LED(4).on() # indicate that we are selecting the mode +pyb.LED(4).on() # indicate that we are selecting the mode if switch_value: - pyb.usb_mode('VCP+MSC') - pyb.main('cardreader.py') # if switch was pressed, run this + pyb.usb_mode("VCP+MSC") + pyb.main("cardreader.py") # if switch was pressed, run this else: - pyb.usb_mode('VCP+HID') - pyb.main('datalogger.py') # if switch wasn't pressed, run this + pyb.usb_mode("VCP+HID") + pyb.main("datalogger.py") # if switch wasn't pressed, run this -pyb.LED(4).off() # indicate that we finished selecting the mode +pyb.LED(4).off() # indicate that we finished selecting the mode diff --git a/examples/SDdatalogger/datalogger.py b/examples/SDdatalogger/datalogger.py index 0690c20bb..3d6f98b38 100644 --- a/examples/SDdatalogger/datalogger.py +++ b/examples/SDdatalogger/datalogger.py @@ -17,17 +17,17 @@ while True: # start if switch is pressed if switch(): - pyb.delay(200) # delay avoids detection of multiple presses - blue.on() # blue LED indicates file open - log = open('/sd/log.csv', 'w') # open file on SD (SD: '/sd/', flash: '/flash/) + pyb.delay(200) # delay avoids detection of multiple presses + blue.on() # blue LED indicates file open + log = open("/sd/log.csv", "w") # open file on SD (SD: '/sd/', flash: '/flash/) # until switch is pressed again while not switch(): - t = pyb.millis() # get time - x, y, z = accel.filtered_xyz() # get acceleration data - log.write('{},{},{},{}\n'.format(t,x,y,z)) # write data to file + t = pyb.millis() # get time + x, y, z = accel.filtered_xyz() # get acceleration data + log.write("{},{},{},{}\n".format(t, x, y, z)) # write data to file # end after switch is pressed again - log.close() # close file - blue.off() # blue LED indicates file closed - pyb.delay(200) # delay avoids detection of multiple presses + log.close() # close file + blue.off() # blue LED indicates file closed + pyb.delay(200) # delay avoids detection of multiple presses diff --git a/examples/accel_i2c.py b/examples/accel_i2c.py index d635e3ccc..7229c2289 100644 --- a/examples/accel_i2c.py +++ b/examples/accel_i2c.py @@ -17,10 +17,10 @@ accel_pwr.value(1) i2c = I2C(1, baudrate=100000) addrs = i2c.scan() print("Scanning devices:", [hex(x) for x in addrs]) -if 0x4c not in addrs: +if 0x4C not in addrs: print("Accelerometer is not detected") -ACCEL_ADDR = 0x4c +ACCEL_ADDR = 0x4C ACCEL_AXIS_X_REG = 0 ACCEL_MODE_REG = 7 diff --git a/examples/accellog.py b/examples/accellog.py index a2fbe1437..e8b5d101d 100644 --- a/examples/accellog.py +++ b/examples/accellog.py @@ -2,19 +2,19 @@ import pyb -accel = pyb.Accel() # create object of accelerometer -blue = pyb.LED(4) # create object of blue LED +accel = pyb.Accel() # create object of accelerometer +blue = pyb.LED(4) # create object of blue LED # open file to write data - /sd/ is the SD-card, /flash/ the internal memory -log = open('/sd/log.csv', 'w') +log = open("/sd/log.csv", "w") -blue.on() # turn on blue LED +blue.on() # turn on blue LED # do 100 times (if the board is connected via USB, you can't write longer because the PC tries to open the filesystem which messes up your file.) for i in range(100): - t = pyb.millis() # get time since reset - x, y, z = accel.filtered_xyz() # get acceleration data - log.write('{},{},{},{}\n'.format(t,x,y,z)) # write data to file + t = pyb.millis() # get time since reset + x, y, z = accel.filtered_xyz() # get acceleration data + log.write("{},{},{},{}\n".format(t, x, y, z)) # write data to file -log.close() # close file -blue.off() # turn off LED +log.close() # close file +blue.off() # turn off LED diff --git a/examples/asmled.py b/examples/asmled.py index 917d9ba03..09a06c184 100644 --- a/examples/asmled.py +++ b/examples/asmled.py @@ -2,8 +2,8 @@ # this version is overly verbose and uses word stores @micropython.asm_thumb def flash_led(r0): - movw(r1, (stm.GPIOA + stm.GPIO_BSRRL) & 0xffff) - movt(r1, ((stm.GPIOA + stm.GPIO_BSRRL) >> 16) & 0x7fff) + movw(r1, (stm.GPIOA + stm.GPIO_BSRRL) & 0xFFFF) + movt(r1, ((stm.GPIOA + stm.GPIO_BSRRL) >> 16) & 0x7FFF) movw(r2, 1 << 13) movt(r2, 0) movw(r3, 0) @@ -17,8 +17,8 @@ def flash_led(r0): str(r2, [r1, 0]) # delay for a bit - movw(r4, 5599900 & 0xffff) - movt(r4, (5599900 >> 16) & 0xffff) + movw(r4, 5599900 & 0xFFFF) + movt(r4, (5599900 >> 16) & 0xFFFF) label(delay_on) sub(r4, r4, 1) cmp(r4, 0) @@ -28,8 +28,8 @@ def flash_led(r0): str(r3, [r1, 0]) # delay for a bit - movw(r4, 5599900 & 0xffff) - movt(r4, (5599900 >> 16) & 0xffff) + movw(r4, 5599900 & 0xFFFF) + movt(r4, (5599900 >> 16) & 0xFFFF) label(delay_off) sub(r4, r4, 1) cmp(r4, 0) @@ -41,6 +41,7 @@ def flash_led(r0): cmp(r0, 0) bgt(loop1) + # flash LED #2 using inline assembler # this version uses half-word sortes, and the convenience assembler operation 'movwt' @micropython.asm_thumb @@ -81,5 +82,6 @@ def flash_led_v2(r0): cmp(r0, 0) bgt(loop1) + flash_led(5) flash_led_v2(5) diff --git a/examples/asmsum.py b/examples/asmsum.py index 07e71c738..f465b25af 100644 --- a/examples/asmsum.py +++ b/examples/asmsum.py @@ -22,6 +22,7 @@ def asm_sum_words(r0, r1): mov(r0, r2) + @micropython.asm_thumb def asm_sum_bytes(r0, r1): @@ -46,12 +47,13 @@ def asm_sum_bytes(r0, r1): mov(r0, r2) + import array -b = array.array('l', (100, 200, 300, 400)) +b = array.array("l", (100, 200, 300, 400)) n = asm_sum_words(len(b), b) print(b, n) -b = array.array('b', (10, 20, 30, 40, 50, 60, 70, 80)) +b = array.array("b", (10, 20, 30, 40, 50, 60, 70, 80)) n = asm_sum_bytes(len(b), b) print(b, n) diff --git a/examples/bluetooth/ble_advertising.py b/examples/bluetooth/ble_advertising.py index 3a06beb74..3fb1281f6 100644 --- a/examples/bluetooth/ble_advertising.py +++ b/examples/bluetooth/ble_advertising.py @@ -26,9 +26,12 @@ def advertising_payload(limited_disc=False, br_edr=False, name=None, services=No def _append(adv_type, value): nonlocal payload - payload += struct.pack('BB', len(value) + 1, adv_type) + value + payload += struct.pack("BB", len(value) + 1, adv_type) + value - _append(_ADV_TYPE_FLAGS, struct.pack('B', (0x01 if limited_disc else 0x02) + (0x00 if br_edr else 0x04))) + _append( + _ADV_TYPE_FLAGS, + struct.pack("B", (0x01 if limited_disc else 0x02) + (0x00 if br_edr else 0x04)), + ) if name: _append(_ADV_TYPE_NAME, name) @@ -44,7 +47,7 @@ def advertising_payload(limited_disc=False, br_edr=False, name=None, services=No _append(_ADV_TYPE_UUID128_COMPLETE, b) # See org.bluetooth.characteristic.gap.appearance.xml - _append(_ADV_TYPE_APPEARANCE, struct.pack(' 60: return False return True @@ -21,7 +22,9 @@ def mandelbrot(): lcd.set(u, v) lcd.show() + # PC testing import lcd + lcd = lcd.LCD(128, 32) mandelbrot() diff --git a/examples/micropython.py b/examples/micropython.py index f91da94f4..5b87ba192 100644 --- a/examples/micropython.py +++ b/examples/micropython.py @@ -2,7 +2,9 @@ # Dummy function decorators + def nodecor(x): return x + bytecode = native = viper = nodecor diff --git a/examples/natmod/features2/test.py b/examples/natmod/features2/test.py index 2e9db00f4..5ac80120d 100644 --- a/examples/natmod/features2/test.py +++ b/examples/natmod/features2/test.py @@ -2,20 +2,22 @@ import array + def isclose(a, b): return abs(a - b) < 1e-3 + def test(): tests = [ isclose(add(0.1, 0.2), 0.3), isclose(add_f(0.1, 0.2), 0.3), ] - ar = array.array('f', [1, 2, 3.5]) + ar = array.array("f", [1, 2, 3.5]) productf(ar) tests.append(isclose(ar[0], 7)) - if 'add_d' in globals(): + if "add_d" in globals(): tests.append(isclose(add_d(0.1, 0.2), 0.3)) print(tests) @@ -23,4 +25,5 @@ def test(): if not all(tests): raise SystemExit(1) + test() diff --git a/examples/network/http_server.py b/examples/network/http_server.py index e3a66e828..76be3ab81 100644 --- a/examples/network/http_server.py +++ b/examples/network/http_server.py @@ -10,6 +10,7 @@ HTTP/1.0 200 OK Hello #%d from MicroPython! """ + def main(micropython_optimize=False): s = socket.socket() diff --git a/examples/network/http_server_simplistic.py b/examples/network/http_server_simplistic.py index 67ecb1ad7..71949419e 100644 --- a/examples/network/http_server_simplistic.py +++ b/examples/network/http_server_simplistic.py @@ -12,6 +12,7 @@ HTTP/1.0 200 OK Hello #%d from MicroPython! """ + def main(): s = socket.socket() ai = socket.getaddrinfo("0.0.0.0", 8080) diff --git a/examples/network/http_server_simplistic_commented.py b/examples/network/http_server_simplistic_commented.py index b58e9eeb6..da042c6c8 100644 --- a/examples/network/http_server_simplistic_commented.py +++ b/examples/network/http_server_simplistic_commented.py @@ -20,6 +20,7 @@ HTTP/1.0 200 OK Hello #%d from MicroPython! """ + def main(): s = socket.socket() diff --git a/examples/network/http_server_ssl.py b/examples/network/http_server_ssl.py index 47d83bf24..1116c71e9 100644 --- a/examples/network/http_server_ssl.py +++ b/examples/network/http_server_ssl.py @@ -1,4 +1,5 @@ import ubinascii as binascii + try: import usocket as socket except: @@ -9,30 +10,32 @@ import ussl as ssl # This self-signed key/cert pair is randomly generated and to be used for # testing/demonstration only. You should always generate your own key/cert. key = binascii.unhexlify( - b'3082013b020100024100cc20643fd3d9c21a0acba4f48f61aadd675f52175a9dcf07fbef' - b'610a6a6ba14abb891745cd18a1d4c056580d8ff1a639460f867013c8391cdc9f2e573b0f' - b'872d0203010001024100bb17a54aeb3dd7ae4edec05e775ca9632cf02d29c2a089b563b0' - b'd05cdf95aeca507de674553f28b4eadaca82d5549a86058f9996b07768686a5b02cb240d' - b'd9f1022100f4a63f5549e817547dca97b5c658038e8593cb78c5aba3c4642cc4cd031d86' - b'8f022100d598d870ffe4a34df8de57047a50b97b71f4d23e323f527837c9edae88c79483' - b'02210098560c89a70385c36eb07fd7083235c4c1184e525d838aedf7128958bedfdbb102' - b'2051c0dab7057a8176ca966f3feb81123d4974a733df0f958525f547dfd1c271f9022044' - b'6c2cafad455a671a8cf398e642e1be3b18a3d3aec2e67a9478f83c964c4f1f') + b"3082013b020100024100cc20643fd3d9c21a0acba4f48f61aadd675f52175a9dcf07fbef" + b"610a6a6ba14abb891745cd18a1d4c056580d8ff1a639460f867013c8391cdc9f2e573b0f" + b"872d0203010001024100bb17a54aeb3dd7ae4edec05e775ca9632cf02d29c2a089b563b0" + b"d05cdf95aeca507de674553f28b4eadaca82d5549a86058f9996b07768686a5b02cb240d" + b"d9f1022100f4a63f5549e817547dca97b5c658038e8593cb78c5aba3c4642cc4cd031d86" + b"8f022100d598d870ffe4a34df8de57047a50b97b71f4d23e323f527837c9edae88c79483" + b"02210098560c89a70385c36eb07fd7083235c4c1184e525d838aedf7128958bedfdbb102" + b"2051c0dab7057a8176ca966f3feb81123d4974a733df0f958525f547dfd1c271f9022044" + b"6c2cafad455a671a8cf398e642e1be3b18a3d3aec2e67a9478f83c964c4f1f" +) cert = binascii.unhexlify( - b'308201d53082017f020203e8300d06092a864886f70d01010505003075310b3009060355' - b'0406130258583114301206035504080c0b54686550726f76696e63653110300e06035504' - b'070c075468654369747931133011060355040a0c0a436f6d70616e7958595a3113301106' - b'0355040b0c0a436f6d70616e7958595a3114301206035504030c0b546865486f73744e61' - b'6d65301e170d3139313231383033333935355a170d3239313231353033333935355a3075' - b'310b30090603550406130258583114301206035504080c0b54686550726f76696e636531' - b'10300e06035504070c075468654369747931133011060355040a0c0a436f6d70616e7958' - b'595a31133011060355040b0c0a436f6d70616e7958595a3114301206035504030c0b5468' - b'65486f73744e616d65305c300d06092a864886f70d0101010500034b003048024100cc20' - b'643fd3d9c21a0acba4f48f61aadd675f52175a9dcf07fbef610a6a6ba14abb891745cd18' - b'a1d4c056580d8ff1a639460f867013c8391cdc9f2e573b0f872d0203010001300d06092a' - b'864886f70d0101050500034100b0513fe2829e9ecbe55b6dd14c0ede7502bde5d46153c8' - b'e960ae3ebc247371b525caeb41bbcf34686015a44c50d226e66aef0a97a63874ca5944ef' - b'979b57f0b3') + b"308201d53082017f020203e8300d06092a864886f70d01010505003075310b3009060355" + b"0406130258583114301206035504080c0b54686550726f76696e63653110300e06035504" + b"070c075468654369747931133011060355040a0c0a436f6d70616e7958595a3113301106" + b"0355040b0c0a436f6d70616e7958595a3114301206035504030c0b546865486f73744e61" + b"6d65301e170d3139313231383033333935355a170d3239313231353033333935355a3075" + b"310b30090603550406130258583114301206035504080c0b54686550726f76696e636531" + b"10300e06035504070c075468654369747931133011060355040a0c0a436f6d70616e7958" + b"595a31133011060355040b0c0a436f6d70616e7958595a3114301206035504030c0b5468" + b"65486f73744e616d65305c300d06092a864886f70d0101010500034b003048024100cc20" + b"643fd3d9c21a0acba4f48f61aadd675f52175a9dcf07fbef610a6a6ba14abb891745cd18" + b"a1d4c056580d8ff1a639460f867013c8391cdc9f2e573b0f872d0203010001300d06092a" + b"864886f70d0101050500034100b0513fe2829e9ecbe55b6dd14c0ede7502bde5d46153c8" + b"e960ae3ebc247371b525caeb41bbcf34686015a44c50d226e66aef0a97a63874ca5944ef" + b"979b57f0b3" +) CONTENT = b"""\ @@ -41,6 +44,7 @@ HTTP/1.0 200 OK Hello #%d from MicroPython! """ + def main(use_stream=True): s = socket.socket() diff --git a/examples/pins.py b/examples/pins.py index aafdb4813..3a8472e8a 100644 --- a/examples/pins.py +++ b/examples/pins.py @@ -4,6 +4,7 @@ import pyb import pins_af + def af(): max_name_width = 0 max_af_width = 0 @@ -13,21 +14,22 @@ def af(): max_af_width = max(max_af_width, len(af_entry[1])) for pin_entry in pins_af.PINS_AF: pin_name = pin_entry[0] - print('%-*s ' % (max_name_width, pin_name), end='') + print("%-*s " % (max_name_width, pin_name), end="") for af_entry in pin_entry[1:]: - print('%2d: %-*s ' % (af_entry[0], max_af_width, af_entry[1]), end='') - print('') + print("%2d: %-*s " % (af_entry[0], max_af_width, af_entry[1]), end="") + print("") + def pins(): - mode_str = { pyb.Pin.IN : 'IN', - pyb.Pin.OUT_PP : 'OUT_PP', - pyb.Pin.OUT_OD : 'OUT_OD', - pyb.Pin.AF_PP : 'AF_PP', - pyb.Pin.AF_OD : 'AF_OD', - pyb.Pin.ANALOG : 'ANALOG' } - pull_str = { pyb.Pin.PULL_NONE : '', - pyb.Pin.PULL_UP : 'PULL_UP', - pyb.Pin.PULL_DOWN : 'PULL_DOWN' } + mode_str = { + pyb.Pin.IN: "IN", + pyb.Pin.OUT_PP: "OUT_PP", + pyb.Pin.OUT_OD: "OUT_OD", + pyb.Pin.AF_PP: "AF_PP", + pyb.Pin.AF_OD: "AF_OD", + pyb.Pin.ANALOG: "ANALOG", + } + pull_str = {pyb.Pin.PULL_NONE: "", pyb.Pin.PULL_UP: "PULL_UP", pyb.Pin.PULL_DOWN: "PULL_DOWN"} width = [0, 0, 0, 0] rows = [] for pin_entry in pins_af.PINS_AF: @@ -42,17 +44,17 @@ def pins(): pin_af = pin.af() for af_entry in pin_entry[1:]: if pin_af == af_entry[0]: - af_str = '%d: %s' % (pin_af, af_entry[1]) + af_str = "%d: %s" % (pin_af, af_entry[1]) break else: - af_str = '%d' % pin_af + af_str = "%d" % pin_af else: - af_str = '' + af_str = "" row.append(af_str) for col in range(len(width)): width[col] = max(width[col], len(row[col])) rows.append(row) for row in rows: for col in range(len(width)): - print('%-*s ' % (width[col], row[col]), end='') - print('') + print("%-*s " % (width[col], row[col]), end="") + print("") diff --git a/examples/pyb.py b/examples/pyb.py index b303777e5..67620e793 100644 --- a/examples/pyb.py +++ b/examples/pyb.py @@ -1,17 +1,22 @@ # pyboard testing functions for CPython import time + def delay(n): - #time.sleep(float(n) / 1000) + # time.sleep(float(n) / 1000) pass + rand_seed = 1 + + def rng(): global rand_seed # for these choice of numbers, see P L'Ecuyer, "Tables of linear congruential generators of different sizes and good lattice structure" rand_seed = (rand_seed * 653276) % 8388593 return rand_seed + # LCD testing object for PC # uses double buffering class LCD: @@ -30,12 +35,12 @@ class LCD: self.buf1[y][x] = self.buf2[y][x] = value def show(self): - print('') # blank line to separate frames + print("") # blank line to separate frames for y in range(self.height): for x in range(self.width): self.buf1[y][x] = self.buf2[y][x] for y in range(self.height): - row = ''.join(['*' if self.buf1[y][x] else ' ' for x in range(self.width)]) + row = "".join(["*" if self.buf1[y][x] else " " for x in range(self.width)]) print(row) def get(self, x, y): diff --git a/examples/switch.py b/examples/switch.py index 0efaf2267..c099a3529 100644 --- a/examples/switch.py +++ b/examples/switch.py @@ -24,6 +24,7 @@ orange_led = pyb.LED(3) blue_led = pyb.LED(4) all_leds = (red_led, green_led, orange_led, blue_led) + def run_loop(leds=all_leds): """ Start the loop. @@ -31,15 +32,16 @@ def run_loop(leds=all_leds): :param `leds`: Which LEDs to light up upon switch press. :type `leds`: sequence of LED objects """ - print('Loop started.\nPress Ctrl+C to break out of the loop.') + print("Loop started.\nPress Ctrl+C to break out of the loop.") while 1: try: if switch(): [led.on() for led in leds] else: [led.off() for led in leds] - except OSError: # VCPInterrupt # Ctrl+C in interpreter mode. + except OSError: # VCPInterrupt # Ctrl+C in interpreter mode. break -if __name__ == '__main__': + +if __name__ == "__main__": run_loop() diff --git a/examples/unix/ffi_example.py b/examples/unix/ffi_example.py index 3c3c3d239..a8f02c766 100644 --- a/examples/unix/ffi_example.py +++ b/examples/unix/ffi_example.py @@ -24,12 +24,14 @@ print("errno value:", errno.get()) perror("perror after error") print() + def cmp(pa, pb): a = uctypes.bytearray_at(pa, 1) b = uctypes.bytearray_at(pb, 1) print("cmp:", a, b) return a[0] - b[0] + cmp_cb = ffi.callback("i", cmp, "PP") print("callback:", cmp_cb) diff --git a/examples/unix/machine_bios.py b/examples/unix/machine_bios.py index f62e4dbdb..878f3fd8f 100644 --- a/examples/unix/machine_bios.py +++ b/examples/unix/machine_bios.py @@ -6,4 +6,4 @@ import umachine as machine -print(hex(machine.mem16[0xc0000])) +print(hex(machine.mem16[0xC0000])) -- cgit v1.2.3