//------------------------------------------------------ unsigned char read_buttons(void) { unsigned char ret_value = 0; if(read_pin(GPIO_139_FOR_POSITION) == 0) ret_value = BTN_GET_POSITION; if(read_pin(GPIO_138_FOR_PICH) == 0) ret_value = BTN_GET_PICH; if(read_pin(GPIO_137_FOR_ROLL) == 0) ret_value = BTN_GET_ROLL; if(read_pin(GPIO_136_FOR_SET_INIT_POS) == 0) ret_value = BTN_SET_POSITION; return ret_value; }
void systick() { static uint32_t counter; if (read_pin(BUTTON_PIN)) counter++; else counter--; write_pin(LED1_PIN, (counter / 50) & 1); write_pin(LED2_PIN, ((counter + 10) / 50) & 1); write_pin(LED3_PIN, ((counter + 20) / 50) & 1); }
int main (void) { unsigned int ret, i, j, k; tpruss_intc_initdata pruss_intc_initdata = PRUSS_INTC_INITDATA; printf("\nINFO: Starting %s example.\r\n", "pru_gpio"); /* Initialize the PRU */ prussdrv_init (); //NOTE : The PRU code does not export the PIN so this must be //done for any pin that used. LOCAL_export_pin(32 + 16,"out"); //GPIO1_16 LOCAL_export_pin(32 + 13,"in"); //GPIO1_13 /* Open PRU Interrupt */ ret = prussdrv_open(PRU_EVTOUT_0); if (ret) { printf("prussdrv_open open failed\n"); return (ret); } /* Get the interrupt initialized */ prussdrv_pruintc_init(&pruss_intc_initdata); /* Initialize example */ printf("\tINFO: Initializing example.\r\n"); LOCAL_exampleInit(); //Start th emain loop pruDataMem_byte[CMD_VALUE] = CMD_NO_OP; /* Execute example on PRU */ printf("\tINFO: Executing example.\r\n"); prussdrv_exec_program (PRU_NUM, "./pru_gpio.bin"); //The PRU is just configured to use GPIO1 for now. This can be changed easily in the assembly. set_pin(16); //GPIO1_16 clear_pin(16); int value = read_pin(13); //GPIO1_13 printf("PIN value is %d\n",value); printf("\tINFO: PRU completed transfer.\r\n"); prussdrv_pru_clear_event (PRU0_ARM_INTERRUPT); /* Disable PRU and close memory mapping*/ prussdrv_pru_disable (PRU_NUM); prussdrv_exit (); LOCAL_unexport_pin(38); return(0); }
int main() { //set pin to output p.p_addr = GPIO_BASE_ADDR; map_gpio(&p); set_as_input(&p, 4); //set_as_output(&p, 4); set_output(&p, 4, 0); clear_output(&p, 4); read_pin(&p, 4); return 0; }
void next_bit(uint8_t *state) { uint8_t tries = 0; // Pulses are all high pulses, low means nothing while(read_pin() == 0) { } // Sample all the meaningful pulses while(read_pin() == 1) { if(tries++ == 255) { break; // Don't get stuck looping forever } } // 1 is a long duration high pulse, 0 is a short one *state = (tries > LONG_PULSE_LENGTH) ? 1 : 0; #if DEBUG tries_b[try_count] = tries; try_count++; #endif }
void main(void) { select_gpio_out_mode(MBED_BASE_LED1); select_gpio_out_mode(MBED_BASE_LED2); select_gpio_out_mode(MBED_BASE_LED3); select_gpio_out_mode(MBED_BASE_LED4); select_gpio_in_mode(MBED_BASE_DIP12); select_gpio_in_mode(MBED_BASE_DIP13); select_gpio_in_mode(MBED_BASE_DIP14); select_gpio_in_mode(MBED_BASE_DIP15); select_gpio_in_mode(MBED_BASE_DIP16); clr_pin(MBED_BASE_LED1); clr_pin(MBED_BASE_LED2); clr_pin(MBED_BASE_LED3); clr_pin(MBED_BASE_LED4); //config(); while(1){ if (read_pin(MBED_BASE_DIP12)){ clr_pin(MBED_BASE_LED1); clr_pin(MBED_BASE_LED2); clr_pin(MBED_BASE_LED3); clr_pin(MBED_BASE_LED4); }else if (read_pin(MBED_BASE_DIP13)){ set_pin(MBED_BASE_LED1); }else if (read_pin(MBED_BASE_DIP14)){ set_pin(MBED_BASE_LED2); }else if (read_pin(MBED_BASE_DIP15)){ set_pin(MBED_BASE_LED3); }else if (read_pin(MBED_BASE_DIP16)){ set_pin(MBED_BASE_LED4); } wait(); } }
uint16_t keypad_get_key_state (void) { uint8_t j; uint16_t key_state; // get current states key_state = 0; for (int j=0; j < CFG_MAX_BUTTONS; j++) { if (read_pin (config.interface_cp_btn_pin[j])) key_state |= _BV(j); } return key_state; }
void LiquidCrystal::busy_wait() { bool busy = true; configure_as_input(data_pins[3]); set_pin(rs_pin, LOW); set_pin(rw_pin, HIGH); while(busy) { set_pin(enable_pin, HIGH); etk::sleep_us(1); busy = read_pin(data_pins[3]); set_pin(enable_pin, LOW); } set_pin(rw_pin, LOW); configure_as_output(data_pins[3]); }
/** Read a byte */ uint8_t _lcd_read_byte() { _lcd_mode_r(); uint8_t res = 0; _lcd_clk(); res = (read_pin(LCD_D7) << 7) | (read_pin(LCD_D6) << 6) | (read_pin(LCD_D5) << 5) | (read_pin(LCD_D4) << 4); _lcd_clk(); res |= (read_pin(LCD_D7) << 3) | (read_pin(LCD_D6) << 2) | (read_pin(LCD_D5) << 1) | (read_pin(LCD_D4) << 0); return res; }
char read_button(char pin_addr, char pin_no) { set_direction_pin(DDR_ADDR(pin_addr), pin_no, 0); return read_pin(pin_addr, pin_no); }
static int8_t dht_read_data(struct dht *self, int8_t *temperature, int8_t *humidity) { uint8_t bits[5]; memset(bits, 0, sizeof(bits)); //reset port pio_configure_pin(self->gpio, self->signal_pin, GP_OUTPUT); pio_write_pin(self->gpio, self->signal_pin, 1); delay_us(100000L); // send request pio_write_pin(self->gpio, self->signal_pin, 0); if(self->sensor_type == DHT_DHT11) delay_us(18000L); else delay_us(500); pio_configure_pin(self->gpio, self->signal_pin, GP_INPUT | GP_PULLUP); delay_us(40); #define read_pin() pio_read_pin(self->gpio, self->signal_pin) // start condition 1 if(read_pin()) return -1; delay_us(80); // start condition 2 if(!read_pin()) return -1; delay_us(80); //read the data uint16_t timeoutcounter = 0; for (int j=0; j<5; j++) { //read 5 byte uint8_t result=0; for(int i=0; i<8; i++) {//read every bit timeoutcounter = 0; while(!read_pin()) { //wait for an high input (non blocking) timeoutcounter++; if(timeoutcounter > DHT_TIMEOUT) { return -1; //timeout } } delay_us(30); if(read_pin()) //if input is high after 30 us, get result result |= (1<<(7-i)); timeoutcounter = 0; while(read_pin()) { //wait until input get low (blocking) timeoutcounter++; if(timeoutcounter > DHT_TIMEOUT) { return -1; //timeout } } } bits[j] = result; } //reset port pio_configure_pin(self->gpio, self->signal_pin, GP_OUTPUT); pio_write_pin(self->gpio, self->signal_pin, 0); //check checksum if ((uint8_t)(bits[0] + bits[1] + bits[2] + bits[3]) == bits[4]) { //return temperature and humidity if(self->sensor_type == DHT_DHT11){ *temperature = bits[2]; *humidity = bits[0]; } else if(self->sensor_type == DHT_DHT22){ uint16_t rawhumidity = bits[0]<<8 | bits[1]; uint16_t rawtemperature = bits[2]<<8 | bits[3]; if(rawtemperature & 0x8000) { *temperature = (float)((rawtemperature & 0x7FFF) / 10.0) * -1.0; } else { *temperature = (float)(rawtemperature)/10.0; } *humidity = (float)(rawhumidity)/10.0; } return 0; } return -1; }
// Read all pins and update the last_value and value_changed arrays void ParticleIO::read_all_pins() { for (unsigned i=0; i<num_pins; i++) { read_pin(i); } }
void IRRemote::HandleInterrupt(bool isUpdate, bool isTrigger) { UNUSED(isTrigger); if (!isUpdate) return; uint8_t irdata = (uint8_t) read_pin(); if (irdata == 0) { irdata = 0; //printf ("data low\n"); } _timerCount++; // One more 50us tick if (_rawlen >= RAWBUF) { // Buffer overflow _rcvstate = STATE_STOP; } switch (_rcvstate) { case STATE_IDLE: // In the middle of a gap if (irdata == MARK) { if (_timerCount < GAP_TICKS) { // Not big enough to be a gap. _timerCount = 0; } else { // gap just ended, record duration and start recording transmission _rawlen = 0; _rawbuf[_rawlen++] = _timerCount; _timerCount = 0; _rcvstate = STATE_MARK; } } break; case STATE_MARK: // timing MARK if (irdata == SPACE) { // MARK ended, record time _rawbuf[_rawlen++] = _timerCount; _timerCount = 0; _rcvstate = STATE_SPACE; } break; case STATE_SPACE: // timing SPACE if (irdata == MARK) { // SPACE just ended, record it _rawbuf[_rawlen++] = _timerCount; _timerCount = 0; _rcvstate = STATE_MARK; } else { // SPACE if (_timerCount > GAP_TICKS) { // big SPACE, indicates gap between codes // Mark current code as ready for processing // Switch to STOP // Don't reset timer; keep counting space width _rcvstate = STATE_STOP; } } break; case STATE_STOP: // waiting, measuring gap if (irdata == MARK) { // reset gap timer _timerCount = 0; } if (_callBack && decode()) { _callBack->RemoteButtonPressed(_value); uint32_t maxIdle = 200; uint32_t totalIdle = 0; while (1) { if (read_pin()) { if (totalIdle++ >= maxIdle) { break; } } else { totalIdle= 0; } RCC_Delay_us(100); } } resume(); break; } }
//The main program (this function is called in an endless loop) void main_program(void) { uint8_t output[2]; update_buttons(); dcf77_update(!read_pin(&pin_dcf77), g_ticks); //Menu button pressed? Switch menu mode if(g_buttons[0] && !g_last_buttons[0]) { g_mode = (enum modes_e)((g_mode + 1) % MODE_COUNT); switch(g_mode) { case MODE_SET_HOURS: case MODE_SET_DAY: g_blink_numbers = 1; break; case MODE_SET_MINUTES: case MODE_SET_MONTH: g_blink_numbers = 2; break; default: g_blink_numbers = 0; }; g_last_blink_action = g_ticks; g_last_blink_state = 1; g_last_inc_action = g_ticks; g_inc_pressed_time = g_ticks; } if(g_mode != MODE_NORMAL) //Inside Setup-mode? { //date/inc buttons pressed if(g_buttons[1]) { if(!g_last_buttons[1]) { on_increment_pressed(); g_inc_pressed_time = g_ticks; } //Increment continously if pressed & hold if(time_since(g_inc_pressed_time) > 100 && time_since(g_last_inc_action) > 15) { g_last_inc_action = g_ticks; on_increment_pressed(); } } //Are we currently setting time or date? display the appropriate output if(g_mode == MODE_SET_HOURS || g_mode == MODE_SET_MINUTES) { output[0] = g_time.hours; output[1] = g_time.minutes; } else { output[0] = g_time.day; output[1] = g_time.month; } // handle blinking of modifiable numbers if(g_blink_numbers != 0) { if(time_since(g_last_blink_action) > 50) { g_last_blink_action = g_ticks; g_last_blink_state = !g_last_blink_state; } // Going beyond single-digit range of the display // should turn the digit off if(g_last_blink_state == 0) output[g_blink_numbers-1] = 0xFF; } } else //normal operation (non-setup) { if(g_buttons[1]) { output[0] = g_time.day; output[1] = g_time.month; } else { output[0] = g_time.hours; output[1] = g_time.minutes; } } write_output(output[0], output[1]); }
uint8_t button_pressed(uint8_t index) { return read_pin(&pins_but[index]) == 0; }
void handle_firmware_configuration_request(const char *name, const char* value) { ConfigurationTree tree; if (*name == '\0') { send_app_error_response(PARAM_APP_ERROR_TYPE_BAD_PARAMETER_FORMAT, 0); return; } ConfigurationTreeNode *node = tree.FindNode(name); if (node == 0) { // TODO: improve on this error message send_app_error_response(PARAM_APP_ERROR_TYPE_BAD_PARAMETER_VALUE, PMSG(ERR_MSG_CONFIG_NODE_NOT_FOUND), name); return; } if (!node->IsLeafNode()) { // TODO: improve on this error message send_app_error_response(PARAM_APP_ERROR_TYPE_BAD_PARAMETER_VALUE, PMSG(ERR_MSG_CONFIG_NODE_NOT_COMPLETE), name); return; } if (value == 0) { if ((node->GetLeafOperations() & FIRMWARE_CONFIG_OPS_READABLE) == 0) { send_app_error_response(PARAM_APP_ERROR_TYPE_BAD_PARAMETER_VALUE, PMSG(ERR_MSG_CONFIG_NODE_NOT_READABLE)); return; } generate_value(node->GetNodeType(), tree.GetParentNode(node)->GetInstanceId(), node->GetInstanceId()); } else { if ((node->GetLeafOperations() & FIRMWARE_CONFIG_OPS_WRITEABLE) == 0) { send_app_error_response(PARAM_APP_ERROR_TYPE_BAD_PARAMETER_VALUE, PMSG(ERR_MSG_CONFIG_NODE_NOT_WRITEABLE)); return; } generate_response_start(RSP_APPLICATION_ERROR, 1); // ensure value has correct format and then attempt to set switch (node->GetLeafSetDataType()) { case LEAF_SET_DATATYPE_PIN: { long number; if (!read_pin(number, value) || number < 0 || number > UINT8_MAX) { send_app_error_response(PARAM_APP_ERROR_TYPE_BAD_PARAMETER_FORMAT, PMSG(ERR_MSG_INVALID_PIN_NUMBER), number); return; } if (!set_uint8_value(node->GetNodeType(), tree.GetParentNode(node)->GetInstanceId(), node->GetInstanceId(), number)) { return; // assume that set function has generated an error response } break; } case LEAF_SET_DATATYPE_UINT8: { long number; if (!read_number(number, value) || number < 0 || number > UINT8_MAX) { send_app_error_response(PARAM_APP_ERROR_TYPE_BAD_PARAMETER_FORMAT, PMSG(ERR_MSG_EXPECTED_UINT8_VALUE)); return; } if (!set_uint8_value(node->GetNodeType(), tree.GetParentNode(node)->GetInstanceId(), node->GetInstanceId(), number)) { return; // assume that set function has generated an error response } break; } case LEAF_SET_DATATYPE_INT16: { long number; if (!read_number(number, value)) { send_app_error_response(PARAM_APP_ERROR_TYPE_BAD_PARAMETER_FORMAT, PMSG(ERR_MSG_EXPECTED_INT16_VALUE)); return; } if (!set_int16_value(node->GetNodeType(), tree.GetParentNode(node)->GetInstanceId(), node->GetInstanceId(), number)) { return; // assume that set function has generated an error response } break; } case LEAF_SET_DATATYPE_BOOL: { bool val; if (strcmp(value, "true") == 0 || strcmp(value, "1") == 0) { val = true; } else if (strcmp(value, "false") == 0 || strcmp(value, "0") == 0) { val = false; } else { send_app_error_response(PARAM_APP_ERROR_TYPE_BAD_PARAMETER_FORMAT, PMSG(ERR_MSG_EXPECTED_BOOL_VALUE)); return; } if (!set_bool_value(node->GetNodeType(), tree.GetParentNode(node)->GetInstanceId(), node->GetInstanceId(), val)) { return; // assume that set function has generated an error response } break; } case LEAF_SET_DATATYPE_STRING: { if (!set_string_value(node->GetNodeType(), tree.GetParentNode(node)->GetInstanceId(), node->GetInstanceId(), value)) { return; // assume that set function has generated an error response } break; } case LEAF_SET_DATATYPE_FLOAT: { char *end; float val = strtod(value, &end); if (end == value || *end != '\0') { send_app_error_response(PARAM_APP_ERROR_TYPE_BAD_PARAMETER_FORMAT, PMSG(ERR_MSG_EXPECTED_FLOAT_VALUE)); return; } if (!set_float_value(node->GetNodeType(), tree.GetParentNode(node)->GetInstanceId(), node->GetInstanceId(), val)) { return; // assume that set function has generated an error response } break; } default: send_app_error_response(PARAM_APP_ERROR_TYPE_BAD_PARAMETER_FORMAT, PMSG(MSG_ERR_CANNOT_HANDLE_FIRMWARE_CONFIG_REQUEST), __LINE__); return; } send_OK_response(); } }