void MAX7219::writeRegister(byte addr, byte value, byte chip) { word cmd, *buf; cmd = word(addr, value); if(chip == MAX7219_CHIP_ALL) { buf = (word *)malloc(_chips * sizeof(word)); for(byte i = 0; i < _chips; i++) buf[i] = cmd; writeRegisters(buf, _chips, 0); free(buf); } else writeRegisters(&cmd, 1, chip); }
Led24::Led24(const int pins[]){ _modeIndex = 0; _autoMode = true; _currentMode = ANIMATIONS[_modeIndex]; // set user defined shift register pins // _serialDataPin = pins[0]; _registerClockPin = pins[1]; _serialClockPin = pins[2]; // initialize counter variables // _time = millis(); _index = 0; _state = 1; _sequence_count = 0; // set data & clock pins on the arduino // pinMode(_serialDataPin, OUTPUT); pinMode(_registerClockPin, OUTPUT); pinMode(_serialClockPin, OUTPUT); // ensure nothing is in the shift registers to start // for(int i = 0; i < TOTAL_LEDS; i++) _registers[i] = LOW; writeRegisters(); }
// Set the PID that controls how we stop as we approach the // angle we're set to stop at bool EVShieldBank::motorSetEncoderPID(uint16_t Kp, uint16_t Ki, uint16_t Kd) { writeIntToBuffer(_i2c_buffer + 0, Kp); writeIntToBuffer(_i2c_buffer + 2, Ki); writeIntToBuffer(_i2c_buffer + 4, Kd); return writeRegisters(SH_ENCODER_PID, 6); }
// Sets the PID that controls how well that motor maintains its speed bool EVShieldBank::motorSetSpeedPID(uint16_t Kp, uint16_t Ki, uint16_t Kd) { writeIntToBuffer(_i2c_buffer + 0, Kp); writeIntToBuffer(_i2c_buffer + 2, Ki); writeIntToBuffer(_i2c_buffer + 4, Kd); return writeRegisters(SH_SPEED_PID, 6); }
void run() { // If a register was changed during this cycle (by calling setRegisterPin()), save it if(shift_register_changed == true) { writeRegisters(); shift_register_changed = false; } }
void Led24::quick_pulse(){ for(int i = TOTAL_LEDS - 1; i >= 0; i--){ _registers[i] = (_state == 1) ? HIGH : LOW; } _state = !_state; writeRegisters(); }
int main(void) { initLCD(display_cmd_buffer, 32); sendLCDCmd(LCD_CMD_CLEAR); serviceLCD(); // Clear the LCD just to make sure we know where we are. _delay_ms(2); // This takes a long time sendLCDCmd(LCD_CMD_DSP_ON); // Configure the ADC initADC(); sei(); sendCommand(CMD_STOP); sendCommand(CMD_SDATAC); uint8_t registers[] = {0x10, 0x10, 0x10, 0x10, 0x90, 0x90, 0x90, 0x90}; writeRegisters(0x05, registers, 8); // Configure data ready interrupt DDRB &= ~(1<<4); PCMSK0 = 1<<4; sendCommand(CMD_START); sendCommand(CMD_RDATAC); PCICR |= 1; char buf[6]; while (1) { itoa(inputBuffer[0], buf, 10); LCD_MOVE_TO_CHAR(0,0); writeString(buf, 6); for (int i = 0; i < 10; i++) { serviceLCD(); _delay_us(60); } _delay_ms(100); } }
void Led24::every_other_one(){ for(int i = TOTAL_LEDS - 1; i >= 0; i--){ _registers[i] = (i % 2 == _state) ? HIGH : LOW; } // flip to the other direction // _state = !_state; writeRegisters(); }
void Led24::slow_step_left_to_right(){ for(int i = TOTAL_LEDS - 1; i >= 0; i--){ _registers[i] = (i == _index) ? HIGH : LOW; } _index++; if (_index == TOTAL_LEDS) _index = 0; writeRegisters(); }
void display_bit_pattern(int cycle) { clearRegisters(); int index; for (index = 0 ; index < SR_MAX ; index++) { setRegisterPin(index, (cycle & (1 << index)) > 0 ? HIGH : LOW); } writeRegisters(); }
void setup() { // Shift Register pinMode(PIN_SR_SER, OUTPUT); pinMode(PIN_SR_RCLK, OUTPUT); pinMode(PIN_SR_SRCLK, OUTPUT); clearRegisters(); // Set all registers to LOW writeRegisters(); // Save changes }
QModbusResponse QModbusServerPrivate::processReadWriteMultipleRegistersRequest( const QModbusRequest &request) { CHECK_SIZE_LESS_THAN(request); quint16 readStartAddress, readQuantity, writeStartAddress, writeQuantity; quint8 byteCount; request.decodeData(&readStartAddress, &readQuantity, &writeStartAddress, &writeQuantity, &byteCount); // byte count does not match number of data bytes following or register count if ((byteCount != (request.dataSize() - 9 )) || (byteCount != (writeQuantity * 2))) { return QModbusExceptionResponse(request.functionCode(), QModbusExceptionResponse::IllegalDataValue); } if ((readQuantity < 0x0001) || (readQuantity > 0x007B) || (writeQuantity < 0x0001) || (writeQuantity > 0x0079)) { return QModbusExceptionResponse(request.functionCode(), QModbusExceptionResponse::IllegalDataValue); } // According to spec, write operation is executed before the read operation // Get the requested range out of the registers. QModbusDataUnit writeRegisters(QModbusDataUnit::HoldingRegisters, writeStartAddress, writeQuantity); if (!q_func()->data(&writeRegisters)) { return QModbusExceptionResponse(request.functionCode(), QModbusExceptionResponse::IllegalDataAddress); } const QByteArray pduData = request.data().remove(0,9); QDataStream stream(pduData); QVector<quint16> values; quint16 tmp; for (int i = 0; i < writeQuantity; i++) { stream >> tmp; values.append(tmp); } writeRegisters.setValues(values); if (!q_func()->setData(writeRegisters)) { return QModbusExceptionResponse(request.functionCode(), QModbusExceptionResponse::ServerDeviceFailure); } // Get the requested range out of the registers. QModbusDataUnit readRegisters(QModbusDataUnit::HoldingRegisters, readStartAddress, readQuantity); if (!q_func()->data(&readRegisters)) { return QModbusExceptionResponse(request.functionCode(), QModbusExceptionResponse::IllegalDataAddress); } return QModbusResponse(request.functionCode(), quint8(readQuantity * 2), readRegisters.values()); }
bool EVShieldBank::ledSetRGB(uint8_t R, uint8_t G, uint8_t B) { bool b; writeByteToBuffer(_i2c_buffer, R); writeByteToBuffer(_i2c_buffer+1,G); writeByteToBuffer(_i2c_buffer+2,B); b = writeRegisters(SH_RGB_LED, 3); delay(1); // required to avoid subsequent i2c errors. return b; }
// This function sets the speed, the number of seconds, and // the control (a.k.a. command register A) bool EVShieldBank::motorSetEncoderSpeedTimeAndControl( SH_Motor which_motors, // Motor_ 1, 2, or Both long encoder, // encoder/tachometer position int speed, // speed, in range [-100, +100] uint8_t duration, // in seconds uint8_t control) // control flags { if (which_motors == SH_Motor_Both) { // The motor control registers are back to back, and both can be written in one command control &= ~SH_CONTROL_GO; // Clear the 'go right now' flag evshieldSetEncoderSpeedTimeAndControlInBuffer(_i2c_buffer, encoder, speed, duration, control); evshieldSetEncoderSpeedTimeAndControlInBuffer(_i2c_buffer + 8, encoder, speed, duration, control); bool success = writeRegisters(SH_SETPT_M1, 16); motorStartBothInSync(); return success; } // Or, just issue the command for one motor evshieldSetEncoderSpeedTimeAndControlInBuffer(_i2c_buffer, encoder, speed, duration, control); uint8_t reg = (which_motors == SH_Motor_1) ? SH_SETPT_M1 : SH_SETPT_M2; return writeRegisters(reg, 8); }
void Led24::knight_rider(){ for(int i = TOTAL_LEDS - 1; i >= 0; i--){ if (_state == 1){ _registers[i] = (i == _index) ? HIGH : LOW; } else if (_state == 0){ _registers[i] = (i == TOTAL_LEDS - _index - 1) ? HIGH : LOW; } } _index++; if (_index == TOTAL_LEDS) { _index = 0; // flip to the other direction // _state = !_state; } writeRegisters(); }
// This function sets the speed, the number of seconds, and // the control (a.k.a. command register A) bool EVShieldBank::motorSetSpeedTimeAndControl( SH_Motor which_motors, // Motor_ 1, 2, or Both int speed, // in range [-100, +100] uint8_t duration, // in seconds uint8_t control) // bit flags for control purposes { if (which_motors == SH_Motor_Both) { control &= ~SH_CONTROL_GO; // Clear the 'go right now' flag bool m1 = motorSetSpeedTimeAndControl(SH_Motor_1, speed, duration, control); bool m2 = motorSetSpeedTimeAndControl(SH_Motor_2, speed, duration, control); motorStartBothInSync(); return m1 && m2; } _i2c_buffer[0] = (uint8_t)(int8_t)speed; _i2c_buffer[1] = duration; _i2c_buffer[2] = 0; // command register B _i2c_buffer[3] = control; // command register A uint8_t reg = (which_motors == SH_Motor_1) ? SH_SPEED_M1 : SH_SPEED_M2; return writeRegisters(reg, 4); }
void Led24::out_from_center(){ // turn everyone off // for(int i = TOTAL_LEDS - 1; i >= 0; i--) _registers[i] = LOW; for(int k = 0; k < CHAIN_LENGTH; k++){ int l = CENTER_LED + _index - k; int r = CENTER_LED - _index + k - 1; // set constraints // if (l < CENTER_LED) l = CENTER_LED; if (r > CENTER_LED - 1) r = CENTER_LED - 1; if (l >= TOTAL_LEDS) l = TOTAL_LEDS - 1; if (r < 0) r = 0; if (_index < CENTER_LED + CHAIN_LENGTH - 1){ _registers[l] = HIGH; _registers[r] = HIGH; } else{ _registers[l] = LOW; _registers[r] = LOW; } } _index++; if (_index == CENTER_LED + CHAIN_LENGTH) _index = 0; writeRegisters(); }
void MAX7219::setDigits(const byte *values, byte topo) { word *buf; word transfers; byte chips; chips = _topology[topo].chipTo - _topology[topo].chipFrom + 1; buf = (word *)malloc(chips * sizeof(word)); transfers = (_topology[topo].chipFrom == _topology[topo].chipTo ? getDigitCount(topo) : (_topology[topo].chipFrom == _topology[topo].chipTo - 1 ? max(7 - _topology[topo].digitFrom, _topology[topo].digitTo) + 1 : 8)); #if defined(MAX7219_DEBUG) Serial.print("Chips: "); Serial.print(chips); Serial.print(", transfers: "); Serial.print(transfers); Serial.print(", element: "); Serial.println(topo); #endif for (word i = 0; i < transfers; i++) { for(byte j = 0; j < chips; j++) if(i < (j == _topology[topo].chipFrom ? 7 - _topology[topo].digitFrom + 1 : (j == _topology[topo].chipTo ? _topology[topo].digitTo + 1 : 8))) buf[j] = word(MAX7219_REG_DIGIT0 + (j ? i : _topology[topo].digitFrom + i), values[(transfers - 1) * j + i]); else buf[j] = word(MAX7219_REG_NOOP, 0x00); writeRegisters(buf, chips, _topology[topo].chipFrom); } free(buf); }
// WRITE A SINGLE REGISTER // Write a single byte of data to a register in the MMA8452Q. void MMA8452Q::writeRegister(MMA8452Q_Register reg, byte data) { writeRegisters(reg, &data, 1); }
void ADXL345PiI2C::writeAddress(uint8_t reg) { writeRegisters(reg, NULL, 0); }
void TICC1100::sendPacket(std::shared_ptr<BaseLib::Systems::Packet> packet) { try { if(!packet) { _out.printWarning("Warning: Packet was nullptr."); return; } if(_fileDescriptor->descriptor == -1 || _gpioDescriptors[1]->descriptor == -1 || _stopped) return; if(packet->payload()->size() > 54) { _out.printError("Error: Tried to send packet larger than 64 bytes. That is not supported."); return; } std::shared_ptr<MAXPacket> maxPacket(std::dynamic_pointer_cast<MAXPacket>(packet)); if(!maxPacket) return; std::vector<uint8_t> packetBytes = maxPacket->byteArray(); int64_t timeBeforeLock = BaseLib::HelperFunctions::getTime(); _sendingPending = true; _txMutex.lock(); _sendingPending = false; if(_stopCallbackThread || _fileDescriptor->descriptor == -1 || _gpioDescriptors[1]->descriptor == -1 || _stopped) { _txMutex.unlock(); return; } _sending = true; sendCommandStrobe(CommandStrobes::Enum::SIDLE); sendCommandStrobe(CommandStrobes::Enum::SFTX); _lastPacketSent = BaseLib::HelperFunctions::getTime(); if(_lastPacketSent - timeBeforeLock > 100) { _out.printWarning("Warning: Timing problem. Sending took more than 100ms. Do you have enough system resources?"); } if(maxPacket->getBurst()) { sendCommandStrobe(CommandStrobes::Enum::STX); usleep(1000000); } writeRegisters(Registers::Enum::FIFO, packetBytes); if(!maxPacket->getBurst()) sendCommandStrobe(CommandStrobes::Enum::STX); if(_bl->debugLevel > 3) { if(packet->timeSending() > 0) { _out.printInfo("Info: Sending (" + _settings->id + ", WOR: " + (maxPacket->getBurst() ? "yes" : "no") + "): " + packet->hexString() + " Planned sending time: " + BaseLib::HelperFunctions::getTimeString(packet->timeSending())); } else { _out.printInfo("Info: Sending (" + _settings->id + ", WOR: " + (maxPacket->getBurst() ? "yes" : "no") + "): " + packet->hexString()); } } //Unlocking of _txMutex takes place in mainThread } catch(const std::exception& ex) { _out.printEx(__FILE__, __LINE__, __PRETTY_FUNCTION__, ex.what()); } catch(BaseLib::Exception& ex) { _out.printEx(__FILE__, __LINE__, __PRETTY_FUNCTION__, ex.what()); } catch(...) { _out.printEx(__FILE__, __LINE__, __PRETTY_FUNCTION__); } }
void DS1307::setTime(const long & p) { // writeRegisters((byte *) &(p ((unsigned long)BITS_HR<<16 | BITS_MIN<<8 | BITS_SEC)), // (byte) DS1307_SEC, 3); writeRegisters((byte) DS1307_SEC, (byte *) &p, 3); }
void Update(void) { writeRegisters(); }
void DS1307::setCalendar(const long & p) { // YYMMDD // writeRegisters((byte*) &(p & ((unsigned long)BITS_YR<<16 | (unsigned long)BITS_MTH<<8 | BITS_DATE)), (uint8_t) DS1307_DOW, 4); writeRegisters((uint8_t) DS1307_DATE, (byte*) &p, 3); }
bool I2C_Base::writeReg(char deviceAddress, char registerAddress, char value) { return writeRegisters(deviceAddress, registerAddress, &value, 1); }