#!/usr/bin/env python

# M code to set Fan speed
# M106 in your G code program will run the python code in this file, 
# passing the P and Q variables as command line arguments.

import sys
import hal

Mcode = 'M106'
usage = 'Fan speed'
pin = 'SP'
SP_signal = 'ext0-cooling-SP'

h = hal.component(Mcode)

print ("Usage: " + Mcode + " Pn - where n is the " + usage)

# P and Q command line arguments
P = sys.argv[1]
Q = sys.argv[2]

#Reprap fan speed is between 0 and 255 (0% to 100%) therefore we need to scale the setpoint

SP = (float(P) / 255) * 100

print ('Setting ' + usage + ' to ' + str(SP) + '%')

h.newpin(pin, hal.HAL_FLOAT, hal.HAL_OUT)
hal.connect(Mcode + '.' + pin, SP_signal)

h[pin] = SP

