microbitimage.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #ifndef __MICROPY_INCLUDED_MICROBIT_IMAGE_H__
  2. #define __MICROPY_INCLUDED_MICROBIT_IMAGE_H__
  3. #include "py/runtime.h"
  4. #define MAX_BRIGHTNESS 9
  5. /** Monochrome images are immutable, which means that
  6. * we only need one bit per pixel which saves quite a lot
  7. * of memory */
  8. /* we reserve a couple of bits, so we won't need to modify the
  9. * layout if we need to add more functionality or subtypes. */
  10. #define TYPE_AND_FLAGS \
  11. mp_obj_base_t base; \
  12. uint8_t five:1; \
  13. uint8_t reserved1:1; \
  14. uint8_t reserved2:1
  15. typedef struct _image_base_t {
  16. TYPE_AND_FLAGS;
  17. } image_base_t;
  18. typedef struct _monochrome_5by5_t {
  19. TYPE_AND_FLAGS;
  20. uint8_t pixel44: 1;
  21. uint8_t bits24[3];
  22. } monochrome_5by5_t;
  23. /* This is an internal method it is up to the caller to validate the inputs */
  24. uint8_t monochromeGetPixelValue(monochrome_5by5_t * p_mono, mp_int_t x, mp_int_t y);
  25. typedef struct _greyscale_t {
  26. TYPE_AND_FLAGS;
  27. uint8_t height;
  28. uint8_t width;
  29. uint8_t byte_data[]; /* Static initializer for this will have to be C, not C++ */
  30. } greyscale_t;
  31. #if 1
  32. void clear(greyscale_t * p_greyscale);
  33. /* Thiese are internal methods and it is up to the caller to validate the inputs */
  34. uint8_t greyscaleGetPixelValue(greyscale_t * p_greyscale, mp_int_t x, mp_int_t y);
  35. void greyscaleSetPixelValue(greyscale_t * p_greyscale, mp_int_t x, mp_int_t y, mp_int_t val);
  36. void greyscaleFill(greyscale_t * p_greyscale, mp_int_t val);
  37. #endif
  38. typedef union _microbit_image_obj_t {
  39. image_base_t base;
  40. monochrome_5by5_t monochrome_5by5;
  41. greyscale_t greyscale;
  42. } microbit_image_obj_t;
  43. #if 1
  44. mp_int_t imageHeight(microbit_image_obj_t * p_image_obj);
  45. mp_int_t imageWidth(microbit_image_obj_t * p_image_obj);
  46. greyscale_t * imageCopy(microbit_image_obj_t * p_image_obj);
  47. greyscale_t * imageInvert(microbit_image_obj_t * p_image_obj);
  48. /* This is an internal method it is up to the caller to validate the inputs */
  49. uint8_t imageGetPixelValue(microbit_image_obj_t * p_image_obj, mp_int_t x, mp_int_t y);
  50. #endif
  51. /** Return a facade object that presents the string as a sequence of images */
  52. mp_obj_t microbit_string_facade(mp_obj_t string);
  53. void microbit_image_set_from_char(greyscale_t *img, char c);
  54. microbit_image_obj_t *microbit_image_for_char(char c);
  55. mp_obj_t microbit_image_slice(microbit_image_obj_t *img, mp_int_t start, mp_int_t width, mp_int_t stride);
  56. /* ref exists so that we can pull a string out of an object and not have it GC'ed while oterating over it */
  57. mp_obj_t scrolling_string_image_iterable(const char* str, mp_uint_t len, mp_obj_t ref, bool monospace, bool repeat);
  58. #define SMALL_IMAGE(p0, p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15, p16, p17, p18, p19, p20, p21, p22, p23, p44) \
  59. { \
  60. { &microbit_image_type }, \
  61. 1, 0, 0, (p44), \
  62. { \
  63. (p0)|((p1)<<1)|((p2)<<2)|((p3)<<3)|((p4)<<4)|((p5)<<5)|((p6)<<6)|((p7)<<7), \
  64. (p8)|((p9)<<1)|((p10)<<2)|((p11)<<3)|((p12)<<4)|((p13)<<5)|((p14)<<6)|((p15)<<7), \
  65. (p16)|((p17)<<1)|((p18)<<2)|((p19)<<3)|((p20)<<4)|((p21)<<5)|((p22)<<6)|((p23)<<7) \
  66. } \
  67. }
  68. extern const monochrome_5by5_t microbit_blank_image;
  69. extern const monochrome_5by5_t microbit_const_image_heart_obj;
  70. extern const mp_obj_type_t microbit_image_type;
  71. #define BLANK_IMAGE (microbit_image_obj_t *)(&microbit_blank_image)
  72. #define HEART_IMAGE (microbit_image_obj_t *)(&microbit_const_image_heart_obj)
  73. #define HAPPY_IMAGE (microbit_image_obj_t *)(&microbit_const_image_happy_obj)
  74. #if MICROPY_PY_BUILTINS_FLOAT
  75. microbit_image_obj_t *microbit_image_dim(microbit_image_obj_t *lhs, mp_float_t fval);
  76. #else
  77. microbit_image_obj_t *microbit_image_dim(microbit_image_obj_t *lhs, mp_int_t val);
  78. #endif
  79. microbit_image_obj_t *microbit_image_sum(microbit_image_obj_t *lhs, microbit_image_obj_t *rhs, bool add);
  80. #endif // __MICROPY_INCLUDED_MICROBIT_IMAGE_H__