#!/usr/bin/env python
# encoding: utf-8
"""
Created by Brendan Erwin on 2008-05-21.
Copyright (c) 2008 Brendan Erwin. All rights reserved.

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

"""

import sys
import getopt
import RepRapArduinoSerialSender
import MAppCommon

help_message = '''
M-App for EMC RepStrap.

M108 - Extruder set motor PWM
	Sets the extruder motor control PWM. 

Usage: M108 P_VALUE [-v]
	-v : Verbose
	
Example: M108 255 
	Sets the extruder motor control PWM to 255 (full blast).

------------------------------------------------------------------

Copyright (C) 2008 Brendan Erwin

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  
02110-1301, USA.
'''


class Usage(Exception):
	def __init__(self, msg):
		self.msg = msg


def main(argv=None):

	gotPwmValue = False
	requestedPwmValue = 0
	verbose = False

	if argv is None:
		argv = sys.argv
		
	try:
		#Remove default "null" values
		argv = filter(MAppCommon.filterDefault, argv)
		
		#Get value of P
		if len(argv) > 1:	
			requestedPwmValue = int(float(argv[1]))
			gotPwmValue = True
			argv.remove(argv[1])
		
		if gotPwmValue == False:
			raise Usage(help_message)			
			
		try:
			opts, args = getopt.getopt(argv[1:], "ho:v", ["help", "output="])
		except getopt.error, msg:
			raise Usage(msg)
	
		# option processing
		for option, value in opts:
			if option == "-v":
				verbose = True
			if option in ("-h", "--help"):
				raise Usage(help_message)
	
	except Usage, err:
		#print >> sys.stderr, sys.argv[0].split("/")[-1] + ": " + str(err.msg)
		print >> sys.stderr, str(err.msg)
		print >> sys.stderr, "For help use --help"
		return 2
		
	if verbose:
		print >> sys.stdout, "Got PWM value of: " + str(requestedPwmValue)
	
	sender = RepRapArduinoSerialSender.RepRapArduinoSerialSender(verbose)
	
	data = "M108 S" + str(requestedPwmValue)
	
	sender.writeToArduino(data)
	
	return 0

if __name__ == "__main__":
	sys.exit(main())
