Example #1
0
//************************************
// Method:    Button
// FullName:  Button::Button
// Access:    public
// Returns:
// Qualifier:
// Parameter: uint8_t buttonPin
// Parameter: unsigned long debounceTime
// Parameter: unsigned long holdTime
// Parameter: unsigned long stateRemains
// Parameter: uint8_t buttonMode
//************************************
Button::Button(uint8_t buttonPin, unsigned long debounceTime, unsigned long holdTime, unsigned long stateRemains, uint8_t buttonMode) :
    state(0),
    pin(buttonPin),
    buttonState(RELEASED),
    debounceThreshold(debounceTime),
    pressedStartTime(-1),
    stateStartTime(-1),
    holdThreshold(holdTime),
    stateRemainsThreshold(stateRemains)
{
    // Configure GPIO mode as Input
    pinMode(pin, INPUT);
    bitWrite(state, MODE, (buttonMode == BUTTON_PULLUP));

    // Get the current status of the pin
    if (digitalRead(pin) == bitRead(state, MODE))
    {
        //currently the button is not pressed
        bitWrite(state, CURRENT, false);
    }
    else
    {
        //currently the button is pressed
        bitWrite(state, CURRENT, true);
    }
}
Example #2
0
void ArduboyTunes::initChannel(byte pin)
{
  byte timer_num;

  // we are all out of timers
  if (_tune_num_chans == AVAILABLE_TIMERS)
    return;

  timer_num = pgm_read_byte(tune_pin_to_timer_PGM + _tune_num_chans);
  _tune_pins[_tune_num_chans] = pin;
  _tune_num_chans++;
  pinMode(pin, OUTPUT);
  switch (timer_num) {
    case 1: // 16 bit timer
      TCCR1A = 0;
      TCCR1B = 0;
      bitWrite(TCCR1B, WGM12, 1);
      bitWrite(TCCR1B, CS10, 1);
      _tunes_timer1_pin_port = portOutputRegister(digitalPinToPort(pin));
      _tunes_timer1_pin_mask = digitalPinToBitMask(pin);
      break;
    case 3: // 16 bit timer
      TCCR3A = 0;
      TCCR3B = 0;
      bitWrite(TCCR3B, WGM32, 1);
      bitWrite(TCCR3B, CS30, 1);
      _tunes_timer3_pin_port = portOutputRegister(digitalPinToPort(pin));
      _tunes_timer3_pin_mask = digitalPinToBitMask(pin);
      playNote(0, 60);  /* start and stop channel 0 (timer 3) on middle C so wait/delay works */
      stopNote(0);
      break;
  }
}
Example #3
0
void LEDs::Set(int Row, int Col, bool On)
{
  int Bit = On?1:0;
  switch (m_iOrientation)
  {
    case eDown:
    {
      // pin 1 top right
      bitWrite(m_pNextPixels[7-Row], 7-Col, Bit);
      break;
    }
    case eLeft:
    {
      // pin 1 bottom right
      bitWrite(m_pNextPixels[Col], 7-Row, Bit);
      break;
    }
    case eRight:
    {
      // pin 1 top left
      bitWrite(m_pNextPixels[7-Col], Row, Bit);
      break;
    }
    default:
    {
      // pin 1 bottom left
      bitWrite(m_pNextPixels[Row], Col, Bit);
      break;
    }
  }
}
Example #4
0
void ButtonGroup::readPin(byte digitalPin)
{
    // Read the state of the switch into a local variable.
    int state = digitalRead(digitalPin);

    // Check to see if the pin's state has changed
    // (i.e. the input went from LOW to HIGH), and you've waited
    // long enough since the last state change to ignore any noise.

    // If the switch changed, due to noise or pressing reset the
    // debouncing timer.
    if (state != bitRead(lastStates, digitalPin))
    {
        lastDebounceTime = millis();
    }

    if ((millis() - lastDebounceTime) > debounceDelay)
    {
        // Whatever the state is at, it's been there for longer
        // than the debounce delay, so take it as the intended state
        // and notify the caller.
        int curState = bitRead(curStates, digitalPin);
        if (state != curState)
        {
            callback(digitalPin, state, curState, callbackData);
            bitWrite(curStates, digitalPin, state);
        }
    }

    // Save the state.  Next time through the loop, it'll be the
    // in lastState.
    bitWrite(lastStates, digitalPin, state);
}
Example #5
0
// XXX: this function only works properly for timer 2 (the only one we use
// currently).  for the others, it should end the tone, but won't restore
// proper PWM functionality for the timer.
void disableTimer(uint8_t _timer)
{
  switch (_timer)
  {
#if !defined(__AVR_ATmega8__)
    case 0:
      TIMSK0 = 0;
      break;
#endif
    case 1:
      bitWrite(TIMSK1, OCIE1A, 0);
      break;
    case 2:
      bitWrite(TIMSK2, OCIE2A, 0); // disable interrupt
      TCCR2A = (1 << WGM20);
      TCCR2B = (TCCR2B & 0b11111000) | (1 << CS22);
      OCR2A = 0;
      break;
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
    case 3:
      TIMSK3 = 0;
      break;
    case 4:
      TIMSK4 = 0;
      break;
    case 5:
      TIMSK5 = 0;
      break;
#endif
  }
}
Example #6
0
int RTC_M41T62::alarmRepeat(){ // return alarm repeat mode
  int byte1, byte2, byte3, byte4, mode = 0, retVal = 0;
  
  WIRE.beginTransmission(M41T62_ADDRESS);
  WIRE._I2C_WRITE(M41T62_ADOM);
  WIRE.endTransmission();
  WIRE.requestFrom(M41T62_ADDRESS, 4);
  byte1 = WIRE._I2C_READ();
  byte2 = WIRE._I2C_READ();
  byte3 = WIRE._I2C_READ();
  byte4 = WIRE._I2C_READ();
  bitWrite(mode,0,bitRead(byte4,7));
  bitWrite(mode,1,bitRead(byte3,7));
  bitWrite(mode,2,bitRead(byte2,7));
  bitWrite(mode,3,bitRead(byte1,7));
  bitWrite(mode,4,bitRead(byte1,6));
  
  switch(mode) {
    case 31: retVal = 1; break; // once per second
    case 30: retVal = 2; break; // once per minute
    case 28: retVal = 3; break; // once per hour
    case 24: retVal = 4; break; // once per day
    case 16: retVal = 5; break; // once per month
    case 0:  retVal = 6; break; // once per year
    default: retVal = 0; break; // ERROR
  }
  pointerReset();
  return retVal;
}
Example #7
0
void RTC_M41T62::writeSqwPinMode(M41T62SqwPinMode mode) {
  int currentByte;

  WIRE.beginTransmission(M41T62_ADDRESS);
  WIRE._I2C_WRITE(M41T62_SQWFQ_DOW);
  WIRE.endTransmission();
  WIRE.requestFrom(M41T62_ADDRESS, 1);
  currentByte = WIRE._I2C_READ();
  
  WIRE.beginTransmission(M41T62_ADDRESS);
  WIRE._I2C_WRITE(M41T62_SQWFQ_DOW);
  WIRE._I2C_WRITE((currentByte & 0x07) | (mode << 4));
  WIRE.endTransmission();
  
  // Flip SQW Enable bit in register M41T62_SQWEN_AMO
  WIRE.beginTransmission(M41T62_ADDRESS);
  WIRE._I2C_WRITE(M41T62_SQWEN_AMO);
  WIRE.endTransmission();
  WIRE.requestFrom(M41T62_ADDRESS, 1);
  
  if (mode == 0){ // Disable
    bitWrite(currentByte,6,0);
  }else{ // Enable
    bitWrite(currentByte,6,1);
  }

  WIRE.beginTransmission(M41T62_ADDRESS);
  WIRE._I2C_WRITE(M41T62_SQWEN_AMO);
  WIRE._I2C_WRITE(currentByte);
  WIRE.endTransmission();
}
Example #8
0
//---------------------------------------------------------------------------
void SndInit(void)
{
	_Memset(&Snd, 0x00, sizeof(ST_SND));

	pinMode(SND_PIN1, OUTPUT);
	Snd.ch[0].pPinPort = portOutputRegister(digitalPinToPort(SND_PIN1));
	Snd.ch[0].pinMask  = digitalPinToBitMask(SND_PIN1);

#if defined(ARDUBOY_10)

	pinMode(SND_PIN2, OUTPUT);
	Snd.ch[1].pPinPort = portOutputRegister(digitalPinToPort(SND_PIN2));
	Snd.ch[1].pinMask  = digitalPinToBitMask(SND_PIN2);

#endif


	TCCR3A = 0;
	TCCR3B = 0;
	TCCR1A = 0;
	TCCR1B = 0;

	bitWrite(TCCR3B, WGM32, 1);
	bitWrite(TCCR3B, CS30,  1);
	bitWrite(TCCR1B, WGM12, 1);
	bitWrite(TCCR1B, CS10,  1);

	power_timer3_enable();
	power_timer1_enable();
}
Example #9
0
//---------------------------------------------------------------------------
void SndStartTimerCh(u8 ch, u32 freq)
{
	// timer ck/1
	u32 ocr = freq;
	u8  pre = 0x01;

	if(ocr > 0xffff)
	{
		// ck/64
		ocr /= 64;
		pre  = 0x03;
	}
	ocr--;


	if(ch == 0)
	{
		TCCR3B = (TCCR3B & 0xf8) | pre;
		OCR3A  = ocr;
		bitWrite(TIMSK3, OCIE3A, 1);
	}
	else
	{
		TCCR1B = (TCCR1B & 0xf8) | pre;
		OCR1A  = ocr;
		bitWrite(TIMSK1, OCIE1A, 1);
	}
}
Example #10
0
// XXX: this function only works properly for timer 2 (the only one we use
// currently).  for the others, it should end the tone, but won't restore
// proper PWM functionality for the timer.
void disableTimer(uint8_t _timer)
{
  switch (_timer)
  {
    case 0:
      #if defined(TIMSK0)
        TIMSK0 = 0;
      #elif defined(TIMSK)
        TIMSK = 0; // atmega32
      #endif
      break;

#if defined(TIMSK1) && defined(OCIE1A)
    case 1:
      bitWrite(TIMSK1, OCIE1A, 0);
      break;
#endif

    case 2:
      #if defined(TIMSK2) && defined(OCIE2A)
        bitWrite(TIMSK2, OCIE2A, 0); // disable interrupt
      #endif
      #if defined(TCCR2A) && defined(WGM20)
        TCCR2A = (1 << WGM20);
      #endif
      #if defined(TCCR2B) && defined(CS22)
        TCCR2B = (TCCR2B & 0b11111000) | (1 << CS22);
      #endif
      #if defined(OCR2A)
        OCR2A = 0;
      #endif
      break;

#if defined(TIMSK3)
    case 3:
      TIMSK3 = 0;
      break;
#endif

#if defined(TIMSK4)
    case 4:
      TIMSK4 = 0;
      break;
#endif

#if defined(TIMSK5)
    case 5:
      TIMSK5 = 0;
      break;
#endif

#if defined(TC0)
    case 6:
      TC_Stop(TC1, 0);
      break;
#endif
  }
}
Example #11
0
byte CheckWiredLines(void)
  {
  byte w,x;

  bitWrite(HW_Config,HW_WIRED_IN, false);
  bitWrite(HW_Config,HW_WIRED_OUT,false);
  for(x=0;x<WIRED_PORTS;x++)
    pinMode(PIN_WIRED_OUT_1+x,OUTPUT); // definieer Arduino pin's voor Wired-Out als output
  
  // Alle WiredOut lijnen hoog.
  for(x=0;x<WIRED_PORTS;x++)
    digitalWrite(PIN_WIRED_OUT_1+x,HIGH);
  
  // Alle WiredOut lijnen laag.
  for(x=0;x<WIRED_PORTS;x++)
    digitalWrite(PIN_WIRED_OUT_1+x,LOW);
  
  // Tel hoeveel WiredIn lijnen hoog zijn
  w=0;
  for(x=0;x<WIRED_PORTS;x++)
    if((analogRead(PIN_WIRED_IN_1+x)>1000))
      w++;

  // Zijn de Wired-In verbonden met de Wired-out? Dan zijn ze allemaal hoog.
  if(w==WIRED_PORTS)
    return WIRED_NOT_CONNECTED;


  // Loop alle lijnen langs on te kijken of de WiredIn de WiredOut volgt        
  for(w=0;w<WIRED_PORTS;w++)
    {
    // Maak steeds een lijn hoog.
    for(x=0;x<WIRED_PORTS;x++)
      digitalWrite(PIN_WIRED_OUT_1+x,LOW);

    digitalWrite(PIN_WIRED_OUT_1+w,HIGH);

   // Is de Wired-In meegenomen naar HIGH door de Wired-Out?
   // Als laag, dan vermoedelijk onterecht verbonden met GND of andere Wired.
   if((analogRead(PIN_WIRED_IN_1+w)<1000))
     return WIRED_SHORT;
        
   // Zijn de andere lijnen allemaal nog laag?
   // Als andere lijn hoog, dan fout in de lijn
   for(x=0;x<WIRED_PORTS;x++)
     if(x!=w && (analogRead(PIN_WIRED_IN_1+x)>50))
       return WIRED_OPEN;
    }

  for(x=0;x<WIRED_PORTS;x++)
    digitalWrite(PIN_WIRED_OUT_1+x,LOW);

  bitWrite(HW_Config,HW_WIRED_IN, true);
  bitWrite(HW_Config,HW_WIRED_OUT,true);

  return WIRED_OK;
  }
