I think there are ICs for that specific purpose? If I understood your second paragraph correctly.
Also this should work.
Also this should work.