Home Codebug Happy or Sad face on Codebug

Happy or Sad face on Codebug

by iainhendry

Lets press a button and then display a happy face with button A pressed and a sad face if button B is pressed

Code

Python Code

# To run this code on CodeBug, visit:
#
#     http://www.codebug.org.uk/learn/activity/66/tethering-codebug-with-python/#step585
#
import codebug_tether
from codebug_tether import (IO_DIGITAL_OUTPUT,
                            IO_DIGITAL_INPUT,
                            IO_PWM_OUTPUT,
                            IO_ANALOG_INPUT)
from codebug_tether.sprites import Sprite

display = None
display_1 = None

codebug = codebug_tether.CodeBug()
codebug.set_leg_io(0, IO_DIGITAL_INPUT);
codebug.set_leg_io(1, IO_DIGITAL_INPUT);
codebug.set_leg_io(2, IO_DIGITAL_INPUT);
codebug.set_leg_io(3, IO_DIGITAL_INPUT);
codebug.set_leg_io(4, IO_DIGITAL_INPUT);
codebug.set_leg_io(5, IO_DIGITAL_INPUT);
codebug.set_leg_io(6, IO_DIGITAL_INPUT);
codebug.set_leg_io(7, IO_DIGITAL_INPUT);

def build_sprite(rows):
  s = Sprite(5, 5)
  for i in range(5):
    s.set_row(i, rows[i])
  return s


display = build_sprite([0b01110,0b10001,0b00000,0b00100,0b10001])
display_1 = build_sprite([0b10001,0b01110,0b00000,0b00100,0b10001])
while True:
  if codebug.get_input('A') == 1:
    codebug.draw_sprite(0, 0, display);
  if codebug.get_input('B') == 1:
    codebug.draw_sprite(0, 0, display_1);

Javascript Code

var display;
var display_1;


codebug_direction('U');
codebug_sleepafter(5);
io_configure(0, IO_DIGITAL_INPUT);
io_configure(1, IO_DIGITAL_INPUT);
io_configure(2, IO_DIGITAL_INPUT);
io_configure(3, IO_DIGITAL_INPUT);
io_configure(4, IO_DIGITAL_INPUT);
io_configure(5, IO_DIGITAL_INPUT);
io_configure(6, IO_DIGITAL_INPUT);
io_configure(7, IO_DIGITAL_INPUT);
io_configure_pullup(0, 0);
io_configure_pullup(2, 0);
io_configure_pullup(3, 0);
io_configure_pullup(4, 0);
io_configure_pullup(5, 0);


display = sprite_build([[0, 1, 1, 1, 0], [1, 0, 0, 0, 1], [0, 0, 0, 0, 0], [0, 0, 1, 0, 0], [1, 0, 0, 0, 1]]);
display_1 = sprite_build([[1, 0, 0, 0, 1], [0, 1, 1, 1, 0], [0, 0, 0, 0, 0], [0, 0, 1, 0, 0], [1, 0, 0, 0, 1]]);
while (true) {
  if (io_get_input('A') == 1) {
    fivebyfivedisplay.sprite_render(0, 0, display);
    fivebyfivedisplay.update();
  }
  if (io_get_input('B') == 1) {
    fivebyfivedisplay.sprite_render(0, 0, display_1);
    fivebyfivedisplay.update();
  }
}

You may also like

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More