Example #12
0
/*
|| @description
|| | Return the bitRead(state,CURRENT) of the switch
|| #
|| 
|| @return true if button is pressed
*/
bool Button::isPressed(void)
{  
  //save the previous value
  bitWrite(state,PREVIOUS,bitRead(state,CURRENT));
  
  //get the current status of the pin
  if (digitalRead(pin) == mode)
  {
    //currently the button is not pressed
    bitWrite(state,CURRENT,false);
  } 
  else 
  {
    //currently the button is pressed
    bitWrite(state,CURRENT,true);
  }
  
  //handle state changes
  if (bitRead(state,CURRENT) != bitRead(state,PREVIOUS))
  {
    //the state changed to PRESSED
    if (bitRead(state,CURRENT) == true) 
    {
      numberOfPresses++;
      if (cb_onPress) { cb_onPress(*this); }   //fire the onPress event
      pressedStartTime = millis();             //start timing
      triggeredHoldEvent = false;
    } 
    else //the state changed to RELEASED
    {
      if (cb_onRelease) { cb_onRelease(*this); } //fire the onRelease event
      if (cb_onClick) { cb_onClick(*this); }   //fire the onClick event AFTER the onRelease
      //reset states (for timing and for event triggering)
      pressedStartTime = -1;
    }
    //note that the state changed
    bitWrite(state,CHANGED,true);
  }
  else
  {
    //note that the state did not change
    bitWrite(state,CHANGED,false);
    //should we trigger a onHold event?
    if (pressedStartTime!=-1 && !triggeredHoldEvent) 
    {
      if (millis()-pressedStartTime > holdEventThreshold) 
      { 
        if (cb_onHold) 
        { 
          cb_onHold(*this); 
          triggeredHoldEvent = true;
        }
      }
    }
  }
  return bitRead(state,CURRENT);
}
Example #13
0
void TM1628::setChar(byte addr, byte chr) {
  for(int i=0; i<7; i++){
      if (addr < 2)
        bitWrite(buffer[(i*2) + 1], seg_addr[addr], bitRead(FONT_DEFAULT[chr - 0x20],i));
      else
        bitWrite(buffer[i*2], seg_addr[addr], bitRead(FONT_DEFAULT[chr - 0x20],i));
    }
	update();
}
RGB ColorDetector::getColor()
{
	//Run sequence required by Sensor

	//Operating sequence 1 - preperation
	digitalWrite (flashPin, HIGH);  //turn on illuminating LEDs
  
	digitalWrite (gatePin, LOW);
	digitalWrite (ckPin, LOW);

	//Operating sequence 2 - set sensitivity
	digitalWrite(rangePin, HIGH); // hardcoded for now
	delay(5);

	//Operating sequence 3 - integrate light
	digitalWrite(gatePin, HIGH);
	delay(100);
	digitalWrite(gatePin, LOW);
	delay(1);

		//Operating sequence 4 - data transfer
	for (int k=0; k<3; k++){  //loop through Red, Green, Blue color
		 for (int i=0; i<12; i++){  //loop through data bits, LSB first
			 // read the input pin:
			digitalWrite(ckPin, HIGH);  //clock rising edge (data change at rising edge)
			delay(1);     //clock high state

			colorArray[i][k] = digitalRead(dataPin);  //read bit from sensor
			//colorArray[i][k] = bitRead(analogRead(1),0);  //read LSB bit from analog input as random data for testing purposes
			
			digitalWrite(ckPin, LOW); //clock falling edge
			delay(1);        //clock low state
		}

		digitalWrite (ckPin, LOW); //hold time t2 - datasheet
		delay (1);    //hold time t2 - datasheet
	 }

	digitalWrite (flashPin, LOW);  //turn off illuminating LEDs


	  //collect bits from array to ints for each color
	for (int i=0; i<12; i++)
	{
		bitWrite(_red, i, colorArray[i][0]);
		bitWrite(_green, i, colorArray[i][1]);
		bitWrite(_blue, i, colorArray[i][2]);
	}

	colorObject.r = _red;
	colorObject.g = _green;
	colorObject.b = _blue;

	//Return red, green, blue values parsed in an RGB object
	return colorObject;
}
void Ecolino_Hijacker::refresh_states()
{
   // TODO: verify no bus contention

   byte sr_buffer;

    // select U2 on ecolino_ctl_board.schem (buttons 1-3)
   digitalWrite(ARD_MUX_A0, LOW);
   digitalWrite(ARD_MUX_A1, LOW);
   
   digitalWrite(ARD_MUX_EN_NOT, LOW); // enable multiplexer on panel
   sr_buffer = shiftIn(ARD_PANEL_DIN, ARD_DIN_CLK, LSBFIRST); // correct bit order?
   user_button_states[0] = (sr_buffer & 0x08) ? BUTTON_STATE_PRESSED : BUTTON_STATE_RELEASED;
   user_button_states[1] = (sr_buffer & 0x04) ? BUTTON_STATE_PRESSED : BUTTON_STATE_RELEASED;
   user_button_states[2] = (sr_buffer & 0x02) ? BUTTON_STATE_PRESSED : BUTTON_STATE_RELEASED;
   digitalWrite(ARD_MUX_EN_NOT, HIGH); // disable multiplexer on panel
   
   
    // select U2 on ecolino_ctl_board.schem (buttons 8-11)
   digitalWrite(ARD_MUX_A0, HIGH);
   digitalWrite(ARD_MUX_A1, LOW);
   digitalWrite(ARD_MUX_EN_NOT, LOW); // enable multiplexer on panel
   sr_buffer = shiftIn(ARD_PANEL_DIN, ARD_DIN_CLK, LSBFIRST); // correct bit order?
   for (int i = 0; i < 8; i++)
      user_button_states[10 - i] = (sr_buffer & (1 << i)) ? BUTTON_STATE_PRESSED : BUTTON_STATE_RELEASED;
   digitalWrite(ARD_MUX_EN_NOT, HIGH); // disable multiplexer on panel
   
   
   // read led state from board
   // arduino is 16 Mhz ,
   // 0.2 us from selecting multiplexer to flipping multiplexer enable
   // we have ~.5 us window to catch led state enable and flip sr load pin.  This gives us 8 cycles to flip pin
   // read is 63 ns, write is 63 ns, loop overhead is? [calc says cycle is 62 ns]
   
   // best method is : wait for demux_en_not to go low.  Wait one millisecond.  The next time the a1 pin goes low, led data is valid
   //                  and we have 0.6 us to catch a single pin flip with another 0.1 to latch into the SR
   
   // for sr:  want clock low and parallel enable low to begin with.  Then clock in data.  
   bitWrite(PORTB, 3, 0); // active low ARD_DIN_LD_NOT enabled.  (Parallel load)
   while(bitRead(PORTC, 4)); // wait for demux enable.  Should put this at top of function so it's done ahead of time and delay isn't needed
   delay(1);                  // delay would be replaced by time spent reading the buttons from panel.  Able to do this because 10 ms between updates
   while (bitRead(PORTC, 2)); // wait for a1 to go low (6 cycles)
   bitWrite(PORTB, 2, 1); // latch in the data waste .1 us (2 cycles).  This is clock pin
   bitWrite(PORTB, 2, 0); // probably should put some delay before this.  This is clock pin
   sr_buffer = shiftIn(ARD_BRD_DIN, ARD_DIN_CLK, LSBFIRST); // bit order correct ?
   
   // save the state of the leds
   for (int i = 0; i < 8; i++)
      board_led_states[i] = (sr_buffer & 1 << i) ?  LED_STATE_ON : LED_STATE_OFF;
	  
   // last four are flipped state
   for (int i = 4; i < 8; i++)
      board_led_states[i] != board_led_states[i];
}
void shiftPin(int pos, int value) {
  if(pos<8) {
      bitWrite(shiftByte1, pos, value);
  } else {
    bitWrite(shiftByte2, pos-8, value);
  }
  digitalWrite(LE,LOW);
  shiftOut(SDI,CLK,MSBFIRST,shiftByte2);    //High byte first
  shiftOut(SDI,CLK,MSBFIRST,shiftByte1);           //Low byte second
  digitalWrite(LE,HIGH);
}
Example #17
0
void TM1628::setSeg(byte addr, byte num) {
  char buf_i = 0;
 // buf_i = addr <
  for(int i=0; i<7; i++){
      // LMO: added check for segments < 2 
      if (addr < 2)
        bitWrite(buffer[(i * 2) + 1] , seg_addr[addr], bitRead(NUMBER_FONT[num],i));
      else
        bitWrite(buffer[i * 2]  , seg_addr[addr], bitRead(NUMBER_FONT[num],i));
    }
}
Example #18
0
/** 
 * @brief Pulses a particular bit (goes HIGH then LOW) with a specify delay
 * @param selected_GPIOs[] An valid (initialized) array of gpioID
 * @param data_to_write An unsigned integer (32 bits), where each bit contains the 
 * information regarding the status of each pin: 1=turn pin ON, 0=turn pin OFF
 * @param nbr_selectedPins Number of pins that were specified by the user
 * @param pinID ID of pin we want to pulse
 * @param delay Delay in seconds that will define the interval between signal states
 **/ 
