void deviceI2cSlaveDebugHandleRawData(char header, InputStream* inputStream, OutputStream* outputStream) {
    // I2C Management
    if (header == COMMAND_I2C_DEBUG_SLAVE_DEBUG) {
        ackCommand(outputStream, I2C_SLAVE_DEBUG_DEVICE_HEADER, COMMAND_I2C_DEBUG_SLAVE_DEBUG);
        printI2cDebugBuffers();
    }
    else if (header == COMMANG_I2C_DEBUG_SLAVE_ADDRESS) {
        ackCommand(outputStream, I2C_SLAVE_DEBUG_DEVICE_HEADER, COMMANG_I2C_DEBUG_SLAVE_ADDRESS);
        unsigned char address = slaveDebugDeviceI2cBusConnection->i2cAddress;
        appendHex2(outputStream, address);
    }
    else if (header == COMMAND_I2C_DEBUG_SLAVE_ENABLE_DISABLE) {
        ackCommand(outputStream, I2C_SLAVE_DEBUG_DEVICE_HEADER, COMMAND_I2C_DEBUG_SLAVE_ENABLE_DISABLE);

        unsigned char enable = readHex2(inputStream);
        setDebugI2cEnabled(enable != 0);
    }
    else if (header == COMMAND_I2C_DEBUG_SLAVE_SEND_CHAR_I2C_TO_MASTER) {
        ackCommand(outputStream, I2C_SLAVE_DEBUG_DEVICE_HEADER, COMMAND_I2C_DEBUG_SLAVE_SEND_CHAR_I2C_TO_MASTER);

        int c = readHex2(inputStream);
        portableSlaveWriteI2C(slaveDebugDeviceI2cBusConnection, c);
    }
    else if (header == COMMAND_I2C_DEBUG_SLAVE_READ_CHAR_I2C_FROM_MASTER) {
        ackCommand(outputStream, I2C_SLAVE_DEBUG_DEVICE_HEADER, COMMAND_I2C_DEBUG_SLAVE_READ_CHAR_I2C_FROM_MASTER);
    
        char c = portableSlaveReadI2C(slaveDebugDeviceI2cBusConnection);
        appendHex2(outputStream, c);
    }
}
Esempio n. 2
0
void deviceTestHandleRawData(char header,
        InputStream* inputStream,
        OutputStream* outputStream) {
    if (header == COMMAND_TEST) {
        int arg1 = readHex2(inputStream);
		int arg2 = readHex2(inputStream);
        int result = arg1 + arg2;
        // data
        appendAck(outputStream);
        append(outputStream, COMMAND_TEST);
        appendHex2(outputStream, result);
    }
    /* TODO
    else if (header == COMMAND_NOTIFY_TEST) {
            int argument = readHex2(inputStream);
            argument *= 2;

            Buffer* buffer = getI2CSlaveOutputBuffer();
            OutputStream* i2cOutputStream = getOutputStream(buffer);

            // Add the value to I2C
            append(i2cOutputStream, COMMAND_NOTIFY_TEST);
            appendHex2(i2cOutputStream, argument);
		
            // Response to the call
            appendAck(outputStream);
            append(outputStream, COMMAND_NOTIFY_TEST);
            appendHex2(outputStream, argument);
    }
     */
}
void deviceRobotInfraredDetectorHandleRawData(char header,
        InputStream* inputStream,
        OutputStream* outputStream) {
	// Command to ask
    if (header == COMMAND_INFRARED_DETECTOR_DETECTION) {
        appendAck(outputStream);
        append(outputStream, COMMAND_INFRARED_DETECTOR_DETECTION);

		int type = readHex2(inputStream);
		BOOL hasDetected;
		if (type == DETECTOR_FORWARD_INDEX) {
			hasDetected = getRobotInfraredObstacleForward();
		}
		else {
			hasDetected = getRobotInfraredObstacleBackward();
		}
		
		// Send argument
		if (hasDetected) {
			appendHex2(outputStream, 1);
		}
		else {
			appendHex2(outputStream, 0);
		}
    }
}
Esempio n. 4
0
void deviceLCDHandleRawData(char commandHeader,
                            InputStream* inputStream,
                            OutputStream* outputStream) {
    if (commandHeader == COMMAND_LCD) {
        ackCommand(outputStream, LCD_DEVICE_HEADER, COMMAND_LCD);
        // print character
        int i;
        for (i = 0; i < LCD_CHAR_COUNT_BY_MESSAGE; i++) {
            int c = readHex2(inputStream);
            // we only valid write data
            if (c == '\0') {
                break;
            }
            writeLCDChar(c);
        }
    }
    else if (commandHeader == COMMAND_LCD_TEST) {
        ackCommand(outputStream, LCD_DEVICE_HEADER, COMMAND_LCD_TEST);
        writeLCDChar('H');
        writeLCDChar('E');
        writeLCDChar('L');
        writeLCDChar('L');
        writeLCDChar('O');
        writeLCDChar(' ');
        writeLCDChar('W');
        writeLCDChar('O');
        writeLCDChar('R');
        writeLCDChar('L');
        writeLCDChar('D');
    }
    else if (commandHeader == COMMAND_CLEAR_LCD) {
        ackCommand(outputStream, LCD_DEVICE_HEADER, COMMAND_CLEAR_LCD);
        clearScreen();
    }
}
Esempio n. 5
0
void devicePinHandleRawData(char header, InputStream* inputStream, OutputStream* outputStream) {
    if (header == COMMAND_SET_PIN_VALUE) {
        int pinIndex = readHex2(inputStream);
        int pinValue = readHex2(inputStream);
        appendAck(outputStream);
        setPinValue(pinIndex, pinValue);
        append(outputStream, COMMAND_SET_PIN_VALUE);
    } else if (header == COMMAND_GET_PIN_VALUE) {
        int pinIndex = readHex2(inputStream);
        appendAck(outputStream);

        int pinValue = getPinValue(pinIndex);

        // Response
        append(outputStream, COMMAND_GET_PIN_VALUE);
        appendHex2(outputStream, pinValue);
    }
}
Esempio n. 6
0
void mainBoardDeviceHandleNotification(const Device* device, const char commandHeader, InputStream* inputStream) {
    appendString(getDebugOutputStreamLogger(), "Notification ! commandHeader=");
    append(getDebugOutputStreamLogger(), commandHeader);
    appendCRLF(getDebugOutputStreamLogger());
    if (commandHeader == NOTIFY_TEST) {
        unsigned char value = readHex2(inputStream);
        appendStringAndDec(getDebugOutputStreamLogger(), "value=", value);
    }
}
void deviceRobotSonarDetectorHandleRawData(char commandHeader,
        InputStream* inputStream,
        OutputStream* outputStream) {

    if (commandHeader == COMMAND_SET_SONAR_STATUS) {
        sonarStatus = readHex2(inputStream);
        ackCommand(outputStream, ROBOT_SONAR_DETECTOR_DEVICE_HEADER, COMMAND_SET_SONAR_STATUS);
    }
}
Esempio n. 8
0
void readCharArray(InputStream* inputStream, char(*s)[], unsigned char length) {
    int i;
    char* sPointer = (char*) s;
    for (i = 0; i < length; i++) {
        char c = (char) readHex2(inputStream);
        *sPointer = c;
        sPointer++;
    }
}
Esempio n. 9
0
void deviceArm2012HandleRawData(char header,
        InputStream* inputStream,
        OutputStream* outputStream) {
    if (header == COMMAND_ARM_2012_UP) {
        int armIndex = readHex2(inputStream);
		upArm(armIndex);

        appendAck(outputStream);
        append(outputStream, COMMAND_ARM_2012_UP);

	
    } else if (header == COMMAND_ARM_2012_DOWN) {
        int armIndex = readHex2(inputStream);
		downArm(armIndex);

        appendAck(outputStream);
        append(outputStream, COMMAND_ARM_2012_DOWN);
    }
}
void deviceTemperatureSensorHandleRawData(unsigned char header, InputStream* inputStream, OutputStream* outputStream, OutputStream* notificationOutputStream){
    if (header == COMMAND_READ_TEMPERATURE_SENSOR) {
        ackCommand(outputStream, TEMPERATURE_SENSOR_DEVICE_HEADER, COMMAND_READ_TEMPERATURE_SENSOR);
        unsigned char value = (unsigned char) temperature->readSensorValue(temperature);
        appendHex2(outputStream, value);
    } else if (header == COMMAND_SET_TEMPERATURE_SENSOR_ALERT) {
        unsigned char temperatureSensorAlert = readHex2(inputStream);
        ackCommand(outputStream, TEMPERATURE_SENSOR_DEVICE_HEADER, COMMAND_SET_TEMPERATURE_SENSOR_ALERT);
        temperature->writeAlertLimit(temperature, temperatureSensorAlert);
    }
}
bool robotInfraredDetectorHasObstacle(enum InfraredDetectorGroupType type) {
    OutputStream* outputStream = getDriverRequestOutputStream();
    InputStream* inputStream = getDriverResponseInputStream();

    append(outputStream, ROBOT_INFRARED_DETECTOR_DEVICE_HEADER);
    append(outputStream, COMMAND_INFRARED_DETECTOR_DETECTION);
    appendHex2(outputStream, type);
    bool result = transmitFromDriverRequestBuffer();
    if (result) {
        int result = readHex2(inputStream);
        return result == 0x01;
    }
    return false;
}
bool clientDistributor2018CleanNext(int direction) {
    OutputStream* outputStream = getDriverRequestOutputStream();
    InputStream* inputStream = getDriverResponseInputStream();

    append(outputStream, LAUNCHER_2018_DEVICE_HEADER);
    append(outputStream, DISTRIBUTOR_LOAD_NEXT_BALL_COMMAND);
    appendHex2(outputStream, direction);

    bool result = transmitFromDriverRequestBuffer();

    // Read the distance of detection, but we don't care about
    readHex2(inputStream);

    return result;
}
Esempio n. 13
0
void deviceRelayHandleRawData(char commandHeader, InputStream* inputStream, OutputStream* outputStream) {
    if (commandHeader == COMMAND_SET_RELAY) {
        int relayIndex = readHex2(inputStream);
        unsigned char value = inputStream->readChar(inputStream);
        
        if (relayIndex == 0) {
            LATDbits.LATD8 = value;
        }
        else if (relayIndex == 1) {
            LATDbits.LATD9 = value;
        }
        appendAck(outputStream);
        append(outputStream, COMMAND_SET_RELAY);
    }
}
Esempio n. 14
0
BOOL sendStrategyNextStep() {
	appendString(getOutputStreamLogger(INFO), "sendStrategyNextStep\n");

    OutputStream* outputStream = getDriverRequestOutputStream();
    InputStream* inputStream = getDriverResponseInputStream();

    append(outputStream, COMMAND_STRATEGY_NEXT_STEP);

    BOOL result = transmitFromDriverRequestBuffer();
    if (result) {
        int result = readHex2(inputStream);
		return result;
    }
    return FALSE;
}
void deviceAirConditionningBoardHandleRawData(char header, InputStream* inputStream, OutputStream* outputStream) {
    if (header == COMMAND_AIR_CONDITIONNING) {
        int powerState = readHex2(inputStream);

        if (powerState != 0x00) {
            pwmServoAll(SERVO_SPEED, SERVO_VALUE_TOUCH);
            delaymSec(1000);
            pwmServoAll(SERVO_SPEED, SERVO_VALUE_STAND_BY);
        } else {
            pwmServoAll(SERVO_SPEED, SERVO_VALUE_TOUCH);
            delaymSec(1000);
            pwmServoAll(SERVO_SPEED, SERVO_VALUE_STAND_BY);
        }

        appendAck(outputStream);
        append(outputStream, COMMAND_AIR_CONDITIONNING);
    }
}
bool handleStreamInstruction(Buffer* inputBuffer,
                            Buffer* outputBuffer,
                            OutputStream* outputStream,
                            filterCharFunction* inputFilterChar,
                            filterCharFunction* outputFilterChar) {

    if (inputBuffer == NULL) {
        writeError(DRIVER_STREAM_LISTENER_INPUT_BUFFER_NULL);
        return false;
    }
    if (outputBuffer == NULL) {
        writeError(DRIVER_STREAM_LISTENER_OUTPUT_BUFFER_NULL);
        return false;
    }

    // Try to clear the buffer if needed ('z' char)
    if (clearBufferIfNeeded(inputBuffer)) {
        return false;
    }

    // We received data
    int inputBufferCount = getBufferElementsCount(inputBuffer);

    if (inputBufferCount > 0) {

        if (filterFirstNextChar(inputBuffer, inputFilterChar)) {
            return false;
        }

        // As there is clear of char filtering, we must reload the size of the buffer
        int bufferSize = getBufferElementsCount(inputBuffer);

        if (bufferSize < DEVICE_HEADER_LENGTH) {
            return false;
        }

        // Get the header
        unsigned char deviceHeader = bufferGetCharAtIndex(inputBuffer, DEVICE_HEADER_INDEX);

        // Manage the dispatcher specifier (3 chars : Ex j01 before real command ...)
        unsigned char specifyDispatcherLength = 0;
        if (deviceHeader == DATA_DISPATCHER_DEVICE_HEADER) {
            specifyDispatcherLength += DISPATCHER_COMMAND_AND_INDEX_HEADER_LENGTH;
        }

        // Check if enough data
        if (bufferSize < specifyDispatcherLength + DEVICE_AND_COMMAND_HEADER_LENGTH) {
            return false;
        }

        // Reload the deviceHeader to take the dispatcher specifier if any
        deviceHeader = bufferGetCharAtIndex(inputBuffer, specifyDispatcherLength + DEVICE_HEADER_INDEX);
        unsigned char commandHeader = bufferGetCharAtIndex(inputBuffer, specifyDispatcherLength + COMMAND_HEADER_INDEX);

        // find the device corresponding to this header. It must at least be declared in local or in remote !
        unsigned char dataSize = bufferSize - specifyDispatcherLength;
        const Device* device = deviceDataDispatcherFindDevice(deviceHeader, commandHeader, dataSize, DEVICE_MODE_INPUT);

        // if the device was not found
        if (device == NULL) {
            return false;
        }

        // At this moment, device Interface is found
        DeviceInterface* deviceInterface = device->deviceInterface;

        // We must send device specifyDispatcherLength + Header + commandHeader + data => + 2
        int dataToTransferCount = deviceInterface->deviceGetInterface(commandHeader, DEVICE_MODE_INPUT, false) + specifyDispatcherLength + DEVICE_AND_COMMAND_HEADER_LENGTH;

        if (bufferSize < dataToTransferCount) {
            return false;
        }

        // We must receive ack + device header + command header + data => + 3
        int dataToReceiveCount = deviceInterface->deviceGetInterface(commandHeader, DEVICE_MODE_OUTPUT, false) + ACK_LENGTH + DEVICE_AND_COMMAND_HEADER_LENGTH;

        InputStream* bufferedInputStream = getInputStream(inputBuffer);
        OutputStream* bufferedOutputStream = getOutputStream(outputBuffer);

        TransmitMode transmitMode = device->transmitMode;

        // we handle locally the request
        if (specifyDispatcherLength == 0 && transmitMode == TRANSMIT_LOCAL) {

            // We need the implementation for local mode
            DeviceDescriptor* deviceDescriptor = device->descriptor;
            if (deviceDescriptor == NULL) {
                writeError(NO_DEVICE_DESC_FOUND_FOR);
                append(getErrorOutputStreamLogger(), deviceHeader);
                append(getErrorOutputStreamLogger(), commandHeader);
                return false;
            }

            // remove the first chars corresponding to the device header and command Header
            bufferClearLastChars(inputBuffer, DEVICE_AND_COMMAND_HEADER_LENGTH);

            // Call to the device
            deviceDescriptor->deviceHandleRawData(commandHeader, bufferedInputStream, bufferedOutputStream);

        } // we forward the request through Remote Operation with Dispatcher
        else if (specifyDispatcherLength > 0 || transmitMode == TRANSMIT_I2C || transmitMode == TRANSMIT_UART || transmitMode == TRANSMIT_ZIGBEE) {

            // Find dispatcher
            DriverDataDispatcher* dispatcher = NULL;

            if (specifyDispatcherLength > 0) {
                bufferClearLastChars(inputBuffer, DEVICE_HEADER_LENGTH);
                unsigned char dispatcherIndex = readHex2(bufferedInputStream);

                dispatcher = getDriverDataDispatcherByIndex(dispatcherIndex);
                if (dispatcher == NULL) {
                    writeError(NO_DISPATCHER_FOUND);
                    OutputStream* errorOutputStream = getErrorOutputStreamLogger();
                    appendStringAndDec(errorOutputStream, ", dispatcherIndex=", dispatcherIndex);
                    return false;
                }
            }
            else {
                TransmitMode transmitMode = device->transmitMode;
                int address = device->address;

                dispatcher = getDriverDataDispatcherByTransmitMode(transmitMode, address);
                
                if (dispatcher == NULL) {
                    writeError(NO_DISPATCHER_FOUND);
                    OutputStream* errorOutputStream = getErrorOutputStreamLogger();
                    appendStringAndDec(errorOutputStream, ", transmitMode=", transmitMode);
                    append(errorOutputStream, '(');
                    appendString(errorOutputStream, getTransmitModeAsString(transmitMode));
                    append(errorOutputStream, ')');
                    appendStringAndDec(errorOutputStream, ", addr=", address);
                    return false;
                }
            }

            // copy Driver buffer with remote Call
            dispatcher->driverDataDispatcherTransmitData(dispatcher,
                    inputBuffer,
                    outputBuffer,
                    dataToTransferCount,
                    dataToReceiveCount
                    );
        }
        
        // In All Cases (Local / I2C / UART / Zigbee ...)

        // Copy the data from bufferOutputStream to the outputStream
        if (outputStream != NULL) {
            copyInputToOutputStream(&(outputBuffer->inputStream), outputStream, outputFilterChar, dataToReceiveCount);
        }
        return true;
    }
    return false;
}
Esempio n. 17
0
void deviceTimerHandleRawData(char commandHeader, InputStream* inputStream, OutputStream* outputStream) {
    if (commandHeader == COMMAND_TIMER_LIST) {
        printTimerList(getInfoOutputStreamLogger(), getTimerList());
        ackCommand(outputStream, TIMER_DEVICE_HEADER, COMMAND_TIMER_LIST);
    }    
    else if (commandHeader == COMMAND_TIMER_COUNT) {
        ackCommand(outputStream, TIMER_DEVICE_HEADER, COMMAND_TIMER_COUNT);
        unsigned timerCount = getTimerCount();
        appendHex2(outputStream, timerCount);
    }
    else if (commandHeader == COMMAND_TIMER_READ) {
        ackCommand(outputStream, TIMER_DEVICE_HEADER, COMMAND_TIMER_READ);
        unsigned char timerIndex = readHex2(inputStream);
        Timer* timer = getTimerByIndex(timerIndex);
        appendHex2(outputStream, timerIndex);
        appendSeparator(outputStream);
        appendHex2(outputStream, timer->timerCode);
        appendSeparator(outputStream);
        appendHex4(outputStream, timer->timeDiviser);
        appendSeparator(outputStream);
        appendHex4(outputStream, timer->timeInternalCounter);
        appendSeparator(outputStream);
        appendHex6(outputStream, timer->time);
        appendSeparator(outputStream);
        appendHex6(outputStream, timer->markTime);
        appendSeparator(outputStream);
        appendBool(outputStream, timer->enabled);
    }
    // Enable / Tisable
    else if (commandHeader == COMMAND_TIMER_ENABLE_DISABLE) {
        unsigned char timerIndex = readHex2(inputStream);
        Timer* timer = getTimerByIndex(timerIndex);
        
        checkIsSeparator(inputStream);
        
        unsigned enableAsChar = readHex(inputStream);
        bool enabled = enableAsChar != 0;
        timer->enabled = enabled;
        ackCommand(outputStream, TIMER_DEVICE_HEADER, COMMAND_TIMER_ENABLE_DISABLE);
    }
    // Mark
    else if (commandHeader == COMMAND_TIMER_MARK) {
        unsigned char timerIndex = readHex2(inputStream);
        Timer* timer = getTimerByIndex(timerIndex);
        unsigned long time = markTimer(timer);
        ackCommand(outputStream, TIMER_DEVICE_HEADER, COMMAND_TIMER_MARK);
        appendHex6(outputStream, time);
    }
    else if (commandHeader == COMMAND_TIMER_TIME_SINCE_LAST_MARK) {
        unsigned char timerIndex = readHex2(inputStream);
        Timer* timer = getTimerByIndex(timerIndex);
        unsigned long value = getTimeSinceLastMark(timer);
        ackCommand(outputStream, TIMER_DEVICE_HEADER, COMMAND_TIMER_TIME_SINCE_LAST_MARK);
        appendHex6(outputStream, value);
    }
    else if (commandHeader == COMMAND_TIMER_TIMEOUT) {
        unsigned char timerIndex = readHex2(inputStream);
        checkIsSeparator(inputStream);
        unsigned long timeToCheck = (unsigned long) readHex6(inputStream);
        Timer* timer = getTimerByIndex(timerIndex);
        bool value = timeout(timer, timeToCheck);
        ackCommand(outputStream, TIMER_DEVICE_HEADER, COMMAND_TIMER_TIMEOUT);
        appendHex2(outputStream, timerIndex);
        appendSeparator(outputStream);
        appendBool(outputStream, value);
    }
    // Demo
    else if (commandHeader == COMMAND_TIMER_DEMO) {
        Timer* timer = getTimerByCode(DEMO_TIMER_INDEX);
        if (timer == NULL) {
            timer = addTimerDemo();
        }
		// Timer could be null when adding the timerDemo because of limit, we don't want any crash !
		if (timer != NULL) {
			unsigned enableAsChar = readHex(inputStream);
			bool enabled = enableAsChar != 0;
			timer->enabled = enabled;
		}
        ackCommand(outputStream, TIMER_DEVICE_HEADER, COMMAND_TIMER_DEMO);
    }
}
Esempio n. 18
0
void deviceServoHandleRawData(unsigned char commandHeader, InputStream* inputStream, OutputStream* outputStream, OutputStream* notificationOutputStream) {
    // WRITE COMMANDS
    if (commandHeader == SERVO_COMMAND_WRITE) {
        ServoList* servoList = getServoDeviceServoList();
        unsigned int servoIndex = readHex2(inputStream);
        checkIsSeparator(inputStream);
        unsigned int servoSpeed = readHex2(inputStream);
        checkIsSeparator(inputStream);
        unsigned int servoValue = readHex4(inputStream);
        if (servoIndex == SERVO_ALL_INDEX) {
            pwmServoAll(servoList, servoSpeed, servoValue);
        } else {
            Servo* servo = getServo(servoList, servoIndex);
            pwmServo(servo, servoSpeed, servoValue, false);
        }
        ackCommand(outputStream, SERVO_DEVICE_HEADER, SERVO_COMMAND_WRITE);
    }
    else if (commandHeader == SERVO_COMMAND_WRITE_MAX_SPEED_UNDER_LOAD) {
        ServoList* servoList = getServoDeviceServoList();
        unsigned int servoIndex = readHex2(inputStream);
        checkIsSeparator(inputStream);
        int servoMaxSpeedUnderLoad = readHex2(inputStream);
        Servo* servo = getServo(servoList, servoIndex);
        servo->maxSpeedUnderLoad = servoMaxSpeedUnderLoad;
        ackCommand(outputStream, SERVO_DEVICE_HEADER, SERVO_COMMAND_WRITE_MAX_SPEED_UNDER_LOAD);
    }
    else if (commandHeader == SERVO_COMMAND_WRITE_COMPACT) {
        unsigned int servoValue = readHex4(inputStream);
        ServoList* servoList = getServoDeviceServoList();
        pwmServoAll(servoList, PWM_SERVO_SPEED_MAX, servoValue);
        ackCommand(outputStream, SERVO_DEVICE_HEADER, SERVO_COMMAND_WRITE_COMPACT);
    }
    // ENABLE / DISABLE
    if (commandHeader == SERVO_COMMAND_WRITE_ENABLE_DISABLE) {
        ackCommand(outputStream, SERVO_DEVICE_HEADER, SERVO_COMMAND_WRITE_ENABLE_DISABLE);
        ServoList* servoList = getServoDeviceServoList();
        unsigned int servoIndex = readHex2(inputStream);
        checkIsSeparator(inputStream);
        bool enabled = readBool(inputStream);
        if (servoIndex == SERVO_ALL_INDEX) {
            servoEnableAll(servoList, enabled);
        }
        else {
            Servo* servo = getServo(servoList, servoIndex);
            pwmServoSetEnabled(servo, enabled);
        }
    }
    else if (commandHeader == SERVO_COMMAND_ENABLE_DISABLE_ALL) {
        ackCommand(outputStream, SERVO_DEVICE_HEADER, SERVO_COMMAND_ENABLE_DISABLE_ALL);
        ServoList* servoList = getServoDeviceServoList();
        bool enabled = readBool(inputStream);
        servoEnableAll(servoList, enabled);
    }
    // READ COMMANDS
    else if (commandHeader == SERVO_COMMAND_GET_COUNT) {
        ackCommand(outputStream, SERVO_DEVICE_HEADER, SERVO_COMMAND_GET_COUNT);
        ServoList* servoList = getServoDeviceServoList();
        unsigned int servoCount = getServoCount(servoList);
        appendHex2(outputStream, servoCount);

    }
    else if (commandHeader == SERVO_COMMAND_READ) {
        ackCommand(outputStream, SERVO_DEVICE_HEADER, SERVO_COMMAND_READ);
        unsigned int servoIndex = readHex2(inputStream);
        ServoList* servoList = getServoDeviceServoList();
        Servo* servo = getServo(servoList, servoIndex);
        unsigned int speed = pwmServoReadTargetSpeed(servo);
        unsigned int currentPosition = pwmServoReadCurrentPosition(servo);
        unsigned int targetPosition = pwmServoReadTargetPosition(servo);

        appendHex2(outputStream, servoIndex);
        appendSeparator(outputStream);
        appendHex2(outputStream, speed);
        appendSeparator(outputStream);
        appendHex4(outputStream, currentPosition);
        appendSeparator(outputStream);
        appendHex4(outputStream, targetPosition);
    }
    else if (commandHeader == SERVO_COMMAND_READ_SPEED) {
        unsigned int servoIndex = readHex2(inputStream);
        ServoList* servoList = getServoDeviceServoList();
        Servo* servo = getServo(servoList, servoIndex);
        unsigned int speed = pwmServoReadTargetSpeed(servo);

        ackCommand(outputStream, SERVO_DEVICE_HEADER, SERVO_COMMAND_READ_SPEED);
        appendHex2(outputStream, speed);
    }
    else if (commandHeader == SERVO_COMMAND_READ_MAX_SPEED_UNDER_LOAD) {
        unsigned int servoIndex = readHex2(inputStream);
        ServoList* servoList = getServoDeviceServoList();
        Servo* servo = getServo(servoList, servoIndex);
        unsigned int maxSpeedUnderLoad = pwmServoReadMaxSpeedUnderLoad(servo);

        ackCommand(outputStream, SERVO_DEVICE_HEADER, SERVO_COMMAND_READ_MAX_SPEED_UNDER_LOAD);
        appendHex2(outputStream, maxSpeedUnderLoad);
    }
    else if (commandHeader == SERVO_COMMAND_READ_CURRENT_POSITION) {
        unsigned int servoIndex = readHex2(inputStream);
        ServoList* servoList = getServoDeviceServoList();
        Servo* servo = getServo(servoList, servoIndex);
        int currentPosition = pwmServoReadCurrentPosition(servo);

        ackCommand(outputStream, SERVO_DEVICE_HEADER, SERVO_COMMAND_READ_CURRENT_POSITION);
        appendHex4(outputStream, currentPosition);
    }
    else if (commandHeader == SERVO_COMMAND_READ_TARGET_POSITION) {
        unsigned int servoIndex = readHex2(inputStream);
        ServoList* servoList = getServoDeviceServoList();
        Servo* servo = getServo(servoList, servoIndex);
        int targetPosition = pwmServoReadTargetPosition(servo);

        ackCommand(outputStream, SERVO_DEVICE_HEADER, SERVO_COMMAND_READ_TARGET_POSITION);
        appendHex4(outputStream, targetPosition);
    }
    // DEBUG COMMANDS
    else if (commandHeader == SERVO_COMMAND_TEST) {
        ServoList* servoList = getServoDeviceServoList();
        testAllPwmServos(servoList);
        ackCommand(outputStream, SERVO_DEVICE_HEADER, SERVO_COMMAND_TEST);
    }
    else if (commandHeader == SERVO_COMMAND_GET_TIME_TO_REACH_UNDER_LOAD) {
        unsigned int servoIndex = readHex2(inputStream);
        checkIsSeparator(inputStream);
        unsigned int servoTargetPosition = readHex4(inputStream);

        ServoList* servoList = getServoDeviceServoList();
        Servo* servo = getServo(servoList, servoIndex);

        unsigned int timeToReachUnderLoad = pwmServoComputeTimeMilliSecondsToReachTargetPosition(servo, servoTargetPosition);
        ackCommand(outputStream, SERVO_DEVICE_HEADER, SERVO_COMMAND_GET_TIME_TO_REACH_UNDER_LOAD);
        appendHex4(outputStream, timeToReachUnderLoad);
    }
    else if (commandHeader == SERVO_COMMAND_DEBUG) {
        ServoList* servoList = getServoDeviceServoList();
        printServoList(getInfoOutputStreamLogger(), servoList);
        ackCommand(outputStream, SERVO_DEVICE_HEADER, SERVO_COMMAND_DEBUG);
    }
}
Esempio n. 19
0
void deviceEepromHandleRawData(char commandHeader, InputStream* inputStream, OutputStream* outputStream) {
    _deviceEepromCheckInitialized();
    if (commandHeader == COMMAND_RELOAD_EEPROM) {
        ackCommand(outputStream, EEPROM_DEVICE_HEADER, COMMAND_RELOAD_EEPROM);
        eeprom_->eepromLoad(eeprom_);
    }
    else if (commandHeader == COMMAND_DUMP_TO_FILE_EEPROM) {
        ackCommand(outputStream, EEPROM_DEVICE_HEADER, COMMAND_DUMP_TO_FILE_EEPROM);
        eeprom_->eepromDump(eeprom_);
    }
    else if (commandHeader == COMMAND_DUMP_TO_LOG_OUTPUT_STREAM_EEPROM) {
        ackCommand(outputStream, EEPROM_DEVICE_HEADER, COMMAND_DUMP_TO_LOG_OUTPUT_STREAM_EEPROM);
        OutputStream* debugOutputStream = getInfoOutputStreamLogger();
        dumpEepromToOutputStream(eeprom_, debugOutputStream, 0, eeprom_->maxIndex);
    }
	else if (commandHeader == COMMAND_DUMP_PARTIAL_CONTENT_TO_LOG_OUTPUT_STREAM_EEPROM) {
		ackCommand(outputStream, EEPROM_DEVICE_HEADER, COMMAND_DUMP_PARTIAL_CONTENT_TO_LOG_OUTPUT_STREAM_EEPROM);
		unsigned long startAddress = readHex4(inputStream);
		checkIsSeparator(inputStream);
		unsigned long length = readHex4(inputStream);
		OutputStream* debugOutputStream = getInfoOutputStreamLogger();
		dumpEepromToOutputStream(eeprom_, debugOutputStream, startAddress, startAddress + length);
	}
    else if (commandHeader == COMMAND_CLEAR_EEPROM) {
        ackCommand(outputStream, EEPROM_DEVICE_HEADER, COMMAND_CLEAR_EEPROM);
        unsigned long startAddress = readHex4(inputStream);
        checkIsSeparator(inputStream);
        unsigned long endAddress = readHex4(inputStream);
        clearEeprom(eeprom_, startAddress, endAddress);
    }
    else if (commandHeader == COMMAND_READ_BYTE_EEPROM) {
        ackCommand(outputStream, EEPROM_DEVICE_HEADER, COMMAND_READ_BYTE_EEPROM);
        unsigned long address = readHex4(inputStream);
        char value = eeprom_->eepromReadChar(eeprom_, address);
        appendHex2(outputStream, value);
    }
    else if (commandHeader == COMMAND_READ_INT_EEPROM) {
        ackCommand(outputStream, EEPROM_DEVICE_HEADER, COMMAND_READ_INT_EEPROM);
        unsigned long address = readHex4(inputStream);
        int value = eepromReadInt(eeprom_, address);
        appendHex4(outputStream, value);
    }
    else if (commandHeader == COMMAND_WRITE_BYTE_EEPROM) {
        ackCommand(outputStream, EEPROM_DEVICE_HEADER, COMMAND_WRITE_BYTE_EEPROM);
        unsigned long address = readHex4(inputStream);
        checkIsSeparator(inputStream);
        char data = readHex2(inputStream);
        eeprom_->eepromWriteChar(eeprom_, address, data);
    }
    else if (commandHeader == COMMAND_WRITE_INT_EEPROM) {
        ackCommand(outputStream, EEPROM_DEVICE_HEADER, COMMAND_WRITE_INT_EEPROM);
        unsigned long address = readHex4(inputStream);
        checkIsSeparator(inputStream);
        int data = readHex4(inputStream);
        eepromWriteInt(eeprom_, address, data);
    }
    else if (commandHeader == COMMAND_READ_BLOCK_EEPROM) {
        ackCommand(outputStream, EEPROM_DEVICE_HEADER, COMMAND_READ_BLOCK_EEPROM);
        unsigned long address = readHex4(inputStream);
        int index;
        for (index = 0; index < EEPROM_DEVICE_READ_BLOCK_LENGTH; index++) {
            char value = eeprom_->eepromReadChar(eeprom_, address + index);
            if (index > 0) {
                appendSeparator(outputStream);
            }
            appendHex2(outputStream, value);
        }
    }
    else if (commandHeader == COMMAND_WRITE_BLOCK_EEPROM) {
        ackCommand(outputStream, EEPROM_DEVICE_HEADER, COMMAND_WRITE_BLOCK_EEPROM);
        unsigned long address = readHex4(inputStream);
        char data;
        int index;
        for (index = 0; index < EEPROM_DEVICE_WRITE_BLOCK_LENGTH; index++) {
            checkIsSeparator(inputStream);
            data = readHex2(inputStream);
            eeprom_->eepromWriteChar(eeprom_, address + index, data);
        }
    }
    else if (commandHeader == COMMAND_INTENSIVE_TEST) {
        ackCommand(outputStream, EEPROM_DEVICE_HEADER, COMMAND_INTENSIVE_TEST);
        unsigned long address = readHex4(inputStream);
        checkIsSeparator(inputStream);
        unsigned long length = readHex4(inputStream);
        unsigned int errorCount = 0;
        unsigned int index;
        // Writes
        for (index = 0; index < length; index++) {
            unsigned char value = (unsigned char) index;
            eeprom_->eepromWriteChar(eeprom_, address + index, value);
        }
        // Reads
        for (index = 0; index < length; index++) {
            unsigned char value = (unsigned char) eeprom_->eepromReadChar(eeprom_, address + index);
            if (value != (unsigned char)index) {
                if (errorCount < 255) {
                    errorCount++;
                }
            }
        }
        appendHex4(outputStream, errorCount);
    }
}
Esempio n. 20
0
void deviceSystemHandleRawData(char header, InputStream* inputStream, OutputStream* outputStream) {
    if (header == COMMAND_PING) {
        // data
        ackCommand(outputStream, SYSTEM_DEVICE_HEADER, COMMAND_PING);
        
        // Read and write in output the pingIndex (to control that it's the right which does the response)
        unsigned char pingIndex = readHex2(inputStream);
        appendHex2(outputStream, pingIndex);
    }
    // Last Error
    else if (header == COMMAND_GET_LAST_ERROR) {
        ackCommand(outputStream, SYSTEM_DEVICE_HEADER, COMMAND_GET_LAST_ERROR);
        unsigned int lastError = getLastError();
        appendHex4(outputStream, lastError);
    }
    else if (header == COMMAND_CLEAR_LAST_ERROR) {
        ackCommand(outputStream, SYSTEM_DEVICE_HEADER, COMMAND_CLEAR_LAST_ERROR);
        clearLastError();
    }
    // Device list
    else if (header == COMMAND_DEVICE_LIST) {
        ackCommand(outputStream, SYSTEM_DEVICE_HEADER, COMMAND_DEVICE_LIST);
        printDeviceList(getInfoOutputStreamLogger());
    // Usage
    } else if (header == COMMAND_USAGE) {
        ackCommand(outputStream, SYSTEM_DEVICE_HEADER, COMMAND_USAGE);
        printDeviceListUsage(getInfoOutputStreamLogger(), false);
    } else if (header == COMMAND_USAGE_PROBLEM) {
        ackCommand(outputStream, SYSTEM_DEVICE_HEADER, COMMAND_USAGE_PROBLEM);
        printDeviceListUsage(getInfoOutputStreamLogger(), true);
    }    
    else if (header == COMMAND_USAGE_SPECIFIC_DEVICE) {
         ackCommand(outputStream, SYSTEM_DEVICE_HEADER, COMMAND_USAGE_SPECIFIC_DEVICE);
         char deviceHeader = readBinaryChar(inputStream);
         int size = getDeviceCount();
         int i;
         for (i = 0; i < size; i++) {
             Device* device = getDevice(i);
             if (deviceHeader == device->deviceInterface->deviceHeader) {
                 println(getInfoOutputStreamLogger());
                 printDeviceUsage(getInfoOutputStreamLogger(), device, false);
                 return;
             }
         }
         appendString(getErrorOutputStreamLogger(), "Device Not Found ! ");
    }
	else if (header == COMMAND_CLS) {
		ackCommand(outputStream, SYSTEM_DEVICE_HEADER, COMMAND_CLS);
#ifdef PC_COMPILER
		system("cls");
#else
		appendString(outputStream, "Unsupported Operation");
#endif // PC_COMPILER
	}
	else if (header == COMMAND_RESET) {
		ackCommand(outputStream, SYSTEM_DEVICE_HEADER, COMMAND_RESET);
		#ifdef PC_COMPILER
			appendString(outputStream, "Unsupported Operation");
		#else
			// goto 0;
		#endif // PC_COMPILER
	}
    // Notifications
    else if (header == COMMAND_NOTIFICATION) {
        ackCommand(outputStream, SYSTEM_DEVICE_HEADER, COMMAND_NOTIFICATION);
        printDeviceListNotification(getInfoOutputStreamLogger(), false);
    } else if (header == COMMAND_WAIT) {
        appendAck(outputStream);
        int mSec = readHex4(inputStream);
        delaymSec(mSec);
        append(outputStream, SYSTEM_DEVICE_HEADER);
        append(outputStream, COMMAND_WAIT);
    } else if (header == COMMAND_BOARD_NAME) {
        appendString(getInfoOutputStreamLogger(), getBoardName());
        println(getInfoOutputStreamLogger());
        ackCommand(outputStream, SYSTEM_DEVICE_HEADER, COMMAND_BOARD_NAME);
    }
}