Skip to content

Commit

Permalink
Switch to C++ to C. Thanks Michael Stilkerich; I never liked C++ anyway.
Browse files Browse the repository at this point in the history
  • Loading branch information
risacher committed Sep 16, 2022
1 parent 49f9a3f commit 151d834
Show file tree
Hide file tree
Showing 8 changed files with 186 additions and 317 deletions.
8 changes: 4 additions & 4 deletions SunwaitPi.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@
<None Include="LICENSE" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="print.cpp" />
<ClCompile Include="sunriset.cpp" />
<ClCompile Include="sunwait.cpp" />
<ClCompile Include="print.c" />
<ClCompile Include="sunriset.c" />
<ClCompile Include="sunwait.c" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="print.h" />
Expand All @@ -94,4 +94,4 @@
</ItemDefinitionGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets" />
</Project>
</Project>
14 changes: 9 additions & 5 deletions makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,23 @@
# 08/12/2014 IFC 0.6 No changes since 0.5
# 02/05/2015 IFC 0.7 No changes since 0.5, still
# 10/03/2020 TLJ 0.9 Move headers to fix build on osx
# 2022-09-16 DRR 0.91 Accept changes from @mstilkerich Michael Stilkerich switches to C from C++
#


CFLAGS=-c -Wall -std=c99 -O2 -Wextra -pedantic
CXXFLAGS=-c -Wall
LDFLAGS= -lm
SOURCES=sunwait.cpp sunriset.cpp print.cpp
SOURCES=sunwait.c sunriset.c print.c
HEADERS=sunwait.h sunriset.h print.h
OBJECTS=$(SOURCES:.cpp=.o)
OBJECTS=$(SOURCES:.c=.o)
EXECUTABLE=sunwait
all: $(SOURCES) $(EXECUTABLE)
all: $(EXECUTABLE)

.c.o:
$(CC) $(CFLAGS) $< -o $@

$(EXECUTABLE): $(OBJECTS)
$(CXX) $(OBJECTS) -o $@ $(LDFLAGS)
$(CC) $(OBJECTS) -o $@ $(LDFLAGS)

clean:
rm -f *.o sunwait
10 changes: 5 additions & 5 deletions openwrt/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,18 @@ define Package/sunwait
CATEGORY:=Utilities
TITLE:=Sunwait
URL:=URL:=https://github.com/risacher/sunwait
DEPENDS:=+libstdcpp +libc
DEPENDS:=+libc
endef

define Package/sunwait/description
Wait until the specified solar event
endef

define Build/Compile
$(TARGET_CC) $(TARGET_CPPFLAGS) -o $(PKG_BUILD_DIR)/sunwait.o -c $(PKG_BUILD_DIR)/sunwait.cpp
$(TARGET_CC) $(TARGET_CPPFLAGS) -o $(PKG_BUILD_DIR)/sunriset.o -c $(PKG_BUILD_DIR)/sunriset.cpp
$(TARGET_CC) $(TARGET_CPPFLAGS) -o $(PKG_BUILD_DIR)/print.o -c $(PKG_BUILD_DIR)/print.cpp
$(TARGET_CC) $(TARGET_LDFLAGS) -o $(PKG_BUILD_DIR)/$1 $(PKG_BUILD_DIR)/sunwait.o $(PKG_BUILD_DIR)/sunriset.o $(PKG_BUILD_DIR)/print.o -lm -lstdc++
$(TARGET_CC) $(TARGET_CPPFLAGS) -o $(PKG_BUILD_DIR)/sunwait.o -c $(PKG_BUILD_DIR)/sunwait.c
$(TARGET_CC) $(TARGET_CPPFLAGS) -o $(PKG_BUILD_DIR)/sunriset.o -c $(PKG_BUILD_DIR)/sunriset.c
$(TARGET_CC) $(TARGET_CPPFLAGS) -o $(PKG_BUILD_DIR)/print.o -c $(PKG_BUILD_DIR)/print.c
$(TARGET_CC) $(TARGET_LDFLAGS) -o $(PKG_BUILD_DIR)/$1 $(PKG_BUILD_DIR)/sunwait.o $(PKG_BUILD_DIR)/sunriset.o $(PKG_BUILD_DIR)/print.o -lm
endef

define Package/sunwait/install
Expand Down
92 changes: 31 additions & 61 deletions print.cpp → print.c
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@