void pulsePin(struct gpioID enabled_gpio[],unsigned int data_to_write,int nbr_selectedPins, int pinID, int delay)
{
	if (CONFIG_DEBUG_LCD) printf("========== START: PULSING PIN %d ==========\n",pinID);
	data_to_write=bitWrite(data_to_write,1,pinID);
	turn_ON_OFF_pins(enabled_gpio,data_to_write,nbr_selectedPins);
        sleep(delay);
	data_to_write=bitWrite(data_to_write,0,pinID);
	turn_ON_OFF_pins(enabled_gpio,data_to_write,nbr_selectedPins);
        sleep(delay);
	if (CONFIG_DEBUG_LCD) printf("========== END: PULSING PIN %d ==========\n",pinID);
}
void PCF8574::i2cSend(){
	PCFBUFFER = i2cRead(0x00);
	for(int i=0;i<8;i++){
		if(bitRead(PCFTRISA,i) == INPUT) bitWrite(PCFPORTA,i,bitRead(PCFBUFFER,i));
		if(PCFPULL[i] == 1) bitWrite(PCFPORTA,i,1); // Required for unblock pull level
		if(PCFPULL[i] == 2) bitWrite(PCFPORTA,i,0); // Required for unblock pull level
	}
	Wire1.beginTransmission(PCFaddress);
	Wire1.write((uint8_t )PCFPORTA);
	Wire1.endTransmission();

}
void PCF8574::blink(int pin,int time,int wait){
	if((pin >= 0) && (pin < 8)){
		i2cRead();
		for(int i=0;i<time;i++){
			bitWrite(PCFPORTA,pin,0);
			i2cSend();
			delay(wait);
			bitWrite(PCFPORTA,pin,1);
			i2cSend();
			delay(wait);
		}
	}
}
Example #21
0
/*
|| Return the bitWrite(state,CURRENT, of the switch
*/
bool Button::isPressed(void){
    bitWrite(state,PREVIOUS,bitRead(state,CURRENT));
    if (digitalRead(pin) == mode){
        bitWrite(state,CURRENT,false);
    } else {
        bitWrite(state,CURRENT,true);
    }
    if (bitRead(state,CURRENT) != bitRead(state,PREVIOUS)){
        bitWrite(state,CHANGED,true);
    }else{
        bitWrite(state,CHANGED,false);
    }
	return bitRead(state,CURRENT);
}
Example #22
0
void ArduboyAudio::setup() {
  // idk what any of this does
  pinMode(PIN_SPEAKER_1, OUTPUT);
  TCCR1A = 0;
  TCCR1B = 0;
  bitWrite(TCCR1B, WGM12, 1);
  bitWrite(TCCR1B, CS10, 1);
  _tunes_timer1_pin_port = portOutputRegister(digitalPinToPort(PIN_SPEAKER_1));
  _tunes_timer1_pin_mask = digitalPinToBitMask(PIN_SPEAKER_1);
  TCCR1B = (TCCR1B & 0b11111000) | 0b001;
  OCR1A = F_CPU / 4096  - 1;
  bitWrite(TIMSK1, OCIE1A, 1);
  if (EEPROM.read(EEPROM_AUDIO_ON_OFF)) on();
}
void Minitel::serialprint7(byte b) {
  boolean  i = false;
  for(int j = 0; j<8;j++) {
    if (bitRead(b,j)==1) {
      i =!i; //calcul de la parité
    }
  }
  if (i) {
    bitWrite(b,7,1); //ecriture de la partié
  }
  else {
    bitWrite(b,7,0); //ecriture de la partié
  }
  write(b); //ecriture du byte sur le software serial
}
void noTone(uint8_t _pin)
{
  int8_t _timer = -1;
  
  for (int i = 0; i < AVAILABLE_TONE_PINS; i++) {
    if (tone_pins[i] == _pin) {
      _timer = pgm_read_byte(tone_pin_to_timer_PGM + i);
      tone_pins[i] = 255;
    }
  }
  
  switch (_timer)
  {
#if defined(__AVR_ATmega8__)
    case 1:
      bitWrite(TIMSK1, OCIE1A, 0);
      break;
    case 2:
      bitWrite(TIMSK2, OCIE2A, 0);
      break;

#else
    case 0:
      TIMSK0 = 0;
      break;
    case 1:
      TIMSK1 = 0;
      break;
    case 2:
      TIMSK2 = 0;
      break;
#endif

#if defined(__AVR_ATmega1280__)
    case 3:
      TIMSK3 = 0;
      break;
    case 4:
      TIMSK4 = 0;
      break;
    case 5:
      TIMSK5 = 0;
      break;
#endif
  }

  digitalWrite(_pin, 0);
}
Example #25
0
/**
 * @brief
 * This method processes function 5
 * This method writes a value assigned by the master to a single bit
 *
 * @return u8BufferSize Response to master length
 * @ingroup discrete
 */
