aboutsummaryrefslogtreecommitdiffstats
path: root/klippy/extras/canbus_ids.py
blob: f0c9601677d6e926702bfcc451b4813e7d4810e6 (plain)
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
# Support for tracking canbus node ids
#
# Copyright (C) 2021  Kevin O'Connor <kevin@koconnor.net>
#
# This file may be distributed under the terms of the GNU GPLv3 license.

NODEID_FIRST = 4


class PrinterCANBus:
    def __init__(self, config):
        self.printer = config.get_printer()
        self.ids = {}

    def add_uuid(self, config, canbus_uuid, canbus_iface):
        if canbus_uuid in self.ids:
            raise config.error("Duplicate canbus_uuid")
        new_id = len(self.ids) + NODEID_FIRST
        self.ids[canbus_uuid] = new_id
        return new_id

    def get_nodeid(self, canbus_uuid):
        if canbus_uuid not in self.ids:
            raise self.printer.config_error("Unknown canbus_uuid %s" % (canbus_uuid,))
        return self.ids[canbus_uuid]


def load_config(config):
    return PrinterCANBus(config)