Project

General

Profile

Link to exercise 4 solution

from Framework4.Control.Core.CObject import CObjectBase, Signal, Slot
import logging
import math

class Foils(CObjectBase):
  signals = [Signal("foilChanged"), Signal("stateChanged")]
  slots = [Slot("get_positions"), Slot("get_current_foil")]

  def init(self):
    self.current_foil = None

    self.tolerance = float(self.config["/object/foils_positions/@tolerance"][0])

    position_names = self.config["/object/foils_positions/position/@name"]
    position_offsets = [float(x) for x in self.config["/object/foils_positions/position/@offset"]]
    self.positions = zip(position_names, position_offsets)

    self.foil_motion_motor = self.objects["foil_motion"]
    self.foil_motion_motor.connect("positionChanged", self.foil_motor_moved)
    self.foil_motion_motor.connect("stateChanged", self.foil_motor_state_changed)

  def get_positions(self):
    return self.positions

  def get_current_foil(self):
    for position_name, offset in self.positions:
      if math.fabs(self.foil_motion_motor.position() - offset) < self.tolerance:
        return position_name

  def foil_motor_moved(self, new_position):
     current_foil = self.get_current_foil()

     if self.current_foil != current_foil:
       self.current_foil = current_foil
       logging.debug("%s: emitting foilChanged signal with data = %s", self.name(), current_foil)
       self.emit("foilChanged", current_foil)

  def foil_motor_state_changed(self, new_state):
     logging.debug("%s: emitting stateChanged signal with data = %s", self.name(), new_state)
     self.emit("stateChanged", new_state)