int8_t ModbusProcess_FC5(uint16_t *regs)
{
    //uint8_t u8currentRegister,
    uint8_t u8currentBit;
    uint8_t u8CopyBufferSize;
    uint16_t u16coil = word(_au8Buffer[ ADD_HI ], _au8Buffer[ ADD_LO ]);
    _lastAddress = u16coil;
    _lastCount = 1;
    // point to the register and its bit
    //u8currentRegister = (uint8_t) (u16coil / 16);
    u8currentBit = (uint8_t) (u16coil % 16);

    // write to coil
    bitWrite(
            *regs,
            u8currentBit,
            _au8Buffer[ NB_HI ] == 0xff);


    // send answer to master
    _u8BufferSize = 6;
    u8CopyBufferSize = _u8BufferSize + 2;
    ModbusSendTxBuffer();

    return u8CopyBufferSize;
}
Example #26
0
stdReturnType MaxMatrix::setDot(byte Column, byte Row, byte Value)
{
	if(Column >= 0 && Column < MAXMATRIX_NUMBER_OF_COLUMNS && Row >= 0 && Row < MAXMATRIX_ROW_NUMBER_OF_MODULE) {
	    bitWrite(buffer[Column], Row, Value);

	    int Module = Column / MAXMATRIX_COLUMN_NUMBER_OF_MODULE;
	    int ModuleColumn = Column % MAXMATRIX_COLUMN_NUMBER_OF_MODULE;
	    digitalWrite(ChipSelectPin, LOW);
	    for(int i = 0; i < NumberOfModules; i++)
	    {
		    if (i == Module) {
			    shiftOut(DataInPin, ClockPin, MSBFIRST, ModuleColumn + 1);
			    shiftOut(DataInPin, ClockPin, MSBFIRST, buffer[Column]);
			    } else {
			    shiftOut(DataInPin, ClockPin, MSBFIRST, 0);
			    shiftOut(DataInPin, ClockPin, MSBFIRST, 0);
		    }
	    }
	    digitalWrite(ChipSelectPin, LOW);
	    digitalWrite(ChipSelectPin, HIGH);
		return E_OK;
	} else {
		return E_NOT_OK;
	}
}
Example #27
0
//-----------------------------------------------------------------------------
// This function is called by the OpenPLC in a loop. Here the internal buffers
// must be updated to reflect the actual I/O state. The mutex bufferLock
// must be used to protect access to the buffers on a threaded environment.
//-----------------------------------------------------------------------------
void updateBuffers()
{
	//Lock mutexes
	pthread_mutex_lock(&bufferLock);
	pthread_mutex_lock(&ioLock);

	//Digital Input
	for (int i = 0; i < 8; i++)
	{
		if (bool_input[0][i] != NULL) *bool_input[0][i] = bitRead(input_data.digital[0], i);
	}

	//Analog Input
	for (int i = 0; i < 6; i++)
	{
		if (int_input[0][i] != NULL) *int_input[0][i] = input_data.analog[i];
	}

	//Digital Output
	for (int i = 0; i < 8; i++)
	{
		if (bool_output[0][i] != NULL) bitWrite(output_data.digital[0], i, *bool_output[0][i]);
	}

	//Analog Output
	for (int i = 0; i < 4; i++)
	{
		if (int_output[0][i] != NULL) output_data.analog[i] = *int_output[0][i];
	}

	pthread_mutex_unlock(&ioLock);
	pthread_mutex_unlock(&bufferLock);
}
void GMMaxMatrix::setDot(byte col, byte row, byte value)
{
	bitWrite(buffer[col], row, value);

	int n = col / 8;
	int c = col % 8;
	// build an array of the byte values to send
	int bufferIndex = 0;
	for (int i = 0; i<numMatrixes; i++)
	{
		if (i == n)
		{
			// this is the matrix we want to update
			tempBuffer[bufferIndex++] = c + 1;
			tempBuffer[bufferIndex++] = buffer[col];
		}
		else
		{
			// leave this matrix unchanged - set register and value to 0s
			tempBuffer[bufferIndex++] = 0;
			tempBuffer[bufferIndex++] = 0;
		}
	}
	//SPI.transferBuffer(tempBuffer, rxbuffer, bufferIndex);
	for (int i = 0; i < bufferIndex; i++)
	{
		//Serial.print(tempBuffer[i], HEX);
		SPI.transfer(tempBuffer[i]);
	}
}
Example #29
0
void Shifty::writeBitSoft(int bitnum, bool value) {
  int bytenum = bitnum / 8;
  int offset = bitnum % 8;
  byte b = this->writeBuffer[bytenum];
  bitWrite(b, offset, value);
  this->writeBuffer[bytenum] = b;
}
Example #30
0
bool Shifty::readBitHard(int bitnum) {
  int bytenum = bitnum / 8;
  int offset = bitnum % 8;

  // To read the bit, set all output pins except the pin we are looking at to 0
  for(int i = 0; i < this->byteCount; i++) {
    byte mask = this->dataModes[i];
    byte outb = this->writeBuffer[i];
    for(int j = 0; j < 8; j++) {
      if(bitRead(mask, j)) {
        if(i == bytenum && j == bitnum) {
          bitSet(outb, j);
        } else {
          bitClear(outb, j);
        }
      }
    }
  }

  // Flush
  writeAllBits();

  // Get our data pin
  bool value = digitalRead(this->readPin);

  // Set the cached value
  byte cacheb = this->readBuffer[bytenum];
  bitWrite(cacheb, offset, value);
  this->readBuffer[bytenum] = cacheb;

  return value;
}