@@ -78,11 +78,11 @@ def standby(self, mode):
78
78
79
79
def read (self ):
80
80
buf = self ._i2c .readfrom (self ._address , 5 )
81
- freqB = ( '{:08b}' . format ( int (buf [0 ])))[ 2 : 8 ] + '{:08b}' . format ( int ( buf [1 ]) )
82
- self .frequency = round ((( int ( freqB , 2 ) * 32768 / 4 ) - 225000 ) / 1000000 , 1 )
83
- self .is_ready = int (buf [0 ]) >> 7 == 1
84
- self .is_stereo = int (buf [2 ]) >> 7 == 1
85
- self .signal_adc_level = int (buf [3 ]) >> 4
81
+ freqB = int (( buf [0 ] & 0x3f ) * ( 1 << 8 ) + buf [1 ])
82
+ self .frequency = round ((freqB * 32768 / 4 - 225000 ) / 1000000 , 1 )
83
+ self .is_ready = int (buf [0 ] >> 7 ) == 1
84
+ self .is_stereo = int (buf [2 ] >> 7 ) == 1
85
+ self .signal_adc_level = int (buf [3 ] >> 4 )
86
86
87
87
def update (self ):
88
88
buf = bytearray (5 )
@@ -93,39 +93,40 @@ def update(self):
93
93
elif self .frequency > self .FREQ_RANGE_US [1 ]:
94
94
self .frequency = self .FREQ_RANGE_US [1 ]
95
95
else :
96
+ self .band_limits = 'JP'
96
97
if self .frequency < self .FREQ_RANGE_JP [0 ]:
97
98
self .frequency = self .FREQ_RANGE_JP [0 ]
98
99
elif self .frequency > self .FREQ_RANGE_JP [1 ]:
99
100
self .frequency = self .FREQ_RANGE_JP [1 ]
100
101
freqB = 4 * (self .frequency * 1000000 + 225000 ) / 32768
101
- freqH = int (freqB ) >> 8
102
- freqL = int (freqB ) & 0Xff
103
- cmd = '1' if self .mute_mode else '0'
104
- cmd += '1' if self .search_mode else '0'
105
- buf [0 ] = int (cmd + '{:06b}' .format (freqH ), 2 )
106
- buf [1 ] = freqL
107
- cmd = '1' if self .search_direction == '1' else '0'
102
+ buf [0 ] = int (freqB ) >> 8
103
+ if self .mute_mode :
104
+ buf [0 ] += 1 << 7
105
+ if self .search_mode :
106
+ buf [0 ] += 1 << 6
107
+ buf [1 ] = int (freqB ) & 0xff
108
+ buf [2 ] = 1 << 4
109
+ if self .search_direction == 1 :
110
+ buf [2 ] += 1 << 7
108
111
if self .search_adc_level == 10 :
109
- cmd += '11'
112
+ buf [ 2 ] += 3 << 5
110
113
elif self .search_adc_level == 7 :
111
- cmd += '10'
114
+ buf [ 2 ] += 2 << 5
112
115
elif self .search_adc_level == 5 :
113
- cmd += '01'
114
- else :
115
- cmd += '00'
116
- cmd += '1'
117
- cmd += '0' if self .stereo_mode else '1'
118
- cmd += '000'
119
- buf [2 ] = int (cmd , 2 )
120
- cmd = '0'
121
- cmd += '1' if self .standby_mode else '0'
122
- cmd += '1' if self .band_limits == 'JP' else '0'
123
- cmd += '1'
124
- cmd += '1' if self .soft_mute_mode else '0'
125
- cmd += '1' if self .high_cut_mode else '0'
126
- cmd += '1' if self .stereo_noise_cancelling_mode else '0'
127
- cmd += '0'
128
- buf [3 ] = int (cmd , 2 )
116
+ buf [2 ] += 1 << 5
117
+ if self .stereo_mode :
118
+ buf [2 ] += 1 << 3
119
+ buf [3 ] = 1 << 4
120
+ if self .standby_mode :
121
+ buf [3 ] += 1 << 6
122
+ if self .band_limits == 'JP' :
123
+ buf [3 ] += 1 << 5
124
+ if self .soft_mute_mode :
125
+ buf [3 ] += 1 << 3
126
+ if self .high_cut_mode :
127
+ buf [3 ] += 1 << 2
128
+ if self .stereo_noise_cancelling_mode :
129
+ buf [3 ] += 1 << 1
129
130
buf [4 ] = 0
130
131
self ._i2c .writeto (self ._address , buf )
131
132
self .read ()
0 commit comments