library ieee; use ieee.std_logic_1164.all; use ieee.std_logic_arith.all; use ieee.std_logic_unsigned.all; entity kr_leds is port( clk, reset, obj_ahead, stop : in std_logic; intensity : in std_logic_vector(7 downto 0); left_right, fwd_back : in std_logic_vector(3 downto 0); led_out : buffer std_logic_vector(7 downto 0) ); end kr_leds; architecture behavioural of kr_leds is --constant max_count: std_logic_vector(13 downto 0) := "00000000000010"; --"10011100010000"; TYPE STATE_TYPE IS (led0, led1, led2, led3, led4, led5, led6, led7); signal count : std_logic_vector(13 downto 0); signal max_count : std_logic_vector(13 downto 0); signal counted, go_left, back : std_logic; signal state : state_type; signal normal_out: std_logic_vector(7 downto 0); signal obj_ahead_out: std_logic_vector(7 downto 0); signal stop_out, back_out: std_logic_vector(7 downto 0); signal sample: std_logic_vector(2 downto 0); begin --the max_count is inversely propotional to the intensity of light -- plus an offset value max_count <= (not intensity) & "111111"; -- assign output for stop mode stop_out <= "11111111"; -- process to do counting to determine when to change states counting: process(clk, reset) begin if reset = '0' then count <= (others =>'0'); elsif rising_edge(clk) then if count >= max_count then count <= (others => '0'); counted <= '1'; else count <= count + 1; counted <= '0'; end if; end if; end process counting; -- if object ahead -- blinking blinking: process(clk) begin if rising_edge(clk) then if fwd_back > "0111" then back <= '1'; else back <= '0'; end if; if counted = '1' then obj_ahead_out <= not obj_ahead_out; end if; end if; end process blinking; -- if no object ahead -- process to for the leds to light from left/right direction: process(clk) begin if rising_edge(clk) then if left_right = "0111" then if state = led0 then go_left <= '0'; elsif state = led7 then go_left <= '1'; end if; elsif left_right > "0111" then go_left <= '0'; else go_left <= '1'; end if; end if; end process direction; -- direction2: process(state) -- begin -- if state = led0 then -- go_left <= '0'; -- elsif state = led7 then -- go_left <= '1'; -- end if; -- end process direction2; process(clk, reset) begin if reset = '0' then state <= led0; go_left <= '0'; elsif rising_edge(clk) then case state is when led0 => if counted = '0' then state <= led0; elsif counted = '1' then if go_left = '0' then state <= led1; else state <= led7; end if; end if; when led1 => if counted = '0' then state <= led1; elsif counted = '1' then if go_left = '0' then state <= led2; else state <= led0; end if; end if; when led2 => if counted = '0' then state <= led2; elsif counted = '1' then if go_left = '0' then state <= led3; else state <= led1; end if; end if; when led3 => if counted = '0' then state <= led3; elsif counted = '1' then if go_left = '0' then state <= led4; else state <= led2; end if; end if; when led4 => if counted = '0' then state <= led4; elsif counted = '1' then if go_left = '0' then state <= led5; else state <= led3; end if; end if; when led5 => if counted = '0' then state <= led5; elsif counted = '1' then if go_left = '0' then state <= led6; else state <= led4; end if; end if; when led6 => if counted = '0' then state <= led6; elsif counted = '1' then if go_left = '0' then state <= led7; else state <= led5; end if; end if; when led7 => if counted = '0' then state <= led7; elsif counted = '1' then if go_left = '0' then state <= led0; else state <= led6; end if; end if; end case; end if; end process; -- assign output of states WITH state SELECT normal_out <= "00000001" WHEN led0, "00000010" WHEN led1, "00000100" WHEN led2, "00001000" WHEN led3, "00010000" WHEN led4, "00100000" WHEN led5, "01000000" WHEN led6, "10000000" WHEN led7; WITH state SELECT back_out <= "00000011" WHEN led0, "00000011" WHEN led1, "00001100" WHEN led2, "00001100" WHEN led3, "00110000" WHEN led4, "00110000" WHEN led5, "11000000" WHEN led6, "11000000" WHEN led7; -- assign actual output of the leds sample <= obj_ahead & stop & back; with sample select led_out <= obj_ahead_out when "100", stop_out when "010"|"110"|"011"|"111", back_out when "001", normal_out when others; end behavioural;