void hexbright::set_light_level(unsigned long level) { // LOW 255 approximately equals HIGH 48/49. There is a color change. // Values < 4 do not provide any light. // I don't know about relative power draw. // look at linearity_test.ino for more detail on these algorithms. #if (DEBUG==DEBUG_LIGHT) Serial.print("light level: "); Serial.println(level); #endif pinModeFast(DPIN_PWR, OUTPUT); digitalWriteFast(DPIN_PWR, HIGH); if(level == 0) { // lowest possible power, but cpu still running (DPIN_PWR still high) digitalWriteFast(DPIN_DRV_MODE, LOW); analogWrite(DPIN_DRV_EN, 0); } else if(level == OFF_LEVEL) { // power off (DPIN_PWR LOW) pinModeFast(DPIN_PWR, OUTPUT); digitalWriteFast(DPIN_PWR, LOW); digitalWriteFast(DPIN_DRV_MODE, LOW); analogWrite(DPIN_DRV_EN, 0); } else if(level<=500) { digitalWriteFast(DPIN_DRV_MODE, LOW); analogWrite(DPIN_DRV_EN, .000000633*(level*level*level)+.000632*(level*level)+.0285*level+3.98); } else { level -= 500; digitalWriteFast(DPIN_DRV_MODE, HIGH); analogWrite(DPIN_DRV_EN, .00000052*(level*level*level)+.000365*(level*level)+.108*level+44.8); } }
/* Setup pins and initialize SPI hardware */ void pspad_init(void) { pinModeFast(pinACK, OUTPUT); digitalWriteFast(pinACK, HIGH); pinModeFast(pinATT, INPUT); digitalWriteFast(pinATT, HIGH); pinModeFast(pinCMD, INPUT); digitalWriteFast(pinCMD, HIGH); pinModeFast(pinDAT, OUTPUT); digitalWriteFast(pinDAT, HIGH); pinModeFast(pinCLK, INPUT); digitalWriteFast(pinCLK, HIGH); SPCR |= (1 << CPOL); // SCK HIGH when idle SPCR |= (1 << CPHA); // setup data on falling edge of CLK, read on rising edge // turn on SPI in slave mode SPCR |= _BV(SPE); SPCR |= (1 << DORD); // data order to LSB first // turn on interrupts SPCR |= _BV(SPIE); }
void Encoder::initialize() { pinModeFast(Utility::PIN_EncoderChannelA, INPUT); pinModeFast(Utility::PIN_EncoderChannelB, INPUT); encoderValues = (digitalReadFast(Utility::PIN_EncoderChannelA) << 1) | digitalReadFast(Utility::PIN_EncoderChannelB); // Sets ISR for external interrupt on pin 2 attachInterrupt(0, quadratureDecoderISR, RISING); }
int PS2Pad::init(bool disableInt) { PS2Pad::_disableInt = disableInt; pinModeFast(DAT_PIN, INPUT); digitalWriteFast(DAT_PIN, HIGH); pinModeFast(CLK_PIN, OUTPUT); pinModeFast(ATT_PIN, OUTPUT); pinModeFast(CMD_PIN, OUTPUT); // Init pad digitalWriteFast(CLK_PIN, HIGH); digitalWriteFast(CMD_PIN, HIGH); PS2Pad::read(); if(PS2Pad::_pad_data[1] != 0x41 && PS2Pad::_pad_data[1] != 0x73 && PS2Pad::_pad_data[1] != 0x79) { return 1; } PS2Pad::_read_delay = 1; for(byte i = 0; i <= 2; i++) { // Enter Config Mode byte enter_config_command[] = {0x01, 0x43, 0x00, 0x01, 0x00}; PS2Pad::send_command(enter_config_command, 5); // Get pad type byte get_pad_type_command[] = {0x01, 0x45, 0x00, 0x5A, 0x5A, 0x5A, 0x5A, 0x5A, 0x5A}; PS2Pad::send_command(get_pad_type_command, 9); PS2Pad::_type = get_pad_type_command[3]; // Lock to Analog Mode on Stick byte lock_analog_mode_command[] = {0x01, 0x44, 0x00, 0x01, 0x03, 0x00, 0x00, 0x00, 0x00}; PS2Pad::send_command(lock_analog_mode_command, 9); // Exit config mode byte exit_config_command[] = {0x01, 0x43, 0x00, 0x00, 0x5A, 0x5A, 0x5A, 0x5A, 0x5A}; PS2Pad::send_command(exit_config_command, 9); PS2Pad::read(); if(PS2Pad::_pad_data[1] == 0x73) { _analogMode = true; break; } PS2Pad::_read_delay++; } return 0; }
void hexbright::init_hardware() { // These next 8 commands are for reference and cost nothing, // as we are initializing the values to their default state. pinModeFast(DPIN_PWR, OUTPUT); digitalWriteFast(DPIN_PWR, LOW); pinModeFast(DPIN_RLED_SW, INPUT); pinModeFast(DPIN_GLED, OUTPUT); pinModeFast(DPIN_DRV_MODE, OUTPUT); pinModeFast(DPIN_DRV_EN, OUTPUT); digitalWriteFast(DPIN_DRV_MODE, LOW); digitalWriteFast(DPIN_DRV_EN, LOW); #if (DEBUG!=DEBUG_OFF) // Initialize serial busses Serial.begin(9600); Wire.begin(); Serial.println("DEBUG MODE ON"); #endif #if (DEBUG!=DEBUG_OFF && DEBUG!=DEBUG_PRINT) if(DEBUG==DEBUG_LIGHT) { // do a full light range sweep, (printing all light intensity info) set_light(0,1000,update_delay*1002); } else if (DEBUG==DEBUG_TEMP) { set_light(0, MAX_LEVEL, NOW); } else if (DEBUG==DEBUG_LOOP) { // note the use of TIME_MS/update_delay. set_light(0, MAX_LEVEL, 2500/update_delay); } #ifdef FREE_RAM Serial.print("Ram available: "); Serial.print(freeRam()); Serial.println("/1024 bytes"); #endif #ifdef FLASH_CHECKSUM Serial.print("Flash checksum: "); Serial.println(flash_checksum()); #endif #endif // DEBUG!=DEBUG_OFF #ifdef ACCELEROMETER enable_accelerometer(); #endif // was this power on from battery? if so, it was a button press, even if it was too fast to register. read_charge_state(); if(get_charge_state()==BATTERY) press_button(); continue_time = micros(); }
void hexbright::init_hardware() { // We just powered on! That means either we got plugged // into USB, or the user is pressing the power button. pinModeFast(DPIN_PWR, INPUT); digitalWriteFast(DPIN_PWR, LOW); // Initialize GPIO pinModeFast(DPIN_RLED_SW, INPUT); pinModeFast(DPIN_GLED, OUTPUT); pinModeFast(DPIN_DRV_MODE, OUTPUT); pinModeFast(DPIN_DRV_EN, OUTPUT); digitalWriteFast(DPIN_DRV_MODE, LOW); digitalWriteFast(DPIN_DRV_EN, LOW); #if (DEBUG!=DEBUG_OFF) // Initialize serial busses Serial.begin(9600); Wire.begin(); Serial.println("DEBUG MODE ON"); #endif #if (DEBUG!=DEBUG_OFF && DEBUG!=DEBUG_PRINT) if(DEBUG==DEBUG_LIGHT) { // do a full light range sweep, (printing all light intensity info) set_light(0,1000,update_delay*1002); } else if (DEBUG==DEBUG_TEMP) { set_light(0, MAX_LEVEL, NOW); } else if (DEBUG==DEBUG_LOOP) { // note the use of TIME_MS/update_delay. set_light(0, MAX_LEVEL, 2500/update_delay); } #ifdef FREE_RAM Serial.print("Ram available: "); Serial.print(freeRam()); Serial.println("/1024 bytes"); #endif #ifdef FLASH_CHECKSUM Serial.print("Flash checksum: "); Serial.println(flash_checksum()); #endif #endif // DEBUG!=DEBUG_OFF #ifdef ACCELEROMETER enable_accelerometer(); #endif continue_time = micros(); }
int PJON::send_string(uint8_t id, char *string, uint8_t length) { if (!*string) return FAIL; if(!this->can_start()) return BUSY; uint8_t CRC = 0; pinModeFast(_input_pin, OUTPUT); this->send_byte(id); CRC ^= id; this->send_byte(length + 3); CRC ^= length + 3; for(uint8_t i = 0; i < length; i++) { this->send_byte(string[i]); CRC ^= string[i]; } this->send_byte(CRC); digitalWriteFast(_input_pin, LOW); if(id == BROADCAST) return ACK; unsigned long time = micros(); int response = FAIL; while(response == FAIL && micros() - time <= BIT_SPACER + BIT_WIDTH) response = this->receive_byte(); if (response == ACK || response == NAK) return response; return FAIL; };
/* DO NOT CHANGE ANYTHING IN THE FUNCTION BELOW!!! * * It was specially crafted to work with an 8Mhz Atmega328/168 (int. resonator). * * */ static inline void GCPad_send(byte *cmd, byte length) { byte bit = 128; byte high; pinModeFast(JOY_DATA_PIN, OUTPUT); loop: high = *cmd & bit; digitalWriteFast(JOY_DATA_PIN, LOW); if(high) { digitalWriteFast(JOY_DATA_PIN, HIGH); bit >>= 1; if(bit < 1) { bit = 128; cmd++; length--; } else { asm volatile ("nop\nnop\nnop\nnop\nnop\n"); } asm volatile ("nop\nnop\nnop\nnop\n"); } else {
boolean PJON::can_start() { pinModeFast(_input_pin, INPUT); this->send_bit(0, 2); if(!this->read_byte()) return true; return false; }
inline void hexbright::_led_off(unsigned char led) { if(led == RLED) { // DPIN_RLED_SW digitalWriteFast(DPIN_RLED_SW, LOW); pinModeFast(DPIN_RLED_SW, INPUT); } else { // DPIN_GLED digitalWriteFast(DPIN_GLED, LOW); } }
inline void hexbright::_led_on(unsigned char led) { if(led == RLED) { // DPIN_RLED_SW pinModeFast(DPIN_RLED_SW, OUTPUT); analogWrite(DPIN_RLED_SW, led_brightness[RLED]); } else { // DPIN_GLED analogWrite(DPIN_GLED, led_brightness[GLED]); } }
void hexbright::shutdown() { pinModeFast(DPIN_PWR, OUTPUT); digitalWriteFast(DPIN_PWR, LOW); digitalWriteFast(DPIN_DRV_MODE, LOW); analogWrite(DPIN_DRV_EN, 0); // make sure we don't try to turn back on change_done = change_duration+1; end_light_level = 0; }
void initializeController() { // The baud rate had to be dropped from 115200 to 19200 as going any higher resulted in data loss // due to the firing of the external interrupt for the decoder Serial.begin(19200); Serial.println(); Serial.flush(); pinModeFast(HEARTBEAT_LED_PIN, OUTPUT); }
void setup(void) { // Init PSX pad emulator pspad_init(); // Configure arcade pins as INPUT and pullups ON for(int i = 0; i < 10; i++) { pinModeFast(i, INPUT); digitalWriteFast(i, HIGH); } pinModeFast(A1, INPUT); digitalWriteFast(A1, HIGH); pinModeFast(A2, INPUT); digitalWriteFast(A2, HIGH); pinModeFast(A3, INPUT); digitalWriteFast(A3, HIGH); }
void hexbright::init_hardware() { // These next 8 commands are for reference and cost nothing, // as we are initializing the values to their default state. pinModeFast(DPIN_PWR, OUTPUT); digitalWriteFast(DPIN_PWR, LOW); pinModeFast(DPIN_RLED_SW, INPUT); pinModeFast(DPIN_GLED, OUTPUT); pinModeFast(DPIN_DRV_MODE, OUTPUT); pinModeFast(DPIN_DRV_EN, OUTPUT); digitalWriteFast(DPIN_DRV_MODE, LOW); digitalWriteFast(DPIN_DRV_EN, LOW); #if (DEBUG!=DEBUG_OFF) // Initialize serial busses Serial.begin(9600); Wire.begin(); Serial.println("DEBUG MODE ON"); #endif #if (DEBUG!=DEBUG_OFF && DEBUG!=DEBUG_PRINT) #ifdef FREE_RAM Serial.print("Ram available: "); Serial.print(freeRam()); Serial.println("/1024 bytes"); #endif #ifdef FLASH_CHECKSUM Serial.print("Flash checksum: "); Serial.println(flash_checksum()); #endif #endif //(DEBUG!=DEBUG_OFF && DEBUG!=DEBUG_PRINT) #ifdef ACCELEROMETER enable_accelerometer(); #endif // was this power on from battery? if so, it was a button press, even if it was too fast to register. read_charge_state(); if(get_charge_state()==BATTERY) press_button(); continue_time = micros(); }
void hmcoreSetup(void) { pinModeFast(PIN_WIRELESS_LED, OUTPUT); blinkLed(); #ifdef HEATERMETER_SERIAL Serial.begin(HEATERMETER_SERIAL); // don't use SerialX because we don't want any preamble Serial.write('\n'); reportVersion(); #endif /* HEATERMETER_SERIAL */ // Disable Analog Comparator ACSR = bit(ACD); // Disable Digital Input on ADC pins DIDR0 = bit(ADC5D) | bit(ADC4D) | bit(ADC3D) | bit(ADC2D) | bit(ADC1D) | bit(ADC0D); // And other unused units power_twi_disable(); // Switch the pin mode first to INPUT with internal pullup // to take it to 5V before setting the mode to OUTPUT. // If we reverse this, the pin will go OUTPUT,LOW and reboot. // SoftReset and WiShield are mutually exlusive, but it is HIGH/OUTPUT too digitalWriteFast(PIN_SOFTRESET, HIGH); pinModeFast(PIN_SOFTRESET, OUTPUT); tone4khz_init(); pid.Probes[TEMP_PIT] = &probe0; pid.Probes[TEMP_FOOD1] = &probe1; pid.Probes[TEMP_FOOD2] = &probe2; pid.Probes[TEMP_AMB] = &probe3; eepromLoadConfig(0); pid.init(); lcdDefineChars(); #ifdef HEATERMETER_RFM12 checkInitRfManager(); #endif Menus.setState(ST_HOME_NOPROBES); }
inline void hexbright::_led_on(unsigned char led) { if(led == RLED) { // DPIN_RLED_SW pinModeFast(DPIN_RLED_SW, OUTPUT); byte l = rledMap[led_brightness[RLED]>>6]; byte r = 1<<(loopCount & 0b11); if(l & r) { digitalWriteFast(DPIN_RLED_SW, HIGH); } else { digitalWriteFast(DPIN_RLED_SW, LOW); } } else { // DPIN_GLED
int PJON::receive_byte() { pinModeFast(_input_pin, INPUT); digitalWriteFast(_input_pin, LOW); unsigned long time = micros(); while (digitalReadFast(_input_pin) && micros() - time <= BIT_SPACER); time = micros() - time; if(time >= ACCEPTANCE && !this->syncronization_bit()) return (int)this->read_byte(); return FAIL; }
boolean PJON::can_start() { pinModeFast(_input_pin, INPUT); for(uint8_t i = 0; i < 9; i++) { if(digitalReadFast(_input_pin)) return false; delayMicroseconds(BIT_WIDTH); } if(digitalReadFast(_input_pin)) return false; return true; }
static inline void pinModeSafe(uint8_t pin, uint8_t mode) { if(!__builtin_constant_p(pin)) { pinMode(pin, mode); } else { if((mode == INPUT || mode == INPUT_PULLUP) && !DIGITALIO_NO_MIX_ANALOGWRITE) noAnalogWrite(pin); const bool write_is_atomic = DIGITALIO_NO_INTERRUPT_SAFETY || (__builtin_constant_p(mode) && mode == OUTPUT && _directionIsAtomic(pin)); if(write_is_atomic) { pinModeFast(pin, mode); } else { ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { pinModeFast(pin, mode); } } } }
void saturn_init() { pinModeFast(D1, INPUT); pinModeFast(D0, INPUT); pinModeFast(D3, INPUT); pinModeFast(D2, INPUT); pinModeFast(S0, OUTPUT); pinModeFast(S1, OUTPUT); digitalWriteFast(D1, HIGH); digitalWriteFast(D0, HIGH); digitalWriteFast(D3, HIGH); digitalWriteFast(D2, HIGH); }
uint16_t PJON::send_string(uint8_t id, char *string, uint8_t length) { if(!*string) return FAIL; if(!this->can_start()) return BUSY; uint8_t CRC = 0; pinModeFast(_input_pin, OUTPUT); this->send_byte(id); CRC ^= id; this->send_byte(length + 3); CRC ^= length + 3; for(uint8_t i = 0; i < length; i++) { this->send_byte(string[i]); CRC ^= string[i]; } this->send_byte(CRC); digitalWriteFast(_input_pin, LOW); if(id == BROADCAST) return ACK; uint32_t time = micros(); uint16_t response = FAIL; /* TODO - If micros() overflow occurs here (really slight chance) transmitter is stuck in reception until a byte is received */ while(response == FAIL && (uint32_t)(micros() - time) <= BIT_SPACER + BIT_WIDTH) response = this->receive_byte(); if(response == ACK) return response; /* Random delay if NAK, corrupted ACK/NAK or collision */ if(response != FAIL) delayMicroseconds(random(0, COLLISION_MAX_DELAY)); if(response == NAK) return response; return FAIL; };
uint16_t PJON::receive() { uint16_t state; uint16_t package_length = PACKET_MAX_LENGTH; uint8_t CRC = 0; for(uint8_t i = 0; i < package_length; i++) { data[i] = state = this->receive_byte(); if(state == FAIL) return FAIL; if(i == 0 && data[i] != _device_id && data[i] != BROADCAST) return BUSY; if(i == 1) if(data[i] > 3 && data[i] < PACKET_MAX_LENGTH) package_length = data[i]; else return FAIL; CRC ^= data[i]; } pinModeFast(_input_pin, OUTPUT); if(!CRC) { if(data[0] != BROADCAST) { this->send_byte(ACK); digitalWriteFast(_input_pin, LOW); } this->_receiver(data[1] - 3, data + 2); return ACK; } else { if(data[0] != BROADCAST) { this->send_byte(NAK); digitalWriteFast(_input_pin, LOW); } return NAK; } }
uint16_t PJON::send_string(uint8_t id, char *string, uint8_t length) { if(!string) return FAIL; if(!this->can_start()) return BUSY; uint8_t CRC = 0; pinModeFast(_input_pin, OUTPUT); this->send_byte(id); CRC = this->compute_crc_8(id, CRC); this->send_byte(length + 3); CRC = this->compute_crc_8(length + 3, CRC); for(uint8_t i = 0; i < length; i++) { this->send_byte(string[i]); CRC = this->compute_crc_8(string[i], CRC); } this->send_byte(CRC); digitalWriteFast(_input_pin, LOW); if(id == BROADCAST) return ACK; uint32_t time = micros(); uint16_t response = FAIL; while(response == FAIL && (uint32_t)(time + BIT_SPACER + BIT_WIDTH) >= micros()) response = this->receive_byte(); if(response == ACK || response == NAK) return response; /* Random delay if NAK, corrupted ACK/NAK or collision */ if(response != FAIL) delayMicroseconds(random(0, COLLISION_MAX_DELAY)); return FAIL; };
void tg16_init(void) { // Configure Data pins pinModeFast(5, INPUT); digitalWriteFast(5, HIGH); pinModeFast(7, INPUT); digitalWriteFast(7, HIGH); pinModeFast(8, INPUT); digitalWriteFast(8, HIGH); pinModeFast(9, INPUT); digitalWriteFast(9, HIGH); // Configure Data Select and /OE pins pinModeFast(10, OUTPUT); digitalWriteFast(10, HIGH); pinModeFast(11, OUTPUT); digitalWriteFast(11, LOW); }
void Adc::initialize() { pinModeFast(Utility::PIN_AdcChipSelect, OUTPUT); digitalWriteFast(Utility::PIN_AdcChipSelect, LOW); digitalWriteFast(Utility::PIN_AdcChipSelect, HIGH); }