/*
** print.cpp (of sunwait)
** print.c (of sunwait)
**
** Who Ver When What
** IFC 0.5 04-12-2014 Fix my 1st release of sunwait for windows and port to linux
** IFC 0.6 08-12-2014 Add timezone for output of timings
** IFC 0.7 30-04-2015 Fix timexone and DST trouble - and problems near dateline
** IFC 0.8 2015-05-27 Resolve 'dodgy day' and cleanup
** DRR 0.91 2022-09-16 Accept change to C from mstilkerich Michael Stilkerich
**
*/

#include <stdio.h>
#include <iostream>
#include <math.h>
#include <time.h>

Expand All @@ -24,17 +24,9 @@ static const char* cComma = ", ";

#define NO_OFFSET 0.0

inline double myDayLength (const double pDouble1, const double pDouble2)
{ return myAbs (pDouble1 - pDouble2);
}

inline double myDayLength (const targetStruct *pTarget)
{ return pTarget->diurnalArc;
}

// The user-specified offset reduces the diurnal arc, at sunrise AND sunset.
// But make sure dawn aways is before dusk. The offset can mess that up.
double diurnalArcWithOffset1 (const double pDiurnalArc, const double pOffset)
static double diurnalArcWithOffset1 (const double pDiurnalArc, const double pOffset)
{ double arcWithOffset = pDiurnalArc - pOffset - pOffset;
if (arcWithOffset >= 24.0) return 24.0;
if (arcWithOffset <= 0.0) return 0.0;
Expand All @@ -46,7 +38,7 @@ double diurnalArcWithOffset (const runStruct *pRun, const targetStruct *pTarget)
}

// What time, in hours UTC, is the offset sunrise?
double getOffsetRiseHourUTC1 (const double pSouthHourUTC, const double pDiurnalArc, const double pOffsetHour)
static double getOffsetRiseHourUTC1 (const double pSouthHourUTC, const double pDiurnalArc, const double pOffsetHour)
{ return pSouthHourUTC - diurnalArcWithOffset1 (pDiurnalArc, pOffsetHour)/2.0;
}
// Simpler to use form
Expand All @@ -55,7 +47,7 @@ double getOffsetRiseHourUTC (const runStruct *pRun, const targetStruct *pTarget)
}

// What time, in hours UTC, is the offset sunset?
double getOffsetSetHourUTC1 (const double pSouthHourUTC, const double pDiurnalArc, const double pOffsetHour)
static double getOffsetSetHourUTC1 (const double pSouthHourUTC, const double pDiurnalArc, const double pOffsetHour)
{ return pSouthHourUTC + diurnalArcWithOffset1 (pDiurnalArc, pOffsetHour)/2.0;
}
// Simpler to use form
Expand All @@ -64,11 +56,11 @@ double getOffsetSetHourUTC (const runStruct *pRun, const targetStruct *pTarget)
}


void print_a_time
static void print_a_time
( const OnOff pGmt_OnOff
, const time_t *pMidnightTimet
, const double pEventHour
)
)
{ struct tm tmpTm;
char tmpBuffer [80];

Expand All @@ -89,64 +81,42 @@ void print_a_time
printf ("%s", tmpBuffer);
}

void print_a_sun_time
static void print_a_sun_time
( const OnOff pGmt_OnOff
, const time_t *pMidnightTimet
, const double pEventHour
, const double pOffsetDiurnalArc
)
)
{ // A positive offset reduces the diurnal arc
if (pOffsetDiurnalArc <= 0.0 || pOffsetDiurnalArc >= 24.0)
printf ("--:--");
else
print_a_time (pGmt_OnOff, pMidnightTimet, pEventHour);
}

