firmware  v0.1.2
Chromation Spectrometer Dev-Kit
test_runner.c
1 // Pick a sensor
2 #ifdef LIS // <------------- $ make sensor=LIS
3 #include "LisConfigs.h"
4 #endif
5 #ifdef S13131 // <---------- $ make sensor=S13131
6 #endif
7 
8 #include <unity.h>
9 #include <Mock.h>
10 #include "HardwareFake.h" // Fake hardware (registers, pins)
11 #include "StatusCodes.h" // Python-to-Firmware communication status codes
12 
13 #include "test_example.h"
14 #include "test_VisCmd.h"
15 #include "test_AutoExpose.h"
16 
17 void (*setUp)(void);
18 void EmptySetUp(void) {
19 }
20 void (*tearDown)(void);
21 void EmptyTearDown(void) {
22 }
23 Mock_s *mock; // Tests record mocked calls and call args here
24 void SetUp_Mock(void){
25  mock = Mock_new();
26 }
27 void TearDown_Mock(void){
28  Mock_destroy(mock);
29  mock = NULL;
30 }
31 
32 bool Yep=true, Nope=false; // Yep/Nope := tests on/off
33 
34 void test_example(bool run_test)
35 {
36  if (run_test)
37  {
38  setUp = EmptySetUp; tearDown = EmptyTearDown;
39  RUN_TEST(test_Can_build_test_suite);
40  RUN_TEST(test_Can_call_function_defined_in_app_lib);
41  RUN_TEST(test_Can_call_inline_function_defined_in_app_lib);
42  }
43 }
44 
45 void test_VisCmd(bool run_test)
46 {
47  if (run_test)
48  {
49  puts("# test VisCmd");
50 
51  puts("## VisCmd.LedNumIsValid");
52  setUp = EmptySetUp;
53  tearDown = EmptyTearDown;
54  RUN_TEST(LedNumIsValid_returns_TRUE_if_led_num_is_0);
55  RUN_TEST(LedNumIsValid_returns_TRUE_if_led_num_is_1);
56  RUN_TEST(LedNumIsValid_returns_FALSE_if_led_num_is_not_0_or_1);
57 
58 #ifdef LIS
59  puts("## VisCmd.LisReadout");
60  setUp = EmptySetUp;
61  tearDown = EmptyTearDown;
62  RUN_TEST(LisReadout_waits_for_Lis_Sync_to_go_HIGH_then_go_LOW);
63  RUN_TEST(LisReadout_reads_one_pixel_on_each_rising_edge_of_Lis_Clk);
64  RUN_TEST(LisReadout_LOOP_wait_for_the_rising_edge_of_Lis_Clk);
65  RUN_TEST(LisReadout_LOOP_start_the_ADC_conversion);
66  RUN_TEST(LisReadout_LOOP_wait_for_45_cycles_of_10MHz_clock);
67  RUN_TEST(LisReadout_LOOP_start_ADC_readout);
68  RUN_TEST(LisReadout_LOOP_wait_for_most_significant_byte_ADC_readout);
69  RUN_TEST(LisReadout_LOOP_save_MSB_to_frame_buffer);
70  RUN_TEST(LisReadout_LOOP_wait_for_least_significant_byte_ADC_readout);
71  RUN_TEST(LisReadout_LOOP_save_LSB_to_frame_buffer);
72 #endif
73 #ifdef S13131
74  puts("## VisCmd.S13131Readout");
75  setUp = SetUp_Mock;
76  tearDown = TearDown_Mock;
77  RUN_TEST(S13131Readout_must_be_called_immediately_after_S13131Expose);
78  RUN_TEST(S13131Readout_stores_pixel_values_in_a_global_array_named_frame);
79  RUN_TEST(S13131Readout_reads_one_pixel_on_each_falling_edge_of_CLK);
80  RUN_TEST(S13131Readout_LOOP_wait_for_a_CLK_falling_edge);
81  RUN_TEST(S13131Readout_LOOP_start_the_ADC_conversion);
82  RUN_TEST(S13131Readout_LOOP_wait_for_45_cycles_of_10MHz_clock);
83  RUN_TEST(S13131Readout_LOOP_start_ADC_readout);
84  RUN_TEST(S13131Readout_LOOP_wait_for_most_significant_byte_ADC_readout);
85  RUN_TEST(S13131Readout_LOOP_save_MSB_to_frame_buffer);
86  RUN_TEST(S13131Readout_LOOP_wait_for_least_significant_byte_ADC_readout);
87  RUN_TEST(S13131Readout_LOOP_save_LSB_to_frame_buffer);
88 
89 #endif
90 
91  puts("## VisCmd.GetSensorLED");
92  setUp = SetUp_Mock;
93  tearDown = TearDown_Mock;
94  RUN_TEST(GetSensorLED_waits_for_byte_led_num);
95  RUN_TEST(GetSensorLED_reads_byte_led_num);
96  RUN_TEST(GetSensorLED_sends_OK_and_LED_SETTING_if_led_num_is_valid);
97  RUN_TEST(GetSensorLED_sends_ERROR_and_pads_second_byte_if_led_num_is_invalid);
98 
99  puts("## VisCmd.SetSensorLED");
100  setUp = SetUp_Mock;
101  tearDown = TearDown_Mock;
102  RUN_TEST(SetSensorLED_waits_for_byte_led_num);
103  RUN_TEST(SetSensorLED_reads_byte_led_num);
104  RUN_TEST(SetSensorLED_waits_for_byte_led_setting);
105  RUN_TEST(SetSensorLED_reads_byte_led_setting);
106  RUN_TEST(SetSensorLED_sends_ERROR_if_led_num_is_invalid);
107  RUN_TEST(SetSensorLED_sends_ERROR_if_led_setting_is_invalid);
108  RUN_TEST(SetSensorLED_applies_LED_setting_if_valid);
109  RUN_TEST(SetSensorLED_sends_OK_if_num_and_setting_are_valid);
110 
111 #ifdef LIS
112  puts("## VisCmd.GetSensorConfig");
113  setUp = SetUp_Mock;
114  tearDown = TearDown_Mock;
115  RUN_TEST(GetSensorConfig_sends_OK);
116  RUN_TEST(GetSensorConfig_sends_binning);
117  RUN_TEST(GetSensorConfig_sends_gain);
118  RUN_TEST(GetSensorConfig_sends_row_bitmap);
119 
120  puts("## VisCmd.SetSensorConfig");
121  setUp = SetUp_Mock;
122  tearDown = TearDown_Mock;
123  RUN_TEST(SetSensorConfig_replies_OK_to_confirm_it_recognized_this_command);
124  RUN_TEST(SetSensorConfig_receives_three_bytes_of_config_from_Bridge);
125  RUN_TEST(SetSensorConfig_does_not_update_config_globals_if_config_is_invalid);
126  RUN_TEST(SetSensorConfig_replies_ERROR_if_binning_is_invalid);
127  RUN_TEST(SetSensorConfig_replies_ERROR_if_gain_is_invalid);
128  RUN_TEST(SetSensorConfig_replies_ERROR_if_active_rows_is_invalid);
129  RUN_TEST(SetSensorConfig_does_not_program_LIS_770i_if_config_is_invalid);
130  RUN_TEST(SetSensorConfig_writes_valid_config_to_LIS_770i_programmable_setup_register);
131  RUN_TEST(SetSensorConfig_replies_OK_if_all_config_values_are_valid);
132  RUN_TEST(SetSensorConfig_the_OK_is_sent_after_LIS_is_programmed_with_new_config);
133 #endif
134 #ifdef S13131
135  // S13131 has nothing to configure
136 #endif
137 
138 
139  puts("## VisCmd.GetExposure");
140  setUp = EmptySetUp;
141  tearDown = EmptyTearDown;
142  RUN_TEST(GetExposure_sends_OK);
143  RUN_TEST(GetExposure_sends_MSB_of_exposure_ticks);
144  RUN_TEST(GetExposure_sends_LSB_of_exposure_ticks);
145 
146  puts("## VisCmd.SetExposure");
147  setUp = EmptySetUp;
148  tearDown = EmptyTearDown;
149  RUN_TEST(SetExposure_waits_for_byte_exposure_MSB);
150  RUN_TEST(SetExposure_reads_byte_exposure_MSB);
151  RUN_TEST(SetExposure_waits_for_byte_exposure_LSB);
152  RUN_TEST(SetExposure_reads_byte_exposure_LSB);
153  RUN_TEST(SetExposure_updates_global_exposure_ticks);
154  RUN_TEST(SetExposure_sends_OK);
155 
156  puts("## VisCmd.CaptureFrame");
157  setUp = EmptySetUp;
158  tearDown = EmptyTearDown;
159  RUN_TEST(CaptureFrame_sends_OK);
160  RUN_TEST(CaptureFrame_checks_binning_to_determine_number_of_pixels_in_frame);
161  RUN_TEST(CaptureFrame_sends_num_pixels_MSB);
162  RUN_TEST(CaptureFrame_sends_num_pixels_LSB);
163  RUN_TEST(CaptureFrame_exposes_the_pixels);
164  RUN_TEST(CaptureFrame_does_readout_of_num_pixels_into_the_frame_buffer);
165  RUN_TEST(CaptureFrame_sends_the_pixel_readings_stored_in_the_frame_buffer);
166 
167 
168 
169  puts("## VisCmd.GetPeak");
170  setUp = EmptySetUp;
171  tearDown = EmptyTearDown;
172  RUN_TEST(GetPeak_finds_the_peak_between_start_pixel_and_stop_pixel_inclusive);
173  RUN_TEST(GetPeak_ignores_peaks_at_pixels_before_start_pixel_and_after_stop_pixel);
174 
175  puts("## VisCmd.GetSensorHash");
176  setUp = SetUp_Mock;
177  tearDown = TearDown_Mock;
178  RUN_TEST(GetSensorHash_hash_sends_OK);
179  RUN_TEST(GetSensorHash_sends_first_three_bytes_of_SHA1_hash_of_sensor);
180  }
181 }
182 
183 void test_AutoExpose(bool run_test)
184 {
185  if (run_test)
186  {
187  puts("# test AutoExpose");
188 
189  puts("## VisCmd.AutoExpose");
190  setUp = SetUp_Mock;
191  tearDown = TearDown_Mock;
192  RUN_TEST(AutoExpose_turns_led1_red_to_indicate_starting);
193  RUN_TEST(AutoExpose_sets_min_peak_at_target_minus_tolerance);
194  RUN_TEST(AutoExpose_clamps_min_peak_at_max_dark_if_target_minus_tolerance_is_GREATER_THAN_target);
195  RUN_TEST(AutoExpose_clamps_min_peak_at_max_dark_if_target_minus_tolerance_is_LESS_THAN_max_dark);
196  RUN_TEST(AutoExpose_sets_max_peak_at_target_plus_tolerance);
197  RUN_TEST(AutoExpose_clamps_max_peak_at_65535_counts_if_target_plus_tolerance_is_LESS_THAN_target);
198  RUN_TEST(AutoExpose_loops_until_done);
199  RUN_TEST(AutoExpose_exposes_the_pixels);
200  RUN_TEST(AutoExpose_reads_pixel_counts_into_global_frame_buffer);
201  RUN_TEST(AutoExpose_finds_frame_peak_in_range_start_pixel_to_stop_pixel);
202  RUN_TEST(AutoExpose_is_done_if_peak_less_than_max_dark_AND_exposure_at_max);
203  RUN_TEST(AutoExpose_scales_exposure_by_10_if_peak_less_than_max_dark);
204  RUN_TEST(AutoExpose_clamps_exposure_at_max_exposure_if_10_x_exposure_is_GREATER_THAN_max_exposure);
205  RUN_TEST(AutoExpose_scales_exposure_by_half_if_peak_ABOVE_max_peak);
206  RUN_TEST(AutoExpose_clamps_exposure_at_min_exposure_if_half_exposure_is_LESS_THAN_min_exposure);
207  RUN_TEST(AutoExpose_is_done_if_peak_BELOW_min_peak_and_exposure_at_max_exposure);
208  RUN_TEST(AutoExpose_scales_exposure_by_target_div_peak_if_peak_BELOW_min_peak_and_exposure_not_at_max);
209  RUN_TEST(AutoExpose_clamps_exposure_at_max_exposure_if_gain_is_GREATER_THAN_max_exposure);
210  RUN_TEST(AutoExpose_is_done_if_peak_is_in_the_target_range);
211  RUN_TEST(AutoExpose_turns_led1_green_to_indicate_it_hit_the_target_range);
212  RUN_TEST(AutoExpose_gives_up_if_it_iterates_for_max_tries);
213 
214  }
215 }
216 
217 int main(void)
218 {
219  UNITY_BEGIN();
220  test_example(Nope);
221  test_VisCmd(Yep);
222  test_AutoExpose(Yep);
223  puts("# Results");
224  return UNITY_END();
225 }
See LisConfig.h for context.
See StatusCode.h for context.
Definition: Mock.c:5