Skip to content

Drawing a Scalable Alarm Bell Icon

Sample Prompt

Prompt

Using the MicroPython poly function, please create a Python function that draws an alarm icon on a 22x24 grid. The function should have three parameters: x, y and scale where x and y are the upper left corner coordinates. Just draw the bell body and the clapper.

Test the icon at scales from 12 to 36.

Sample Result

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
from machine import Pin, I2C, SPI
from utime import sleep, localtime
from ds1307 import DS1307
from array import array

import ssd1306
import config

SCL=Pin(config.SPI_SCL_PIN)
SDA=Pin(config.SPI_SDA_PIN)
DC = Pin(config.SPI_DC_PIN)
RES = Pin(config.SPI_RESET_PIN)
CS = Pin(config.SPI_CS_PIN)
SPI_BUS = config.SPI_BUS
WIDTH = config.DISPLAY_WIDTH
HEIGHT = config.DISPLAY_HEIGHT

spi=SPI(SPI_BUS, sck=SCL, mosi=SDA, baudrate=1000000)
oled = ssd1306.SSD1306_SPI(WIDTH, HEIGHT, spi, DC, RES, CS)

i2c_sda = Pin(config.I2C_SDA_PIN)
i2c_scl = Pin(config.I2C_SCL_PIN)
I2C_BUS = config.I2C_BUS
RTC_TYPE = config.RTC_TYPE
RTC_I2C_ADDR = config.RTC_I2C_ADDR

# I2C setup
i2c = I2C(I2C_BUS, scl=i2c_scl, sda=i2c_sda, freq=3000000)
rtc = DS1307(addr=RTC_I2C_ADDR, i2c=i2c)

def draw_alarm_icon(display, x, y, size=24):
    """
    Draw an alarm bell icon with time display using polygons.

    Args:
        display: SSD1306 display instance
        x, y: Top-left position for the icon
        size: Base size of the icon (default 24 pixels)
    """
    # Scale factors
    scale = size / 24  # Base size is 24 pixels

    # Bell body coordinates (scaled from base design 22 wide and 20 high)
    bell_body = array('B', [
        # start at the top right and go 0,4,10,17 and 20 down
        int(14 * scale), int(0 * scale),    # Top right of dome top row right
        int(17 * scale), int(4 * scale),    # Top right of dome 2nd row
        int(19 * scale), int(10 * scale),   # Bottom right curve
        int(18 * scale), int(16 * scale),   # Right side indent
        # bottom row
        int(22 * scale), int(20 * scale),   # Right bottom corner
        int(0 * scale),  int(20 * scale),   # Bottom left corner
        # left side
        int(4 * scale),  int(16 * scale),   # Left up indent
        int(3 * scale),  int(10 * scale),   # Left up
        int(5 * scale),  int(4 * scale),    # Top left of dome 2nd row
        int(8 * scale),  int(0 * scale),    # Top left of dome top row left
    ])

    # Clapper coordinates
    clapper = array('B', [
        int(10 * scale), int(20 * scale),   # Top left
        int(12 * scale), int(20 * scale),   # Top right
        int(14 * scale), int(24 * scale),   # Bottom right
        int(8 * scale), int(24 * scale),   # Bottom left
    ])

    # Draw the components
    display.poly(x, y, bell_body, 1, 1)  # Filled bell body
    display.poly(x, y, clapper, 1, 1)    # Filled clapper

def demo_alarm_icons(display):
    """
    Demonstrate the alarm icon at different sizes and positions
    """
    # Clear the display
    display.fill(0)

    # Draw three different sized bells
    draw_alarm_icon(display, 0,  0, size=10)    # Very small bell 10
    draw_alarm_icon(display, 20, 0, size=14)    # Very small bell 14 
    draw_alarm_icon(display, 40, 0, size=16)    # Very small bell 16
    draw_alarm_icon(display, 60, 0, size=20)    # Small bell 20
    draw_alarm_icon(display, 80, 0, size=24)    # Standard bell 24

    draw_alarm_icon(display, 0,  24, size=28)   # Medium bell 28
    draw_alarm_icon(display, 30, 24, size=32)   # Medium bell 28
    draw_alarm_icon(display, 60, 24, size=36)   # Large bell 36


    # Update the display
    display.show()


demo_alarm_icons(oled)