gfx.ino 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. void drawPixel(uint8_t x, uint8_t y, bool on) {
  2. uint8_t page = y / 8;
  3. uint8_t bit = y % 8;
  4. uint8_t column = x;
  5. // Set the page and column address
  6. I2CWriteCommand(0xB0 + page); // Set page address
  7. I2CWriteCommand(0x00 | (column & 0x0F)); // Set lower column address
  8. I2CWriteCommand(0x10 | (column >> 4)); // Set higher column address
  9. // Read the current data
  10. uint8_t data;
  11. I2CStart();
  12. I2CSend(OLED_I2C_ADDRESS << 1); // I2C address + Write bit
  13. I2CSend(0x00); // Co = 0, D/C# = 0 (Command mode)
  14. I2CSend(0x00); // Dummy command to read data
  15. I2CStop();
  16. // Assume data is read correctly, here data should be handled
  17. // Modify data based on the `on` parameter
  18. if (on) {
  19. data |= (1 << bit); // Set the pixel bit
  20. } else {
  21. data &= ~(1 << bit); // Clear the pixel bit
  22. }
  23. // Write the modified data back to the display
  24. I2CStart();
  25. I2CSend(OLED_I2C_ADDRESS << 1); // I2C address + Write bit
  26. I2CSend(0x40); // Co = 0, D/C# = 1 (Data mode)
  27. I2CSend(data); // Write the modified data
  28. I2CStop();
  29. }
  30. void drawRectangle(uint8_t x, uint8_t y, uint8_t width, uint8_t height, bool fill) {
  31. for (uint8_t i = 0; i < width; i++) {
  32. for (uint8_t j = 0; j < height; j++) {
  33. if (fill || (i == 0 || i == width - 1 || j == 0 || j == height - 1)) {
  34. drawPixel(x + i, y + j, true); // Draw the pixel
  35. }
  36. }
  37. }
  38. }