{
"id": "264",
"prompt": "A thermostat with hysteresis: room temperature (x16 fixed point in a slot) gains 10 per frame while the heater runs and always leaks toward a cold outside at (temp - ambient)/96. The heater is bang-bang with a dead band - ON below setpoint minus 1 degree, OFF above setpoint plus 1 - so the temperature sawtooths inside the band instead of chattering. Dragging vertically moves the setpoint. Draw a thermometer column, the two dead-band lines, an orange HEAT ON indicator, and the live temperature and setpoint in whole degrees.",
"solution_rl": "fn frame(t: i32) {\n let w: i32 = host::display::width();\n let h: i32 = host::display::height();\n if host::display::state_get(0) == 0 {\n host::display::state_set(0, 1);\n host::display::state_set(1, 288);\n host::display::state_set(2, 0);\n host::display::state_set(3, 336);\n }\n let mut temp: i32 = host::display::state_get(1);\n let mut heater: i32 = host::display::state_get(2);\n let mut set: i32 = host::display::state_get(3);\n if host::display::pointer_down() == 1 {\n set = 192 + (h - host::display::pointer_y()) * 288 / h;\n }\n if temp < set - 16 { heater = 1; }\n if temp > set + 16 { heater = 0; }\n temp = temp + heater * 10;\n temp = temp - (temp - 80) / 96;\n host::display::state_set(1, temp);\n host::display::state_set(2, heater);\n host::display::state_set(3, set);\n host::display::clear(0x0c0c10);\n let tube_x: i32 = w / 2 - 20;\n let base: i32 = h - 60;\n host::display::fill_rect(tube_x - 4, 56, 48, base - 52, 0x1c1c24);\n let bar: i32 = temp * 5 / 8;\n host::display::fill_rect(tube_x, base - bar, 40, bar, if heater == 1 { 0xe05030 } else { 0xc04040 });\n let sp: i32 = base - set * 5 / 8;\n host::display::fill_rect(tube_x - 12, sp - 10, 64, 1, 0xd0d0d0);\n host::display::fill_rect(tube_x - 12, sp + 10, 64, 1, 0xd0d0d0);\n if heater == 1 {\n host::display::fill_rect(tube_x, base + 10, 40, 14, 0xff8020);\n host::display::draw_string(tube_x + 50, base + 12, \"HEAT ON\", 0xff8020, 1);\n }\n host::display::draw_string(30, 30, \"TEMP\", 0x808080, 2);\n host::display::draw_number(110, 30, temp / 16, 0xffffff, 2);\n host::display::draw_string(30, 60, \"SET\", 0x808080, 1);\n host::display::draw_number(80, 60, set / 16, 0xa0a0a0, 1);\n host::display::draw_string(30, h - 24, \"DRAG UP DOWN = SETPOINT\", 0x505050, 1);\n host::display::present();\n}\n",
"tags": [
"simulation",
"control",
"state",
"pointer"
]
}