Although in the two examples you give it does depend on whether the previous word ends in a consonant or a vowel, the rules are a bit more complicated than that.
Basically you use "o" as the subject of an inflected verb and "fo" as the object (stopiodd o ~ mi weles i fo); "fo" is generally used after prepositions, except compound prepositions (wrtho fo ~ o'i flaen o).