{
  "id": "planetinnovation/micropython-mock-machine",
  "name": "micropython-mock-machine",
  "prettyName": "micropython-mock-machine",
  "description": "A MicroPython library designed to mock the machine module for use in unit tests.",
  "version": "",
  "author": "planetinnovation",
  "license": "MIT",
  "tags": [],
  "status": "VALID",
  "official": false,
  "source": "https://github.com/planetinnovation/micropython-mock-machine",
  "updatedAt": "2026-08-20T07:01:00.335Z",
  "page": "https://checkmim.com/packages/planetinnovation+micropython-mock-machine",
  "createdAt": "2026-01-28T08:46:06.487Z",
  "submittedBy": "oliverr",
  "install": {
    "mpremote": "mpremote mip install github:planetinnovation/micropython-mock-machine",
    "mip": "import mip; mip.install(\"github:planetinnovation/micropython-mock-machine\")"
  },
  "readme": "# micropython-mock-machine\n\nA MicroPython library that provides mock implementations of the `machine` module for unit testing.\n\nThis library allows you to test MicroPython drivers and applications that use the `machine` module without requiring actual hardware. Mock implementations are provided for common peripherals like I2C, SPI, Pin, ADC, and more.\n\n## Features\n\nCurrently `mock_machine` implements:\n\n- **I2C** - Inter-Integrated Circuit bus with device simulation\n  - `init`, `deinit`, `scan`\n  - `readfrom`, `readfrom_into`, `writeto`\n  - `readfrom_mem`, `readfrom_mem_into`, `writeto_mem`\n  - `add_device` (for simulating multiple devices on the bus)\n\n- **SPI** - Serial Peripheral Interface\n  - `init`, `deinit`\n  - `read`, `readinto`, `write`, `write_readinto`\n\n- **Pin** - Digital I/O pins\n  - `init`, `value`, `on`, `off`, `high`, `low`\n  - `irq` with IRQ_RISING and IRQ_FALLING triggers\n  - `mode` configuration\n  - `Pin.board` and `Pin.cpu` namespaces for pin name resolution\n\n- **ADC** - Analog to Digital Converter\n  - `read_u16`\n\n- **PWM** - Pulse Width Modulation\n  - Frequency and duty cycle control\n\n- **UART** - Universal Asynchronous Receiver/Transmitter\n  - Read/write operations with buffering\n\n- **Timer** - Hardware timers\n  - Periodic and one-shot modes\n\n- **WDT** - Watchdog Timer\n  - Timeout and feed operations\n\n- **RTC** - Real Time Clock\n  - Wakeup alarms and datetime\n\nAdditional utilities:\n- **I2CDevice** - Helper class for simulating I2C devices with register-based interfaces\n- **RegisterBasedI2CDevice** - Extended I2CDevice for testing register-based I2C devices (RTCs, sensors)\n- **Signal** - Digital signal with inversion support\n\n## Installation\n\n### Using mip (recommended)\n\n```python\nimport mip\nmip.install(\"github:planetinnovation/micropython-mock-machine\")\n```\n\n### Manual installation\n\nCopy `mock_machine.py` to your MicroPython device's `/lib` directory.\n\n## Usage\n\n### Basic Example\n\n```python\n# In your test file\nimport mock_machine\n\n# Register mock_machine as the machine module\nmock_machine.register_as_machine()\n\n# Now any import of 'machine' will use the mock\nimport machine\n\n# Create mock hardware\ni2c = machine.I2C(0)\npin = machine.Pin(0, machine.Pin.OUT)\n\n# Use as normal\npin.value(1)\nassert pin.value() == 1\n```\n\n### Simulating I2C Devices\n\n```python\nimport mock_machine\nfrom mock_machine import I2C, I2CDevice\n\n# Create I2C bus\ni2c = I2C(0)\n\n# Create a mock device at address 0x68\ndevice = I2CDevice(addr=0x68, i2c=i2c)\n\n# Set up register values\ndevice.register_values[0x00] = b'\\x12\\x34'  # WHO_AM_I register\ndevice.register_values[0x01] = b'\\x00\\x00'  # Control register\n\n# Your driver can now read from the device\ndata = i2c.readfrom_mem(0x68, 0x00, 2)\nassert data == b'\\x12\\x34'\n```\n\n### Pin Interrupts\n\n```python\nimport mock_machine\nimport asyncio\n\nmock_machine.register_as_machine()\nimport machine\n\n# Create a pin with interrupt\npin = machine.Pin(0, machine.Pin.IN)\n\n# Set up interrupt handler\ndef pin_handler(pin):\n    print(f\"Pin interrupt! Value: {pin.value()}\")\n\npin.irq(handler=pin_handler, trigger=machine.Pin.IRQ_RISING)\n\n# Simulate pin change\npin.value(0)  # No interrupt\npin.value(1)  # Triggers interrupt (rising edge)\n```\n\n### SPI Communication\n\n```python\nimport mock_machine\nmock_machine.register_as_machine()\nimport machine\n\n# Create SPI bus\nspi = machine.SPI(0)\n\n# Set up mock responses\nspi.read_buf = b'\\x01\\x02\\x03\\x04'\n\n# Test your SPI driver\ndata = spi.read(4)\nassert data == b'\\x01\\x02\\x03\\x04'\n\n# Check what was written\nspi.write(b'\\xAA\\xBB')\nassert spi.write_buf == b'\\xAA\\xBB'\n```\n\n## Examples\n\nThe `examples/` directory contains sample drivers with tests:\n- `hardware_revision.py` - Reading hardware revision from I2C EEPROM\n- `tmp117.py` - TMP117 temperature sensor driver with I2C communication\n\n## Testing\n\nRun the unit tests:\n\n```bash\n# Run all tests\npython -m pytest test/\n\n# Run specific test\npython -m pytest test/test_mock_machine.py::test_i2c_scan\n```\n\n## Contributing\n\nContributions are welcome! Please feel free to submit a Pull Request.\n\n## License\n\nThis project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.\n\nCopyright (c) 2020-2024 Planet Innovation Pty Ltd\n"
}