void print_times
( const OnOff pGmt
, const OnOff pSunrise
, const OnOff pSunset
, const time_t pMidnightTimet
, const double pSouthHour
, const double pDiurnalArc
, const double pOffset
, const char *pSeparator
)
{ double offsetDiurnalArc = diurnalArcWithOffset1 (pDiurnalArc, pOffset);
double riseHour = getOffsetRiseHourUTC1 (pSouthHour, pDiurnalArc, pOffset);
double setHour = getOffsetSetHourUTC1 (pSouthHour, pDiurnalArc, pOffset);
static void print_times
( const runStruct *pRun
, const targetStruct *pTarget
, const double pOffsetHour
, const char *pSeparator
)
{ double offsetDiurnalArc = diurnalArcWithOffset1 (pTarget->diurnalArc, pOffsetHour);
double riseHour = getOffsetRiseHourUTC1 (pTarget->southHourUTC, pTarget->diurnalArc, pOffsetHour);
double setHour = getOffsetSetHourUTC1 (pTarget->southHourUTC, pTarget->diurnalArc, pOffsetHour);

if (pSunrise == ONOFF_ON)
print_a_sun_time (pGmt, &pMidnightTimet, riseHour, offsetDiurnalArc);
if (pSunrise == ONOFF_ON && pSunset == ONOFF_ON)
if (pRun->reportSunrise == ONOFF_ON)
print_a_sun_time (pRun->utc, &pRun->targetTimet, riseHour, offsetDiurnalArc);
if (pRun->reportSunrise == ONOFF_ON && pRun->reportSunset == ONOFF_ON)
printf ("%s", pSeparator);
if (pSunset == ONOFF_ON)
print_a_sun_time (pGmt, &pMidnightTimet, setHour, offsetDiurnalArc);
if (pRun->reportSunset == ONOFF_ON)
print_a_sun_time (pRun->utc, &pRun->targetTimet, setHour, offsetDiurnalArc);

if (offsetDiurnalArc >= 24.0) printf (" (Midnight sun)");
else if (offsetDiurnalArc <= 0.0) printf (" (Polar night)");

printf ("\n");
}

inline void print_times
( const runStruct *pRun
, const targetStruct *pTarget
, const double pOffsetHour
, const char *pSeparator
)
{ print_times
( pRun->utc
, pRun->reportSunrise
, pRun->reportSunset
, pRun->targetTimet
, pTarget->southHourUTC
, pTarget->diurnalArc
, pOffsetHour
, pSeparator
);
}

inline void print_twilight
( const double pDayLength
, const double pTwilightLength
Expand All @@ -161,7 +131,7 @@ inline void print_twilight
void generate_report (const runStruct *pRun)
{
/*
** Generate and save sunrise and sunset times for target
** Generate and save sunrise and sunset times for target
*/

targetStruct tmpTarget;
Expand All @@ -173,7 +143,7 @@ void generate_report (const runStruct *pRun)
double twilightAngleTarget = tmpTarget.twilightAngle;

/*
** Now generate the report
** Now generate the report
*/

struct tm nowTm;
Expand All @@ -190,19 +160,19 @@ void generate_report (const runStruct *pRun)
}

printf ("\n");

strftime (buffer, 80, "%d-%b-%Y %H:%M %Z", &nowTm);
printf
printf
(" Current Date and Time: %s\n", buffer);

printf ("\n\nTarget Information ...\n\n");

printf
printf
(" Location: %10.6fN, %10.6fE\n"
, pRun->latitude
, pRun->longitude
);

strftime (buffer, 80, "%d-%b-%Y", &targetTm);
printf
(" Date: %s\n", buffer);
Expand Down Expand Up @@ -231,14 +201,14 @@ void generate_report (const runStruct *pRun)
else printf(" Twilight angle: %5.2f degrees (custom angle)\n", twilightAngleTarget);

printf (" Day with twilight: "); print_times (pRun, &tmpTarget, NO_OFFSET, cTo);

if (pRun->offsetHour != NO_OFFSET)
{ printf (" Day with twilight & offset: "); print_times (pRun, &tmpTarget, pRun->offsetHour, cTo); }

printf (" It is: %s\n", isDay (pRun) == ONOFF_ON ? "Day (or twilight)" : "Night");

/*
** Generate times for different types of twilight
** Generate times for different types of twilight
*/

targetStruct daylightTarget;
Expand Down
6 changes: 2 additions & 4 deletions print.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// 08-12-2014 IFC 0.6 No changes from 0.5
// 02-05-2015 IFC 0.7 No changes from 0.5, still
// 2015-05-27 IFC 0.8 Resolve 'dodgy day' and cleanup
// 2022-09-16 DRR 0.91 Convert to C courtesy mstilkerich
//

#ifndef PRINT_H
Expand All @@ -12,15 +13,12 @@
#include "sunwait.h"
#include "sunriset.h"

void generate_report (const runStruct *pRun);
void generate_report(const runStruct *pRun);

void print_list (const runStruct *pRun);

double diurnalArcWithOffsetX (const double pDiurnalArc, const double pOffset);
double diurnalArcWithOffset (const runStruct *pRun, const targetStruct *pTarget);
double getOffsetRiseHourUTCX (const double pSouthHourUTC, const double pDiurnalArc, const double pOffsetHour);
double getOffsetRiseHourUTC (const runStruct *pRun, const targetStruct *pTarget);
double getOffsetSetHourUTCX (const double pSouthHourUTC, const double pDiurnalArc, const double pOffsetHour);
double getOffsetSetHourUTC (const runStruct *pRun, const targetStruct *pTarget);


Expand Down
Loading

0 comments on commit 151d834

Please sign in to comment.