// Yep, this is actually -*- c++ -*- //initialize the firmware to default state. inline void init_commands() { finishedCommands = 0; } //handle our packets. void process_host_packets() { unsigned long start = millis(); unsigned long end = start + PACKET_TIMEOUT; #ifdef ENABLE_COMMS_DEBUG //Serial.print("IN: "); #endif //do we have a finished packet? while (!hostPacket.isFinished()) { if (Serial.available() > 0) { digitalWrite(DEBUG_PIN, HIGH); //grab a byte and process it. byte d = Serial.read(); hostPacket.process_byte(d); #ifdef ENABLE_COMMS_DEBUG //Serial.print(d, HEX); //Serial.print(" "); #endif serial_rx_count++; //keep us goign while we have data coming in. start = millis(); end = start + PACKET_TIMEOUT; if (hostPacket.getResponseCode() == RC_CRC_MISMATCH) { //host_crc_errors++; #ifdef ENABLE_COMMS_DEBUG Serial.println("Host CRC Mismatch"); #endif } digitalWrite(DEBUG_PIN, LOW); } //are we sure we wanna break mid-packet? //have we timed out? if (millis() >= end) { #ifdef ENABLE_COMMS_DEBUG Serial.println("Host timeout"); #endif break; } } if (hostPacket.isFinished()) { serial_packet_count++; byte b = hostPacket.get_8(0); // top bit high == bufferable command packet (eg. #128-255) if (b & 0x80) { //do we have room? if (commandBuffer.remainingCapacity() >= hostPacket.getLength()) { //okay, throw it in the buffer. for (byte i=0; i 0) { /* Serial.print("size: "); Serial.println(commandBuffer.size(), DEC); */ //peek at our command. cmd = commandBuffer[0]; // Do it later if it's a point queueing command and we don't have room yet. if ((cmd == HOST_CMD_QUEUE_POINT_ABS /* || cmd == HOST_CMD_QUEUE_POINT_INC */) && pointBuffer.remainingCapacity() < POINT_SIZE) { return; } //okay, which command are we handling? cmd = commandBuffer.remove_8(); /* Serial.print("cmd: "); Serial.println(cmd, DEC); */ switch(cmd) { case HOST_CMD_QUEUE_POINT_ABS: x = (long)commandBuffer.remove_32(); y = (long)commandBuffer.remove_32(); z = (long)commandBuffer.remove_32(); step_delay = (unsigned long)commandBuffer.remove_32(); queue_absolute_point(x, y, z, step_delay); break; case HOST_CMD_SET_POSITION: wait_until_target_reached(); //dont want to get hasty. cli(); target_steps.x = current_steps.x = (long)commandBuffer.remove_32(); target_steps.y = current_steps.y = (long)commandBuffer.remove_32(); target_steps.z = current_steps.z = (long)commandBuffer.remove_32(); sei(); break; case HOST_CMD_FIND_AXES_MINIMUM: wait_until_target_reached(); //dont want to get hasty. //no dda interrupts. disableTimer1Interrupt(); //which ones are we going to? flags = commandBuffer.remove_8(); //find them! seek_minimums( flags & 1, flags & 2, flags & 4, commandBuffer.remove_32(), commandBuffer.remove_16()); //turn on point seeking agian. enableTimer1Interrupt(); break; case HOST_CMD_FIND_AXES_MAXIMUM: wait_until_target_reached(); //dont want to get hasty. //no dda interrupts. disableTimer1Interrupt(); //find them! seek_maximums( flags & 1, flags & 2, flags & 4, commandBuffer.remove_32(), commandBuffer.remove_16()); //turn on point seeking agian. enableTimer1Interrupt(); break; case HOST_CMD_DELAY: wait_until_target_reached(); //dont want to get hasty. //take it easy. delay(commandBuffer.remove_32()); break; case HOST_CMD_CHANGE_TOOL: wait_until_target_reached(); //dont want to get hasty. //extruder, i choose you! select_tool(commandBuffer.remove_8()); break; case HOST_CMD_WAIT_FOR_TOOL: wait_until_target_reached(); //dont want to get hasty. //get your temp in gear, you lazy bum. //what tool / timeout /etc? currentToolIndex = commandBuffer.remove_8(); toolPingDelay = (unsigned int)commandBuffer.remove_16(); toolTimeout = (unsigned int)commandBuffer.remove_16(); //check to see if its ready now if (!is_tool_ready(currentToolIndex)) { //how often to ping? toolNextPing = millis() + toolPingDelay; toolTimeoutEnd = millis() + (toolTimeout * 1000); //okay, put us in ping-tool-until-ready mode commandMode = COMMAND_MODE_WAIT_FOR_TOOL; } break; case HOST_CMD_TOOL_COMMAND: wait_until_target_reached(); //dont want to get hasty. send_tool_command(); break; case HOST_CMD_ENABLE_AXES: wait_until_target_reached(); { unsigned char param = commandBuffer.remove_8(); bool x = (param & 0x01) != 0; bool y = (param & 0x02) != 0; bool z = (param & 0x04) != 0; if ((param & 0x80) != 0) { // enable axes enable_steppers(x,y,z); } else { // disable axes disable_steppers(x,y,z); } } default: hostPacket.unsupported(); } } }