Advertisement
rric

kabel_zum_spielen

Feb 6th, 2024 (edited)
10
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.79 KB | None | 0 0
  1. # Liest Werte des Beschleunigungssensors und schickt diese über die
  2. # serielle Schnittstelle
  3. # Copyright 2023, 2024 Roland Richter                [Mu: BBC micro:bit]
  4.  
  5. import microbit
  6.  
  7. # Wandle einen Gyro-Wert (-1000 bis 1000) in eine Zeile/Spalte (0 bis 4) um
  8. def compute_position(value):
  9.     if value <= -1000:
  10.         value = -999
  11.     if value >= 1000:
  12.         value = 999
  13.  
  14.     scaled = (value + 1000) // 400
  15.     return scaled
  16.  
  17.  
  18. # Zeichne eine senkrechte Linie in Spalte c (zwischen 0 und 4)
  19. def draw_vertical_line(c):
  20.     for row in range(5):
  21.         microbit.display.set_pixel(c, row, 9)
  22.  
  23.  
  24. # Zeichne eine waagrechte Linie in Zeile r (zwischen 0 und 4)
  25. def draw_horizontal_line(r):
  26.     for col in range(5):
  27.         microbit.display.set_pixel(col, r, 9)
  28.  
  29.  
  30. # Taste A gedrückt, geneigt nach links vorne: "Ab<^"
  31. # keine Taste gedrückt, geneigt nach rechts hinten: "ab>v"
  32. # Tasten A und B gedrückt, annähernde waagrecht: "AB|-"
  33. def create_sensor_string():
  34.     result = ""
  35.  
  36.     # Status der Tasten A und B
  37.     result += ('A' if microbit.button_a.is_pressed() else 'a')
  38.     result += ('B' if microbit.button_b.is_pressed() else 'b')
  39.  
  40.     # x- und y-Wert des Beschleunigungssensors
  41.     x_val = microbit.accelerometer.get_x()
  42.     y_val = microbit.accelerometer.get_y()
  43.     result += ('<' if x_val < -200 else ('>' if x_val > 200 else '|'))
  44.     result += ('^' if y_val < -200 else ('v' if y_val > 200 else '-'))
  45.  
  46.     # PROBIERE, auch die Daten des Kompass in den Text einzufügen
  47.  
  48.     return result
  49.  
  50. # Öffne die serielle Schnittstelle
  51. microbit.uart.init(115200)
  52.  
  53. while True:
  54.     x_val = microbit.accelerometer.get_x()
  55.     y_val = microbit.accelerometer.get_y()
  56.  
  57.     col = compute_position(x_val)
  58.     row = compute_position(y_val)
  59.  
  60.     microbit.display.clear()
  61.     draw_vertical_line(col)
  62.     draw_horizontal_line(row)
  63.  
  64.     # Sende Daten an den Plotter:
  65.     # print((x_val, y_val))
  66.  
  67.     res = microbit.uart.write(create_sensor_string())
  68.     if res is None:
  69.         microbit.display.show(microbit.Image.SAD)
  70.         microbit.sleep(1000)
  71.  
  72.     microbit.sleep(10)
  73.  
  74.  
  75. # ----------------------------------------------------------------------
  76. # This program is free software: you can redistribute it and/or modify
  77. # it under the terms of the GNU General Public License as published by
  78. # the Free Software Foundation, either version 3 of the License, or
  79. # (at your option) any later version.
  80. #
  81. # This program is distributed in the hope that it will be useful,
  82. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  83. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  84. # GNU General Public License for more details.
  85. #
  86. # You should have received a copy of the GNU General Public License
  87. # along with this program.  If not, see <https://www.gnu.org/licenses/>.
  88.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement