Coverage for /Users/fmorton/GitHub/BirdBrain-Python-Library-2/src/birdbrain_microbit_output.py: 100%

31 statements  

« prev     ^ index     » next       coverage.py v7.6.12, created at 2025-03-04 20:20 -0500

1from birdbrain_constant import BirdbrainConstant 

2from birdbrain_exception import BirdbrainException 

3from birdbrain_request import BirdbrainRequest 

4from birdbrain_state import BirdbrainState 

5from birdbrain_utility import BirdbrainUtility 

6 

7class BirdbrainMicrobitOutput(BirdbrainRequest): 

8 @classmethod 

9 def display(self, state, device, list): 

10 if len(list) != 25: raise BirdbrainException("Error: display() requires a list of length 25") 

11 

12 return BirdbrainRequest.response_status('hummingbird', 'out', 'symbol', device, state.display_map_as_string(list)) 

13 

14 @classmethod 

15 def clear_display(self, state, device): 

16 return self.display(state, device, BirdbrainState.microbit_empty_display_map()) 

17 

18 @classmethod 

19 def point(self, state, device, x, y, value): 

20 index = ((x * 5) + y - 6) 

21 

22 try: 

23 state.display_map[index] = value 

24 except IndexError: 

25 raise BirdbrainException("Error: point out of range") 

26 

27 return self.display(state, device, state.display_map) 

28 

29 @classmethod 

30 def print(self, state, device, message): 

31 """Print the characters on the LED screen.""" 

32 

33 # clear internal representation of the display since it will be blank when the print ends 

34 self.clear_display(state, device) 

35 

36 # need to encode space for uri (used to be %20) 

37 message = message.replace(' ', '+') 

38 

39 return BirdbrainRequest.response_status('hummingbird', 'out', 'print', message) 

40 

41 @classmethod 

42 def play_note(self, device, note, beats): 

43 """Make the buzzer play a note for certain number of beats. Note is the midi 

44 note number and should be specified as an integer from 32 to 135. Beats can be 

45 any number from 0 to 16. One beat corresponds to one second.""" 

46 

47 note = BirdbrainUtility.bounds(note, 32, 135) 

48 beats = int(BirdbrainUtility.decimal_bounds(beats, 0, 16) * BirdbrainConstant.BEATS_TEMPO_FACTOR) 

49 

50 return BirdbrainRequest.response_status('hummingbird', 'out', 'playnote', note, beats, device)