summaryrefslogtreecommitdiff
path: root/shared-module/_pew
diff options
context:
space:
mode:
authorDan Halbert <halbert@halwitz.org>2019-03-01 14:07:32 -0500
committerGitHub <noreply@github.com>2019-03-01 14:07:32 -0500
commit17bf2afa414d605676643be301a05d74c3b68290 (patch)
tree640270114605db6c574ff7886f6ed44f8455af08 /shared-module/_pew
parent1ba761086a057387a7a14d9df1bf267850fa303d (diff)
parent263134dcd3e1d0151be081a2ba04b358f732035e (diff)
Merge pull request #1607 from pewpew-game/pewpew10.x-4.x
Add support for PewPew 10.x boards
Diffstat (limited to 'shared-module/_pew')
-rw-r--r--shared-module/_pew/PewPew.c125
-rw-r--r--shared-module/_pew/PewPew.h48
-rw-r--r--shared-module/_pew/__init__.c85
-rw-r--r--shared-module/_pew/__init__.h32
4 files changed, 290 insertions, 0 deletions
diff --git a/shared-module/_pew/PewPew.c b/shared-module/_pew/PewPew.c
new file mode 100644
index 000000000..c1d6aa5c3
--- /dev/null
+++ b/shared-module/_pew/PewPew.c
@@ -0,0 +1,125 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2019 Radomir Dopieralski
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#include <stdbool.h>
+
+#include "py/mpstate.h"
+#include "py/runtime.h"
+#include "__init__.h"
+#include "PewPew.h"
+
+#include "shared-bindings/digitalio/Pull.h"
+#include "shared-bindings/digitalio/DigitalInOut.h"
+#include "shared-bindings/util.h"
+#include "samd/timers.h"
+#include "supervisor/shared/translate.h"
+#include "timer_handler.h"
+
+
+static uint8_t pewpew_tc_index = 0xff;
+
+
+void pewpew_interrupt_handler(uint8_t index) {
+ if (index != pewpew_tc_index) {
+ return;
+ }
+ Tc* tc = tc_insts[index];
+ if (!tc->COUNT16.INTFLAG.bit.MC0) {
+ return;
+ }
+
+ pew_tick();
+
+ // Clear the interrupt bit.
+ tc->COUNT16.INTFLAG.reg = TC_INTFLAG_MC0;
+}
+
+void pew_init() {
+ pew_obj_t* pew = MP_STATE_VM(pew_singleton);
+
+ common_hal_digitalio_digitalinout_switch_to_input(pew->buttons, PULL_UP);
+
+ for (size_t i = 0; i < pew->rows_size; ++i) {
+ digitalio_digitalinout_obj_t *pin = MP_OBJ_TO_PTR(pew->rows[i]);
+ common_hal_digitalio_digitalinout_switch_to_output(pin, false,
+ DRIVE_MODE_PUSH_PULL);
+ }
+ for (size_t i = 0; i < pew->cols_size; ++i) {
+ digitalio_digitalinout_obj_t *pin = MP_OBJ_TO_PTR(pew->cols[i]);
+ common_hal_digitalio_digitalinout_switch_to_output(pin, true,
+ DRIVE_MODE_OPEN_DRAIN);
+ }
+ if (pewpew_tc_index == 0xff) {
+ // Find a spare timer.
+ uint8_t index = find_free_timer();
+ if (index == 0xff) {
+ mp_raise_RuntimeError(translate(""));
+ }
+ Tc *tc = tc_insts[index];
+
+ pewpew_tc_index = index;
+ set_timer_handler(true, index, TC_HANDLER_PEW);
+
+ // We use GCLK0 for SAMD21 and GCLK1 for SAMD51 because they both run
+ // at 48mhz making our math the same across the boards.
+ #ifdef SAMD21
+ turn_on_clocks(true, index, 0);
+ #endif
+ #ifdef SAMD51
+ turn_on_clocks(true, index, 1);
+ #endif
+
+
+ #ifdef SAMD21
+ tc->COUNT16.CTRLA.reg = TC_CTRLA_MODE_COUNT16 |
+ TC_CTRLA_PRESCALER_DIV64 |
+ TC_CTRLA_WAVEGEN_MFRQ;
+ #endif
+ #ifdef SAMD51
+ tc_reset(tc);
+ tc_set_enable(tc, false);
+ tc->COUNT16.CTRLA.reg = TC_CTRLA_MODE_COUNT16
+ | TC_CTRLA_PRESCALER_DIV64;
+ tc->COUNT16.WAVE.reg = TC_WAVE_WAVEGEN_MFRQ;
+ #endif
+
+ tc_set_enable(tc, true);
+ tc->COUNT16.CC[0].reg = 160;
+
+ // Clear our interrupt in case it was set earlier
+ tc->COUNT16.INTFLAG.reg = TC_INTFLAG_MC0;
+ tc->COUNT16.INTENSET.reg = TC_INTENSET_MC0;
+ tc_enable_interrupts(pewpew_tc_index);
+ }
+}
+
+void pew_reset(void) {
+ if (pewpew_tc_index != 0xff) {
+ tc_reset(tc_insts[pewpew_tc_index]);
+ pewpew_tc_index = 0xff;
+ }
+ MP_STATE_VM(pew_singleton) = NULL;
+}
diff --git a/shared-module/_pew/PewPew.h b/shared-module/_pew/PewPew.h
new file mode 100644
index 000000000..da1cae0a2
--- /dev/null
+++ b/shared-module/_pew/PewPew.h
@@ -0,0 +1,48 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2019 Radomir Dopieralski
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#ifndef MICROPY_INCLUDED_PEW_PEWPEW_H
+#define MICROPY_INCLUDED_PEW_PEWPEW_H
+
+#include <stdint.h>
+#include "shared-bindings/digitalio/DigitalInOut.h"
+
+typedef struct {
+ mp_obj_base_t base;
+ uint8_t* buffer;
+ mp_obj_t* rows;
+ mp_obj_t* cols;
+ digitalio_digitalinout_obj_t *buttons;
+ uint8_t rows_size;
+ uint8_t cols_size;
+ uint8_t pressed;
+} pew_obj_t;
+
+void pew_init(void);
+void pewpew_interrupt_handler(uint8_t index);
+void pew_reset(void);
+
+#endif // MICROPY_INCLUDED_PEW_PEWPEW_H
diff --git a/shared-module/_pew/__init__.c b/shared-module/_pew/__init__.c
new file mode 100644
index 000000000..90fba3990
--- /dev/null
+++ b/shared-module/_pew/__init__.c
@@ -0,0 +1,85 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2019 Radomir Dopieralski
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#include <stdbool.h>
+
+#include "py/mpstate.h"
+#include "__init__.h"
+#include "PewPew.h"
+
+#include "shared-bindings/digitalio/DigitalInOut.h"
+
+
+void pew_tick(void) {
+ static uint8_t col = 0;
+ static uint8_t turn = 0;
+ static uint8_t pressed = 0;
+ static uint8_t last_pressed = 0;
+ digitalio_digitalinout_obj_t *pin;
+
+ pew_obj_t* pew = MP_STATE_VM(pew_singleton);
+ if (!pew) { return; }
+
+ pin = MP_OBJ_TO_PTR(pew->cols[col]);
+ ++col;
+ if (col >= pew->cols_size) {
+ pew->pressed |= last_pressed & pressed;
+ last_pressed = pressed;
+ pressed = 0;
+ col = 0;
+ ++turn;
+ if (turn >= 8) {
+ turn = 0;
+ }
+ }
+ if (!common_hal_digitalio_digitalinout_get_value(pew->buttons)) {
+ pressed |= 1 << col;
+ }
+ common_hal_digitalio_digitalinout_set_value(pin, true);
+ for (size_t x = 0; x < pew->rows_size; ++x) {
+ pin = MP_OBJ_TO_PTR(pew->rows[x]);
+ uint8_t color = pew->buffer[col * (pew->rows_size) + x];
+ bool value = false;
+ switch (color & 0x03) {
+ case 3:
+ value = true;
+ break;
+ case 2:
+ if (turn == 2 || turn == 4 || turn == 6) {
+ value = true;
+ }
+ case 1:
+ if (turn == 0) {
+ value = true;
+ }
+ case 0:
+ break;
+ }
+ common_hal_digitalio_digitalinout_set_value(pin, value);
+ }
+ pin = MP_OBJ_TO_PTR(pew->cols[col]);
+ common_hal_digitalio_digitalinout_set_value(pin, false);
+}
diff --git a/shared-module/_pew/__init__.h b/shared-module/_pew/__init__.h
new file mode 100644
index 000000000..4f953c610
--- /dev/null
+++ b/shared-module/_pew/__init__.h
@@ -0,0 +1,32 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2019 Radomir Dopieralski
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#ifndef MICROPY_INCLUDED_PEW_H
+#define MICROPY_INCLUDED_PEW_H
+
+void pew_tick(void);
+
+#endif // MICROPY_INCLUDED_PEW_H