case 'c': //8v enable sequence, c/# command from gui LEDGREENON();//led on char onoff =…
- User
case 'c': //8v enable sequence, c/# command from gui LED_GREEN_ON();//led on char on_off = getchar();
if (on_off =='1'){ GPIO_P8V_EN_ON(); // 8V IC_enable pin set to high //measure output voltage value via adc adc16ChannelConfigStruct.channelNumber = 0U; ADC16_SetChannelConfig(ADC0, 0U, &adc16ChannelConfigStruct); delay(10, 100); ADC8V = ADC16_GetChannelConversionValue(ADC0, 0U); float ADC8V_m = (float)ADC8V/4096*3.3; // increase output voltage if it is too low // output voltage will increase after a pulse on GPIO_P8V_UP pin, loop until the correct voltage level is meet counter8v = 0; while (ADC8V_m < ADC8V_dn){ GPIO_P8V_UP_ON(); GPIO_P8V_UP_OFF(); ADC16_SetChannelConfig(ADC0, 0U, &adc16ChannelConfigStruct); delay(2000, 100); ADC8V = ADC16_GetChannelConversionValue(ADC0, 0U); ADC8V_m = (float)ADC8V/4096*3.3; counter8v += 1; if (counter8v > 10 ) { printf("8v output is too low, vout adc is %f\n\r",ADC8V_m); goto exit8v; // Exits the loop when counter reaches 20 as a limit } } counter8v = 0; // decrease output voltage if it is too high // output voltage will decrease after a pulse on GPIO_P8V_DN pin, loop until the correct voltage level is meet while (ADC8V_m > ADC8V_up){ GPIO_P8V_DN_ON(); GPIO_P8V_DN_OFF(); ADC16_SetChannelConfig(ADC0, 0U, &adc16ChannelConfigStruct); delay(2000, 100); ADC8V = ADC16_GetChannelConversionValue(ADC0, 0U); ADC8V_m = (float)ADC8V/4096*3.3; counter8v += 1; if (counter8v > 10 ) { printf("8v output is too high, vout adc is %f\n\r",ADC8V_m); goto exit8v; // Exits the loop when counter reaches 20 as a limit } } printf("8v debug adjusted\n\r"); }else{ GPIO_P8V_EN_OFF(); // 8V IC_enable pin set to high printf("8v IC disabled\n\r"); } exit8v: LED_GREEN_OFF();//led off break;how to transfer this entire case to a function I can call.
- Assistant
- User
I want to have it like this
void Seq_8v_en(char on_off){ if (on_off =='1'){ GPIO_P8V_EN_ON(); // 8V IC_enable pin set to high //measure output voltage value via adc adc16ChannelConfigStruct.channelNumber = 0U; ADC16_SetChannelConfig(ADC0, 0U, &adc16ChannelConfigStruct); delay(10, 100); ADC8V = ADC16_GetChannelConversionValue(ADC0, 0U); float ADC8V_m = (float)ADC8V/40963.3; // increase output voltage if it is too low // output voltage will increase after a pulse on GPIO_P8V_UP pin, loop until the correct voltage level is meet counter8v = 0; while (ADC8V_m < ADC8V_dn){ GPIO_P8V_UP_ON(); GPIO_P8V_UP_OFF(); ADC16_SetChannelConfig(ADC0, 0U, &adc16ChannelConfigStruct); delay(2000, 100); ADC8V = ADC16_GetChannelConversionValue(ADC0, 0U); ADC8V_m = (float)ADC8V/40963.3; counter8v += 1; if (counter8v > 10 ) { printf("8v output is too low, vout adc is %f\n\r",ADC8V_m); goto exit8v; // Exits the loop when counter reaches 20 as a limit } } counter8v = 0; // decrease output voltage if it is too high // output voltage will decrease after a pulse on GPIO_P8V_DN pin, loop until the correct voltage level is meet while (ADC8V_m > ADC8V_up){ GPIO_P8V_DN_ON(); GPIO_P8V_DN_OFF(); ADC16_SetChannelConfig(ADC0, 0U, &adc16ChannelConfigStruct); delay(2000, 100); ADC8V = ADC16_GetChannelConversionValue(ADC0, 0U); ADC8V_m = (float)ADC8V/4096*3.3; counter8v += 1; if (counter8v > 10 ) { printf("8v output is too high, vout adc is %f\n\r",ADC8V_m); goto exit8v; // Exits the loop when counter reaches 20 as a limit } } printf("8v debug adjusted\n\r"); }else{ GPIO_P8V_EN_OFF(); // 8V IC_enable pin set to high printf("8v IC disabled\n\r"); } exit8v:
- Assistant
- User
but with a break it only exit out of while loop. it will run the other code
- Assistant
- User
Starting_ad_hex_send = hex(int(Starting_ad,16) + i//2) otp_data = '00' + str(Starting_ad_hex_send).replace('0x', '')
Starting_ad= 01 sometimes 10, I would like to keep the 0 how do I make sure the 0 does not get erased
- Assistant
To ensure that the leading zero in your hexadecimal string is not removed, you can use Python's string formatting capabilities to consistently format your hexadecimal numbers. Here's how you can achieve this:
- User
bit_size = 1 Starting_ad = "1" # Starting address in hexadecimal (string) i = 10 # Increment value (example)
Starting_ad_hex = int(Starting_ad, 16) # Convert “01” to integer 1 Starting_ad_hex_send = Starting_ad_hex + (i // 2) # Add half of i, result is 6
otp_data = '00'*bit_size + format(Starting_ad_hex_send, '02x') # Format to hex, ensures two characters with leading zeros if necessary
print(otp_data) # Output is “0006”
I like to modify this code above, for example here I like to have 0006 when bit_size = 1. if bitsize is 2, it will be 000006, while with bitzied =0 it will be 06
- Assistant
- User
simplify this code
- Assistant
Certainly! You can simplify the code by avoiding repeated variable assignments and streamlining the calculation for the required length. Here is a more compact version of your code:
bit_size = 1 Starting_ad = "1" i = 10 # Convert starting address to integer and compute new address Starting_ad_hex_send = int(Starting_ad, 16) + (i // 2) # Compute required length for the final hexadecimal string required_length = 2 + 2 * bit_size # Format the new address as a hexadecimal string with leading zeros otp_data = format(Starting_ad_hex_send, f'0{required_length}x') print(otp_data)Streamlined Explanation
- Direct Calculation: The
Starting_ad_hex_sendcomputation combines the conversion and addition in a single line. - Single Required Length Calculation:
required_lengthis calculated in one line, making it clear what the length includes. - Dynamic Formatting: The
formatfunction is used with an f-string to dynamically create a hex string of the correct length.
This condensed version maintains all the functionality of the original code while being more readable and easier to understand.
- Direct Calculation: The
- User
bit_size = 2 Starting_ad = "1" i = 10 added_value = "dd"
Starting_ad_hex_send = int(Starting_ad, 16) + (i // 2) required_length = 2 * bit_size # Resulting length will be 2 + 2*0 = 2 otp_data = added_value + format(Starting_ad_hex_send, f'0{required_length}x') # Format to ‘06’
print(otp_data) # Output: “dd06”
I like to add a str at the beginning of the otp data. for example if bit_size is 2 I would like to have dd06 as a result.
- Assistant
- User
for example if my bit size is 2 with the 0006, I still like to add dd0006, is there a way to add dd to the beginning
- Assistant
- User
added_value = "dd" how to calculate how many characters in this
- Assistant
You can calculate the number of characters in
added_valueusing Python's built-inlen()function, which returns the length of a string. Here’s how you can incorporate the number of characters in into your existing code: - User
File "C:.Workplace\Python\class\main.py", line 496, in validate_button_func otp_value = format(otp_value, f'0{required_length_DATA - Validate_DATA_length}x') ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ValueError: Unknown format code 'x' for object of type 'str'
how to fix
- Assistant
- User
BYTE_SIZE_INT = 2 Validate_DATA = "dd" otp_value = "02" Validate_DATA_length = len(Validate_DATA) required_length_DATA = 2 * BYTE_SIZE_INT
otp_value = format(otp_value, f'0{required_length_DATA - Validate_DATA_length}x')
otp_value = Validate_DATA + otp_valueprint(otp_value) otp_value = "02" is in string, how do I fix this
- Assistant
- User
I also dont know do I use neither or either
- Assistant