/** * Returns Analog Pin value in volts to SCPI context * @param context SCPI context * @return success or failure */ scpi_result_t RP_AnalogPinValueQ(scpi_t * context) { int32_t choice; /* Read first parameter - APIN */ if (!SCPI_ParamChoice(context, scpi_RpApin, &choice, true)) { RP_LOG(LOG_ERR, "*ANALOG:PIN? is missing first parameter.\n"); return SCPI_RES_ERR; } // Convert port into pin id rp_apin_t pin = choice; // Now get the pin value float value; int result = rp_ApinGetValue(pin, &value); if (RP_OK != result){ RP_LOG(LOG_ERR, "*ANALOG:PIN? Failed to get pin value: %s\n", rp_GetError(result)); return SCPI_RES_ERR; } // Return back result SCPI_ResultDouble(context, value); RP_LOG(LOG_INFO, "*ANALOG:PIN? Successfully returned port value.\n"); return SCPI_RES_OK; }
int main(int argc, char **argv){ /* Print error, if rp_Init() function failed */ if(rp_Init() != RP_OK){ fprintf(stderr, "Rp api init failed!\n"); } float volts; rp_ApinGetValue(RP_AIN3, &volts); printf("Volts: %f\n", volts); rp_dpin_t led_pin = 0; float p = volts*(100/3.3); int i; for(i = 1; i < 7; i++){ if(p >= (100/7) * i){ rp_DpinSetState(led_pin + i, RP_HIGH); }else{ rp_DpinSetState(led_pin + i, RP_LOW); } } rp_Release(); return 0; }
void *read_analog_sig(void *ptr) { unsigned int i; int send_ms = 0; float *panalog_sig0 = panalog_signals; float *panalog_sig1 = &panalog_signals[NUM_ANA_SIG_LEN]; float *panalog_sig2 = &panalog_signals[(NUM_ANA_SIG_LEN*2)]; float *panalog_sig3 = &panalog_signals[(NUM_ANA_SIG_LEN*3)]; /* fprintf(fd, "panalog_sig0 addr:%p\npanalog_sig1 addr:%p\npanalog_sig2 addr:%p\n\ panalog_sig3 addr:%p\n", panalog_sig0, panalog_sig1, panalog_sig2, panalog_sig3);*/ //fflush(fd); float *pcurr_sig0 = NULL; float *pcurr_sig1 = NULL; float *pcurr_sig2 = NULL; float *pcurr_sig3 = NULL; while(api_running) { while( *pdelta_T == -1 && *pnum_of_meas == -1 ) usleep(2000); sleep_delta_T = ((unsigned int)((*pdelta_T) * 1000)); num_of_meas = (unsigned int)(*pnum_of_meas); i = 0; while(num_of_meas > 0) { if( *pmeas_control == 3 ) { *pdelta_T = -1; *pnum_of_meas = -1; break; } while( *pmeas_control == 1 ) usleep(2000); pcurr_sig0 = panalog_sig0; pcurr_sig1 = panalog_sig1; pcurr_sig2 = panalog_sig2; pcurr_sig3 = panalog_sig3; for( i = 0; i < NUM_ANA_SIG_LEN; ++i ) { if( num_of_meas == 0) { //n = last_ind; for( ; i < NUM_ANA_SIG_LEN; ++i) { *pcurr_sig0 = -100; *pcurr_sig1 = -100; *pcurr_sig2 = -100; *pcurr_sig3 = -100; ++pcurr_sig0; ++pcurr_sig1; ++pcurr_sig2; ++pcurr_sig3; } break; } rp_ApinGetValue(RP_AIN0, pcurr_sig0); rp_ApinGetValue(RP_AIN1, pcurr_sig1); rp_ApinGetValue(RP_AIN2, pcurr_sig2); rp_ApinGetValue(RP_AIN3, pcurr_sig3); ++pcurr_sig0; ++pcurr_sig1; ++pcurr_sig2; ++pcurr_sig3; ++last_ind; --num_of_meas; usleep(sleep_delta_T); send_ms += *pdelta_T; if( send_ms >= SEND_MS ) { ++i; for( ; i < NUM_ANA_SIG_LEN; ++i) { *pcurr_sig0 = -100; *pcurr_sig1 = -100; *pcurr_sig2 = -100; *pcurr_sig3 = -100; ++pcurr_sig0; ++pcurr_sig1; ++pcurr_sig2; ++pcurr_sig3; } last_ind = 511; send_ms = 0; break; } } send_ms = 0; if( num_of_meas <= 0) { *pdelta_T = -1; *pnum_of_meas = -1; last_ind = 511; //usleep(50); } ++(*pchange); if( *pchange == 100e3 ) *pchange = 1; //n = last_ind; while( last_ind == 511 ) usleep(2000); } } return NULL; }