0
0
mirror of https://github.com/Doodle3D/doodle3d-firmware.git synced 2024-06-29 04:31:22 +02:00

Initial API replacement to use new printer driver (limited functionality).

This commit is contained in:
Wouter R 2013-09-17 00:14:42 +02:00
parent c1fa030b97
commit 8a7162330b

View File

@ -2,6 +2,7 @@ local lfs = require('lfs')
local log = require('util.logger') local log = require('util.logger')
local utils = require('util.utils') local utils = require('util.utils')
local settings = require('util.settings') local settings = require('util.settings')
local printDriver = require('print3d')
local M = { local M = {
isApi = true isApi = true
@ -14,21 +15,29 @@ local PROGRESS_FILE = 'progress2.out'
local COMMAND_FILE = 'command.in' local COMMAND_FILE = 'command.in'
local GCODE_TMP_FILE = 'combined.gc' local GCODE_TMP_FILE = 'combined.gc'
-- returns full path + ultifi path or nil
local function printerExists(id)
if id == nil then return nil end --NEW
--returns a printer instance or nil (and sets error state on response in the latter case)
local path = '/dev/ttyACM' .. id local function createPrinterOrFail(deviceId, response)
local upath = ULTIFI_BASE_PATH .. '/ttyACM' .. id local msg,printer = nil, nil
if utils.exists(path) then return path,upath end
if deviceId and deviceId ~= "" then
path = '/dev/ttyUSB' .. id printer,msg = printDriver.getPrinter(deviceId)
upath = ULTIFI_BASE_PATH .. '/ttyUSB' .. id else
if utils.exists(path) then return path,upath end msg = "missing device ID"
end
return nil
if not printer then
response:setError("could not open printer driver (" .. msg .. ")")
response:addData('id', deviceId)
return nil
end
return printer
end end
-- returns printerId,devicePath,ultifiPath or nil if printer does not exist -- returns printerId,devicePath,ultifiPath or nil if printer does not exist
-- when nil is returned, response has already been set as an error -- when nil is returned, response has already been set as an error
local function getPrinterDataOrFail(request, response) local function getPrinterDataOrFail(request, response)
@ -38,47 +47,64 @@ local function getPrinterDataOrFail(request, response)
response:setError("missing id argument") response:setError("missing id argument")
return nil return nil
end end
local devpath,ultipath = printerExists(id) local devpath,ultipath = printerExists(id)
if not devpath then if not devpath then
response:setError("printer does not exist") response:setError("printer does not exist")
response:addData('id', id) response:addData('id', id)
return nil return nil
end end
return id,devpath,ultipath return id,devpath,ultipath
end end
-- returns full path + ultifi path or nil
local function printerExists(id)
if id == nil then return nil end
local path = '/dev/ttyACM' .. id
local upath = ULTIFI_BASE_PATH .. '/ttyACM' .. id
if utils.exists(path) then return path,upath end
path = '/dev/ttyUSB' .. id
upath = ULTIFI_BASE_PATH .. '/ttyUSB' .. id
if utils.exists(path) then return path,upath end
return nil
end
-- assumes printerPath exists, returns true if successful, false if command file already exists and is non-empty (i.e. printer busy), -- assumes printerPath exists, returns true if successful, false if command file already exists and is non-empty (i.e. printer busy),
-- nil+err if file could not be opened -- nil+err if file could not be opened
local function sendGcode(printerPath, gcode) local function sendGcode(printerPath, gcode)
local cmdPath = printerPath .. '/' .. COMMAND_FILE local cmdPath = printerPath .. '/' .. COMMAND_FILE
local f,msg = io.open(cmdPath, 'a+') -- 'a+' is important, do not overwrite current contents in any case local f,msg = io.open(cmdPath, 'a+') -- 'a+' is important, do not overwrite current contents in any case
if not f then return nil,msg end if not f then return nil,msg end
if utils.fileSize(f) > 0 then return false end if utils.fileSize(f) > 0 then return false end
log:debug("sending " .. gcode:len() .. " bytes of gcode") log:debug("sending " .. gcode:len() .. " bytes of gcode")
f:write(gcode) f:write(gcode)
f:close() f:close()
return true return true
end end
local function addToGcodeFile(printerPath, gcode) local function addToGcodeFile(printerPath, gcode)
if not gcode or type(gcode) ~= 'string' then return nil,"missing gcode data" end if not gcode or type(gcode) ~= 'string' then return nil,"missing gcode data" end
local gtFile = printerPath .. '/' .. GCODE_TMP_FILE local gtFile = printerPath .. '/' .. GCODE_TMP_FILE
local gcf,msg = io.open(gtFile, 'a+') local gcf,msg = io.open(gtFile, 'a+')
if not gcf then return nil,msg end if not gcf then return nil,msg end
log:debug("appending " .. gcode:len() .. " bytes of gcode to " .. gtFile) log:debug("appending " .. gcode:len() .. " bytes of gcode to " .. gtFile)
gcf:write(gcode) gcf:write(gcode)
gcf:write("\n") gcf:write("\n")
gcf:close() gcf:close()
return true return true
end end
@ -88,14 +114,14 @@ local function printGcodeFile(printerPath)
local gtFile = printerPath .. '/' .. GCODE_TMP_FILE local gtFile = printerPath .. '/' .. GCODE_TMP_FILE
local cmdPath = printerPath .. '/' .. COMMAND_FILE local cmdPath = printerPath .. '/' .. COMMAND_FILE
local cmdf,msg = io.open(cmdPath, 'a+') -- 'a+' is important, do not overwrite current contents in any case local cmdf,msg = io.open(cmdPath, 'a+') -- 'a+' is important, do not overwrite current contents in any case
if not cmdf then return nil,msg end if not cmdf then return nil,msg end
if utils.fileSize(cmdf) > 0 then return false end if utils.fileSize(cmdf) > 0 then return false end
log:debug("starting print of gcode in " .. gtFile) log:debug("starting print of gcode in " .. gtFile)
cmdf:write('(SENDFILE=' .. gtFile) cmdf:write('(SENDFILE=' .. gtFile)
cmdf:close() cmdf:close()
return true return true
end end
@ -105,28 +131,28 @@ end
local function stopGcodeFile(printerPath) local function stopGcodeFile(printerPath)
local cmdPath = printerPath .. '/' .. COMMAND_FILE local cmdPath = printerPath .. '/' .. COMMAND_FILE
local cmdf,msg = io.open(cmdPath, 'a+') -- 'a+' is important, do not overwrite current contents in any case local cmdf,msg = io.open(cmdPath, 'a+') -- 'a+' is important, do not overwrite current contents in any case
if not cmdf then return nil,msg end if not cmdf then return nil,msg end
if utils.fileSize(cmdf) > 0 then return false end if utils.fileSize(cmdf) > 0 then return false end
log:debug("stopping print of gcode") log:debug("stopping print of gcode")
cmdf:write('(CANCELFILE') cmdf:write('(CANCELFILE')
cmdf:close() cmdf:close()
return true return true
end end
local function isBusy(printerPath) local function isBusy(printerPath)
local cmdPath = printerPath .. '/' .. COMMAND_FILE local cmdPath = printerPath .. '/' .. COMMAND_FILE
if not utils.exists(cmdPath) then return false end if not utils.exists(cmdPath) then return false end
local f,msg = io.open(cmdPath, 'r') local f,msg = io.open(cmdPath, 'r')
if not f then return nil,msg end if not f then return nil,msg end
local size = utils.fileSize(f) local size = utils.fileSize(f)
f:close() f:close()
return size > 0 return size > 0
end end
@ -137,73 +163,47 @@ function M._global(request, response)
end end
--requires id(int) --requires id(int)
--accepts with_raw(bool) to include raw printer response --accepts with_raw(bool) to include raw printer response (currently not implemented)
function M.temperature(request, response) function M.temperature(request, response)
local withRaw = utils.toboolean(request:get("with_raw")) local argId = request:get("id")
local printer,msg = createPrinterOrFail(argId, response)
local argId,devpath,ultipath = getPrinterDataOrFail(request, response) if not printer then return end
if argId == nil then return end
local f = io.open(ultipath .. '/' .. TEMPERATURE_FILE)
if not f then
response:setError("could not open temperature file")
response:addData('id', argId)
return
end
local tempText = f:read('*all')
f:close()
local hotend, hotendTarget, bed, bedTarget = tempText:match('T:(.*)%s+/(.*)%s+B:(.*)%s/(.*)%s+@.*')
response:setSuccess() response:setSuccess()
if withRaw then response:addData('raw', tempText) end response:addData('id', argId)
response:addData('hotend', printer:getTemperature())
-- After pressing print it waits until it's at the right temperature.
-- it then stores temperature in the following format
-- T:204.5 E:0 W:?
if(hotend == nil) then
local hotend = tempText:match('T:([%d%.]*).*')
response:addData('hotend', hotend)
else
response:addData('hotend', hotend)
response:addData('bed', bed)
response:addData('hotend_target', hotendTarget)
response:addData('bed_target', bedTarget)
end
-- get last modified time
local file_attr = lfs.attributes(ultipath .. '/' .. TEMPERATURE_FILE)
local last_mod = file_attr.modification
local last_mod = os.difftime (os.time(),last_mod)
response:addData('last_mod', last_mod)
-- TODO: reimplement the following fields
-- if withRaw then response:addData('raw', tempText) end
-- response:addData('bed', printer:getBedTemperature())
-- response:addData('hotend_target', printer:getHotendTargetTemperature())
-- response:addData('bed_target', printer:getBedTargetTemperature())
-- response:addData('last_mod', printer:getLastTemperatureUpdate())
end end
--requires id(int) --requires id(int)
function M.progress(request, response) function M.progress(request, response)
local argId,devpath,ultipath = getPrinterDataOrFail(request, response) local argId,devpath,ultipath = getPrinterDataOrFail(request, response)
if argId == nil then return end if argId == nil then return end
local f = io.open(ultipath .. '/' .. PROGRESS_FILE) local f = io.open(ultipath .. '/' .. PROGRESS_FILE)
if not f then if not f then
response:setError("could not open progress file") response:setError("could not open progress file")
response:addData('id', argId) response:addData('id', argId)
return return
end end
local tempText = f:read('*all') local tempText = f:read('*all')
f:close() f:close()
local currentLine,numLines = tempText:match('(%d+)/(%d+)') local currentLine,numLines = tempText:match('(%d+)/(%d+)')
response:setSuccess() response:setSuccess()
if(currentLine == nil) then if(currentLine == nil) then
response:addData('printing', false) response:addData('printing', false)
else else
response:addData('printing', true) response:addData('printing', true)
response:addData('current_line', currentLine) response:addData('current_line', currentLine)
response:addData('num_lines', numLines) response:addData('num_lines', numLines)
@ -221,9 +221,9 @@ end
function M.busy(request, response) function M.busy(request, response)
local argId,devpath,ultipath = getPrinterDataOrFail(request, response) local argId,devpath,ultipath = getPrinterDataOrFail(request, response)
if argId == nil then return end if argId == nil then return end
local b,msg = isBusy(ultipath) local b,msg = isBusy(ultipath)
if b == nil then if b == nil then
response:setError("could not determine printer state") response:setError("could not determine printer state")
response:addData('msg', msg) response:addData('msg', msg)
@ -243,10 +243,10 @@ end
function M.heatup_POST(request, response) function M.heatup_POST(request, response)
local argId,devpath,ultipath = getPrinterDataOrFail(request, response) local argId,devpath,ultipath = getPrinterDataOrFail(request, response)
if argId == nil then return end if argId == nil then return end
local gcode = settings.get('printer.autoWarmUpCommand') .. "\n" local gcode = settings.get('printer.autoWarmUpCommand') .. "\n"
local rv,msg = sendGcode(ultipath, gcode) local rv,msg = sendGcode(ultipath, gcode)
if rv then if rv then
response:setSuccess() response:setSuccess()
elseif rv == false then elseif rv == false then
@ -280,26 +280,26 @@ end
function M.print_POST(request, response) function M.print_POST(request, response)
local argId,devpath,ultipath = getPrinterDataOrFail(request, response) local argId,devpath,ultipath = getPrinterDataOrFail(request, response)
if argId == nil then return end if argId == nil then return end
local gtFile = ultipath .. '/' .. GCODE_TMP_FILE local gtFile = ultipath .. '/' .. GCODE_TMP_FILE
local argGcode = request:get("gcode") local argGcode = request:get("gcode")
local argIsFirst = utils.toboolean(request:get("first")) local argIsFirst = utils.toboolean(request:get("first"))
local argIsLast = utils.toboolean(request:get("last")) local argIsLast = utils.toboolean(request:get("last"))
if argGcode == nil or argGcode == '' then if argGcode == nil or argGcode == '' then
response:setError("missing gcode argument") response:setError("missing gcode argument")
return return
end end
if argIsFirst == true then if argIsFirst == true then
log:debug("clearing all gcode in " .. gtFile) log:debug("clearing all gcode in " .. gtFile)
response:addData('gcode_clear',true) response:addData('gcode_clear',true)
os.remove(gtFile) os.remove(gtFile)
end end
local rv,msg local rv,msg
rv,msg = addToGcodeFile(ultipath, argGcode) rv,msg = addToGcodeFile(ultipath, argGcode)
if rv == nil then if rv == nil then
response:setError("could not add gcode") response:setError("could not add gcode")
@ -309,10 +309,10 @@ function M.print_POST(request, response)
--NOTE: this does not report the number of lines, but only the block which has just been added --NOTE: this does not report the number of lines, but only the block which has just been added
response:addData('gcode_append',argGcode:len()) response:addData('gcode_append',argGcode:len())
end end
if argIsLast == true then if argIsLast == true then
rv,msg = printGcodeFile(ultipath) rv,msg = printGcodeFile(ultipath)
if rv then if rv then
response:setSuccess() response:setSuccess()
response:addData('gcode_print',true) response:addData('gcode_print',true)