[chirp_devel] def convert_freq_to_bytes(real_freq):
Hi group,
I'm trying to create a helper function to convert a floating point number to a group of bytes
For example I have this value: 146.52000 I think I need it change to something like 0x01,0x04,0x06,0x05,0x02,0x00,0x00,0x00
The first thing I beieve I need to do is shift the decimal point to the right 5 places. So I did this
real_freq *= 100000.0
I believe this gets me 14652000
Now I think I need to do something like this to separate the individual digits
digits =[int(i) for i in str(real_freq)]
I'm not positive, but I think this makes an array something like
digits[0] = 1 digits[1] = 4 digits[2] = 6 digits[3] = 5 digits[4] = 2 digits[5] = 0 digits[6] = 0 digits[7] = 0
Here is where I'm stuck. I've tried a few think but so far no luck. I've searched the other drivers to see if I could locate something similar. If there is, I missed it.
Then ultimately, I think I need to end with
return bytes
So am I on the right track? Any advice on what I need to do next.
Thanks, Jim
That looks to me like it will work, if you convert the real number to an int before converting it to a string (to prevent the for loop from applying the int operator to the decimal point). You can even combine everything into one statement, so the function definition would be something like:
def freq2bytes(f): return [int(i) for i in str(int(f*100000))]
-Les W6VN
On 21 Jan 2013, at 19:08, Jim Unroe rock.unroe@gmail.com wrote:
Hi group,
I'm trying to create a helper function to convert a floating point number to a group of bytes
For example I have this value: 146.52000 I think I need it change to something like 0x01,0x04,0x06,0x05,0x02,0x00,0x00,0x00
The first thing I beieve I need to do is shift the decimal point to the right 5 places. So I did this
real_freq *= 100000.0
I believe this gets me 14652000
Now I think I need to do something like this to separate the individual digits
digits =[int(i) for i in str(real_freq)]
I'm not positive, but I think this makes an array something like
digits[0] = 1 digits[1] = 4 digits[2] = 6 digits[3] = 5 digits[4] = 2 digits[5] = 0 digits[6] = 0 digits[7] = 0
Here is where I'm stuck. I've tried a few think but so far no luck. I've searched the other drivers to see if I could locate something similar. If there is, I missed it.
Then ultimately, I think I need to end with
return bytes
So am I on the right track? Any advice on what I need to do next.
Thanks, Jim
The answer is:
def convert_freq_to_bytes(freq): bytes = [ 0 for x in range(0,8) ] # init list with 8 times 0 real_freq = int(freq * 100000) # it has to be integer for i in range(7, -1, -1): # go from 7 to 0 bytes[i] = real_freq%10 # extract last digit real_freq /= 10 # throw away last digit return bytes
I'm not a python guru, may be this can be done better with some python magics but this function just works :)
73 de IZ3GME
On 22/01/2013 04:08, Jim Unroe wrote:
Hi group,
I'm trying to create a helper function to convert a floating point number to a group of bytes
For example I have this value: 146.52000 I think I need it change to something like 0x01,0x04,0x06,0x05,0x02,0x00,0x00,0x00
The first thing I beieve I need to do is shift the decimal point to the right 5 places. So I did this
real_freq *= 100000.0
I believe this gets me 14652000
Now I think I need to do something like this to separate the individual digits
digits =[int(i) for i in str(real_freq)]
I'm not positive, but I think this makes an array something like
digits[0] = 1 digits[1] = 4 digits[2] = 6 digits[3] = 5 digits[4] = 2 digits[5] = 0 digits[6] = 0 digits[7] = 0
Here is where I'm stuck. I've tried a few think but so far no luck. I've searched the other drivers to see if I could locate something similar. If there is, I missed it.
Then ultimately, I think I need to end with
return bytes
So am I on the right track? Any advice on what I need to do next.
Thanks, Jim
chirp_devel mailing list chirp_devel@intrepid.danplanet.com http://intrepid.danplanet.com/mailman/listinfo/chirp_devel Developer docs: http://chirp.danplanet.com/projects/chirp/wiki/Developers
Thanks Les and Marc0,
I tried this quickly before work and ran into this problem "NameError: global name 'convert_freq_to_bytes' is not defined"
I study some other drivers after work to see if I can find something similar to mimic. Here is how I am trying to use it just in case you can spot what I am doing wrong.
I've added 2 "elif" bits of code to the set_settings procedure
def set_settings(self, settings): _settings = self._memobj.settings[0] for element in settings: if not isinstance(element, RadioSetting): self.set_settings(element) continue elif element.get_name() == "vfoa.freq": convert_freq_to_bytes(self._memobj.vfoa.freq, element.value) elif element.get_name() == "vfob.freq": convert_freq_to_bytes(self._memobj.vfob.freq, element.value) try:
Then I placed the following just ahead of my menu item
def convert_freq_to_bytes(real_freq): bytes = [ 0 for x in range(0,8) ] # init list with 8 times 0 real_freq = int(freq * 100000) # it has to be integer for i in range(7, -1, -1): # go from 7 to 0 bytes[i] = real_freq%10 # extract last digit real_freq /= 10 # throw away last digit return bytes
When I got the "not defined" error, I moved it above "def set_settings(self, settings):" but got the same "not defined".
Jim
Hi Jim I suppose you have two different error for the two different situation: - if you place the function definition inside another one you have to augment the indentation level - if you define the function at the object level it will become a method and should be defined and called differently
There's one more option which is place it in the global scope but ... My suggestion is to place it inside the set_settings, you can look at _filter definition in get_settings (uv5r.py) for an example.
BTW It would be easier to test/debug your code if you attach the whole source or a hg patch (better) so we can see all the details and replicate the problem.
73 de IZ3GME Marco
On 22/01/2013 11:41, Jim Unroe wrote:
Thanks Les and Marc0,
I tried this quickly before work and ran into this problem "NameError: global name 'convert_freq_to_bytes' is not defined"
I study some other drivers after work to see if I can find something similar to mimic. Here is how I am trying to use it just in case you can spot what I am doing wrong.
I've added 2 "elif" bits of code to the set_settings procedure
def set_settings(self, settings): _settings = self._memobj.settings[0] for element in settings: if not isinstance(element, RadioSetting): self.set_settings(element) continue elif element.get_name() == "vfoa.freq": convert_freq_to_bytes(self._memobj.vfoa.freq,
element.value) elif element.get_name() == "vfob.freq": convert_freq_to_bytes(self._memobj.vfob.freq, element.value) try:
Then I placed the following just ahead of my menu item
def convert_freq_to_bytes(real_freq): bytes = [ 0 for x in range(0,8) ] # init list with 8 times 0 real_freq = int(freq * 100000) # it has to be integer for i in range(7, -1, -1): # go from 7 to 0 bytes[i] = real_freq%10 # extract last digit real_freq /= 10 # throw away last digit return bytes
When I got the "not defined" error, I moved it above "def set_settings(self, settings):" but got the same "not defined".
Jim
chirp_devel mailing list chirp_devel@intrepid.danplanet.com http://intrepid.danplanet.com/mailman/listinfo/chirp_devel Developer docs: http://chirp.danplanet.com/projects/chirp/wiki/Developers
BTW It would be easier to test/debug your code if you attach the whole source or a hg patch (better) so we can see all the details and replicate the problem.
73 de IZ3GME Marco
Thanks Marco,
I'm still quite a noob at using Mercurial. I believe these 3 patch files will give you the current state that I am working within.
Jim
Here we are, the attached patch has to be applied after yours three. I deleted redundant function definition in get_settings, also deleted some test code there, moved the definition of the convertion function _inside_ set_settings and finally debugged a little bit.
hope this can help
73 de IZ3GME Marco
On 22/01/2013 12:31, Jim Unroe wrote:
BTW It would be easier to test/debug your code if you attach the whole source or a hg patch (better) so we can see all the details and replicate the problem. 73 de IZ3GME Marco
Thanks Marco,
I'm still quite a noob at using Mercurial. I believe these 3 patch files will give you the current state that I am working within.
Jim
chirp_devel mailing list chirp_devel@intrepid.danplanet.com http://intrepid.danplanet.com/mailman/listinfo/chirp_devel Developer docs: http://chirp.danplanet.com/projects/chirp/wiki/Developers
Marco, Wow! After a long day at work I finally got a chance to apply your patch and it works perfectly! Thank you very much. Jim
On Tue, Jan 22, 2013 at 11:54 AM, IZ3GME Marco iz3gme.marco@gmail.comwrote:
Here we are, the attached patch has to be applied after yours three. I deleted redundant function definition in get_settings, also deleted some test code there, moved the definition of the convertion function _inside_ set_settings and finally debugged a little bit.
hope this can help
73 de IZ3GME Marco
On 22/01/2013 12:31, Jim Unroe wrote:
BTW It would be easier to test/debug your code if you attach the whole source or a hg patch (better) so we can see all the details and replicate the problem. 73 de IZ3GME Marco
Thanks Marco,
I'm still quite a noob at using Mercurial. I believe these 3 patch files will give you the current state that I am working within.
Jim
______________________________**_________________ chirp_devel mailing list chirp_devel@intrepid.**danplanet.com chirp_devel@intrepid.danplanet.com http://intrepid.danplanet.com/**mailman/listinfo/chirp_develhttp://intrepid.danplanet.com/mailman/listinfo/chirp_devel Developer docs: http://chirp.danplanet.com/** projects/chirp/wiki/Developershttp://chirp.danplanet.com/projects/chirp/wiki/Developers
chirp_devel mailing list chirp_devel@intrepid.danplanet.com http://intrepid.danplanet.com/mailman/listinfo/chirp_devel Developer docs: http://chirp.danplanet.com/projects/chirp/wiki/Developers
On Tue, Jan 22, 2013 at 8:12 PM, Jim Unroe rock.unroe@gmail.com wrote:
Marco, Wow! After a long day at work I finally got a chance to apply your patch and it works perfectly! Thank you very much.
Marco, After doing some additional testing today, I discovered a problem and I can't figure out what might be causing it or how to fix it.
Here's the problem.... If I enter the VFO frequency of 146.52000, it saves to an image file and uploads to the UV-5R just fine. If I enter the VFO frequency of 146.64000, it saves to an image file and uploads to the UV-5R as 146.63999
I also noticed that if I enter 146.640 into the UV-5R VFO using the keypad, download to CHIRP and then upload back to the UV-5R, the frequency is correct.
Any thoughts as to what may be causing this and how I might fix it? I have attached a single patch file that includes my 3 test patches plus your cleanup patch.
Thanks, Jim
It's a rounding problem due to the internal representation of float.
I tried the following version of convert_freq_to_bytes which seems to works but may be Dan or some other python expert have a better solution.
def convert_freq_to_bytes(freq): bytes = [ 0 for x in range(0,8) ] # init list with 8 times 0 real_freq = "%3.5f" % freq # now is a string real_freq = int(real_freq[0:3]+ real_freq[4:]) # and now an int for i in range(7, -1, -1): # go from 7 to 0 bytes[i] = real_freq%10 # extract last digit real_freq /= 10 # throw away last digit return bytes
73 de IZ3GME Marco
On 25/01/2013 05:04, Jim Unroe wrote:
On Tue, Jan 22, 2013 at 8:12 PM, Jim Unroe <rock.unroe@gmail.com mailto:rock.unroe@gmail.com> wrote:
Marco, Wow! After a long day at work I finally got a chance to apply your patch and it works perfectly! Thank you very much.
Marco, After doing some additional testing today, I discovered a problem and I can't figure out what might be causing it or how to fix it.
Here's the problem.... If I enter the VFO frequency of 146.52000, it saves to an image file and uploads to the UV-5R just fine. If I enter the VFO frequency of 146.64000, it saves to an image file and uploads to the UV-5R as 146.63999
I also noticed that if I enter 146.640 into the UV-5R VFO using the keypad, download to CHIRP and then upload back to the UV-5R, the frequency is correct.
Any thoughts as to what may be causing this and how I might fix it? I have attached a single patch file that includes my 3 test patches plus your cleanup patch.
Thanks, Jim
chirp_devel mailing list chirp_devel@intrepid.danplanet.com http://intrepid.danplanet.com/mailman/listinfo/chirp_devel Developer docs: http://chirp.danplanet.com/projects/chirp/wiki/Developers
On Fri, Jan 25, 2013 at 4:13 AM, IZ3GME Marco iz3gme.marco@gmail.comwrote:
It's a rounding problem due to the internal representation of float.
I tried the following version of convert_freq_to_bytes which seems to works but may be Dan or some other python expert have a better solution.
def convert_freq_to_bytes(freq): bytes = [ 0 for x in range(0,8) ] # init list with 8 times 0 real_freq = "%3.5f" % freq # now is a string real_freq = int(real_freq[0:3]+ real_freq[4:]) # and now an int for i in range(7, -1, -1): # go from 7 to 0 bytes[i] = real_freq%10 # extract last digit real_freq /= 10 # throw away last digit return bytes
73 de IZ3GME Marco
I still get 146.63999 out for 146.64000 in.
In settings.py, I had changed 'precision=4' to 'precision=5'
class RadioSettingValueFloat(RadioSettingValue): """A floating-point setting""" def __init__(self, minval, maxval, current, resolution=0.001, precision=4):
The reason I did this was to get 5 places past the decimal. Maybe that was the wrong way to accomplish that. Anyway, I changed back to 'precision=4' and now the result is 146.6400, which is better than 146.63999, but still not exactly what I am looking for.
Thanks again. You help and guidance is greatly appreciated. Jim KC9HI
If you want the parameter to be different than the default you simply have to specify it (eg. add precision=5 at object creation)
For vfob try this
val1b = RadioSettingValueFloat(136, 512, convert_bytes_to_freq(self._memobj.vfob.freq), precision=5)
73 de IZ3GME Marco
On 25/01/2013 12:28, Jim Unroe wrote:
On Fri, Jan 25, 2013 at 4:13 AM, IZ3GME Marco <iz3gme.marco@gmail.com mailto:iz3gme.marco@gmail.com> wrote:
It's a rounding problem due to the internal representation of float. I tried the following version of convert_freq_to_bytes which seems to works but may be Dan or some other python expert have a better solution. def convert_freq_to_bytes(freq): bytes = [ 0 for x in range(0,8) ] # init list with 8 times 0 real_freq = "%3.5f" % freq # now is a string real_freq = int(real_freq[0:3]+ real_freq[4:]) # and now an int for i in range(7, -1, -1): # go from 7 to 0 bytes[i] = real_freq%10 # extract last digit real_freq /= 10 # throw away last digit return bytes 73 de IZ3GME Marco
I still get 146.63999 out for 146.64000 in.
In settings.py, I had changed 'precision=4' to 'precision=5'
class RadioSettingValueFloat(RadioSettingValue): """A floating-point setting""" def __init__(self, minval, maxval, current, resolution=0.001, precision=4):
The reason I did this was to get 5 places past the decimal. Maybe that was the wrong way to accomplish that. Anyway, I changed back to 'precision=4' and now the result is 146.6400, which is better than 146.63999, but still not exactly what I am looking for.
Thanks again. You help and guidance is greatly appreciated. Jim KC9HI
chirp_devel mailing list chirp_devel@intrepid.danplanet.com http://intrepid.danplanet.com/mailman/listinfo/chirp_devel Developer docs: http://chirp.danplanet.com/projects/chirp/wiki/Developers
participants (4)
-
Andrew Errington
-
IZ3GME Marco
-
Jim Unroe
-
Les Niles